Class: Medusa::Directory

Inherits:
Node
  • Object
show all
Includes:
Resource
Defined in:
lib/medusa/directory.rb

Overview

Represents a Medusa directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#exists?, #id, #uuid

Methods included from Uuidable

#uuid_url

Methods inherited from Node

#name, #relative_key

Constructor Details

#initializeDirectory

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

#jsonObject

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.

Parameters:

  • json (Hash)

Returns:



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.

Parameters:

  • id (Integer)

Returns:



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.

Parameters:

  • uuid (String)

Returns:



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

#directoriesEnumerable<Medusa::Directory>

Returns:



67
68
69
70
# File 'lib/medusa/directory.rb', line 67

def directories
  load
  @directories
end

#directory_tree_urlString

Returns URI of the corresponding Medusa directory tree resource.

Returns:

  • (String)

    URI of the corresponding Medusa directory tree resource.

See Also:



76
77
78
# File 'lib/medusa/directory.rb', line 76

def directory_tree_url
  self.url + '/show_tree.json'
end

#filesEnumerable<Medusa::File>

Returns:



83
84
85
86
# File 'lib/medusa/directory.rb', line 83

def files
  load
  @files
end

#loadObject

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

#parentMedusa::Directory

Returns The parent directory, or nil if the instance is a root directory within a file group.

Returns:

  • (Medusa::Directory)

    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

#urlString

Returns Absolute URI of the corresponding Medusa resource.

Returns:

  • (String)

    Absolute URI of the corresponding Medusa resource.

See Also:



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.

Yields:

  • (_self)

Yield Parameters:



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