Class: Medusa::Directory
- Includes:
- Resource
- Defined in:
- lib/medusa/directory.rb
Overview
Represents a Medusa directory.
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
Class Method Summary collapse
-
.from_json(json) ⇒ Medusa::Directory
Initializes a new instance from a JSON fragment.
-
.with_id(id) ⇒ Medusa::Directory
Returns a new instance with the given ID.
-
.with_uuid(uuid) ⇒ Medusa::Directory
Returns a new instance with the given UUID.
Instance Method Summary collapse
- #directories ⇒ Enumerable<Medusa::Directory>
-
#directory_tree_url ⇒ String
URI of the corresponding Medusa directory tree resource.
- #files ⇒ Enumerable<Medusa::File>
-
#initialize ⇒ Directory
constructor
A new instance of Directory.
-
#load ⇒ Object
Updates the instance with current properties from Medusa.
-
#parent ⇒ Medusa::Directory
The parent directory, or
nil
if the instance is a root directory within a file group. -
#url ⇒ String
Absolute URI of the corresponding Medusa resource.
- #walk_tree {|_self| ... } ⇒ Object
Methods included from Resource
Methods included from Uuidable
Methods inherited from Node
Constructor Details
#initialize ⇒ Directory
Returns a new instance of Directory.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/medusa/directory.rb', line 51 def initialize super # These relate to the /cfs_directories/:id.json representation. @parent_id = nil @files = Set.new @directories = Set.new @loading = false @loaded = false # These relate to the /cfs_directories/:id/show_tree.json representation. @directory_tree = nil @directory_tree_loaded = false end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
10 11 12 |
# File 'lib/medusa/directory.rb', line 10 def json @json end |
Class Method Details
.from_json(json) ⇒ Medusa::Directory
Initializes a new instance from a JSON fragment.
18 19 20 21 22 23 |
# File 'lib/medusa/directory.rb', line 18 def self.from_json(json) dir = Medusa::Directory.new dir.json = json dir.load dir end |
.with_id(id) ⇒ Medusa::Directory
Returns a new instance with the given ID. Existence is not checked, so an instance is returned regardless of whether the ID is valid.
32 33 34 35 36 |
# File 'lib/medusa/directory.rb', line 32 def self.with_id(id) dir = Directory.new dir.instance_variable_set('@id', id) dir end |
.with_uuid(uuid) ⇒ Medusa::Directory
Returns a new instance with the given UUID. Existence is not checked, so an instance is returned regardless of whether the UUID is valid.
45 46 47 48 49 |
# File 'lib/medusa/directory.rb', line 45 def self.with_uuid(uuid) dir = Directory.new dir.instance_variable_set('@uuid', uuid) dir end |
Instance Method Details
#directories ⇒ Enumerable<Medusa::Directory>
67 68 69 70 |
# File 'lib/medusa/directory.rb', line 67 def directories load @directories end |
#directory_tree_url ⇒ String
Returns URI of the corresponding Medusa directory tree resource.
76 77 78 |
# File 'lib/medusa/directory.rb', line 76 def directory_tree_url self.url + '/show_tree.json' end |
#files ⇒ Enumerable<Medusa::File>
83 84 85 86 |
# File 'lib/medusa/directory.rb', line 83 def files load @files end |
#load ⇒ Object
Updates the instance with current properties from Medusa.
It should not typically be necessary to use this method publicly.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/medusa/directory.rb', line 93 def load return if @loading || @loaded @loading = true struct = json ? json : fetch_body @id = struct['id'] @relative_key = struct['relative_pathname'] @uuid = struct['uuid'] @parent_id = struct['parent_directory']['id'] if struct['parent_directory'] if struct['subdirectories'].respond_to?(:each) struct['subdirectories'].each do |subdir| @directories << Directory.with_id(subdir['id']) end end if struct['files'].respond_to?(:each) struct['files'].each do |file| @files << File.from_json(file) end end @loaded = true ensure @loading = false end |
#parent ⇒ Medusa::Directory
Returns The parent directory, or nil
if the instance is a root directory within a file group.
120 121 122 123 |
# File 'lib/medusa/directory.rb', line 120 def parent load @parent ||= ::Medusa::Directory.with_id(@parent_id) if @parent_id end |
#url ⇒ String
Returns Absolute URI of the corresponding Medusa resource.
129 130 131 132 133 |
# File 'lib/medusa/directory.rb', line 129 def url [::Medusa::Client.configuration[:medusa_base_url].chomp('/'), 'cfs_directories', self.id].join('/') end |
#walk_tree {|_self| ... } ⇒ Object
Pass a block to invoke it on every File and Medusa::Directory node at any level within this instance, including the instance itself.
139 140 141 142 143 |
# File 'lib/medusa/directory.rb', line 139 def walk_tree(&block) load_directory_tree yield self walk(@directory_tree, &block) end |