Class: Medusa::File
Overview
Represents a Medusa file.
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
Class Method Summary collapse
-
.from_json(json) ⇒ Medusa::File
Initializes a new instance from a JSON fragment.
-
.with_id(id) ⇒ Medusa::File
Returns a new instance with the given ID.
-
.with_uuid(uuid) ⇒ Medusa::File
Returns a new instance with the given UUID.
Instance Method Summary collapse
- #directory ⇒ Medusa::Directory
-
#initialize ⇒ File
constructor
A new instance of File.
-
#load ⇒ Object
Updates the instance with current properties from Medusa.
- #md5_sum ⇒ String
- #media_type ⇒ String
- #mtime ⇒ Time
- #size ⇒ Integer
-
#url ⇒ String
Absolute URI of the corresponding Medusa resource.
Methods included from Resource
Methods included from Uuidable
Methods inherited from Node
Constructor Details
#initialize ⇒ File
Returns a new instance of File.
51 52 53 54 |
# File 'lib/medusa/file.rb', line 51 def initialize super @loading = @loaded = false end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
10 11 12 |
# File 'lib/medusa/file.rb', line 10 def json @json end |
Class Method Details
.from_json(json) ⇒ Medusa::File
Initializes a new instance from a JSON fragment.
18 19 20 21 22 23 |
# File 'lib/medusa/file.rb', line 18 def self.from_json(json) file = Medusa::File.new file.json = json file.load file end |
.with_id(id) ⇒ Medusa::File
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/file.rb', line 32 def self.with_id(id) file = Medusa::File.new file.instance_variable_set('@id', id) file end |
.with_uuid(uuid) ⇒ Medusa::File
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/file.rb', line 45 def self.with_uuid(uuid) file = Medusa::File.new file.instance_variable_set('@uuid', uuid) file end |
Instance Method Details
#directory ⇒ Medusa::Directory
59 60 61 62 |
# File 'lib/medusa/file.rb', line 59 def directory load @directory ||= ::Medusa::Directory.with_id(@directory_id) end |
#load ⇒ Object
Updates the instance with current properties from Medusa.
It should not typically be necessary to use this method publicly.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/medusa/file.rb', line 101 def load return if @loading || @loaded @loading = true struct = json ? json : fetch_body @id = struct['id'] @md5_sum = struct['md5_sum'] @media_type = struct['content_type'] @mtime = Time.parse(struct['mtime']) if struct['mtime'] @relative_key = struct['relative_pathname'] @size = struct['size'] @uuid = struct['uuid'] # this will only be present in the /cfs_files/:id representation, not the # one nested in /cfs_directories if struct['directory'] and struct['directory']['uuid'] @directory_id = struct['directory']['id'] end @loaded = true ensure @loading = false end |
#md5_sum ⇒ String
67 68 69 70 |
# File 'lib/medusa/file.rb', line 67 def md5_sum load @md5_sum end |
#media_type ⇒ String
75 76 77 78 |
# File 'lib/medusa/file.rb', line 75 def media_type load @media_type end |
#mtime ⇒ Time
83 84 85 86 |
# File 'lib/medusa/file.rb', line 83 def mtime load @mtime end |
#size ⇒ Integer
91 92 93 94 |
# File 'lib/medusa/file.rb', line 91 def size load @size end |
#url ⇒ String
Returns Absolute URI of the corresponding Medusa resource.
125 126 127 128 129 |
# File 'lib/medusa/file.rb', line 125 def url [::Medusa::Client.configuration[:medusa_base_url].chomp('/'), 'cfs_files', self.id].join('/') end |