Class: Fog::Compute::RackspaceV2::Metadata
- Inherits:
-
Fog::Collection
- Object
- Fog::Collection
- Fog::Compute::RackspaceV2::Metadata
- Includes:
- MetaParent
- Defined in:
- lib/fog/rackspace/models/compute_v2/metadata.rb
Instance Method Summary collapse
-
#[](key) ⇒ #value
Retrieve specific value for key from Metadata.
-
#[]=(key, value) ⇒ String
Set value for key.
-
#all ⇒ Fog::Compute::RackspaceV2::Metadatum
Retrieves all of the metadata from server.
-
#from_hash(hash) ⇒ Object
Resets metadata using data from hash.
-
#get(key) ⇒ Fog::Compute::RackspaceV2::Metadatum
Retrieves specific metadata from server.
-
#new(attributes = {}) ⇒ Object
Creates new metadata.
-
#save ⇒ Object
Saves the current metadata on server.
-
#to_hash ⇒ Hash
Converts metadata object to hash.
Methods included from MetaParent
#collection_name, #metas_to_hash, #parent, #parent=
Instance Method Details
#[](key) ⇒ #value
Retrieve specific value for key from Metadata.
-
If key is of type String, this method will return the value of the metadatum
-
If key is of type Fixnum, this method will return a Fog::Compute::RackspaceV2::Metadatum object (legacy)
49 50 51 52 53 54 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 49 def [](key) return super(key) if key.is_a?(Integer) return nil unless key datum = self.find {|datum| datum.key == key || datum.key == key.to_sym } datum ? datum.value : nil end |
#[]=(key, value) ⇒ String
Set value for key.
-
If key is of type String, this method will set/add the value to Metadata
-
If key is of type Fixnum, this method will set a Fog::Compute::RackspaceV2::Metadatum object (legacy)
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 61 def []=(key, value) return super(key,value) if key.is_a?(Integer) return nil unless key datum = self.find {|datum| datum.key == key || datum.key == key.to_sym } if datum datum.value = value else self << Fog::Compute::RackspaceV2::Metadatum.new(:key => key, :value => value, :service => service, :parent => parent) end value end |
#all ⇒ Fog::Compute::RackspaceV2::Metadatum
Retrieves all of the metadata from server
21 22 23 24 25 26 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 21 def all requires :parent return unless parent.identity data = service.(collection_name, parent.id).body['metadata'] from_hash(data) end |
#from_hash(hash) ⇒ Object
This will remove existing data
Resets metadata using data from hash
91 92 93 94 95 96 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 91 def from_hash(hash) return unless hash = [] hash.each_pair {|k,v| << {:key => k, :value => v} } load() end |
#get(key) ⇒ Fog::Compute::RackspaceV2::Metadatum
Retrieves specific metadata from server
35 36 37 38 39 40 41 42 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 35 def get(key) requires :parent data = service.(collection_name, parent.id, key).body["meta"] datum = data.first new(:key => datum[0], :value => datum[1]) rescue Fog::Compute::RackspaceV2::NotFound nil end |
#new(attributes = {}) ⇒ Object
Creates new metadata
84 85 86 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 84 def new(attributes = {}) super({ :parent => parent }.merge!(attributes)) end |
#save ⇒ Object
Saves the current metadata on server
78 79 80 81 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 78 def save requires :parent service.(collection_name, parent.id, to_hash) end |
#to_hash ⇒ Hash
Converts metadata object to hash
100 101 102 103 104 |
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 100 def to_hash h = {} self.each { |datum| h[datum.key] = datum.value } h end |