Class: Fog::Compute::RackspaceV2::Metadata

Inherits:
Fog::Collection show all
Includes:
MetaParent
Defined in:
lib/fog/rackspace/models/compute_v2/metadata.rb

Instance Attribute Summary

Attributes inherited from Fog::Collection

#service

Instance Method Summary collapse

Methods included from MetaParent

#collection_name, #metas_to_hash, #parent, #parent=

Methods inherited from Fog::Collection

#clear, #create, #destroy, #initialize, #inspect, #load, model, #model, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Collection

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)

Parameters:

  • key (#key)

Returns:

  • (#value)


43
44
45
46
47
48
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 43

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 
* If key is of type Fixnum, this method will set a Fog::Compute::RackspaceV2::Metadatum object (legacy)

Parameters:

  • key (#key)

Returns:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 55

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

#allArray<Fog::Compute::RackspaceV2::Metadatum>

Retrieves all of the metadata from server

Returns:



19
20
21
22
23
24
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 19

def all
  requires :parent
  return unless parent.identity
  data = service.(collection_name, parent.id).body['metadata']
  from_hash(data)
end

#from_hash(hash) ⇒ Object

Note:

This will remove existing data

Resets metadata using data from hash

Parameters:

  • hash

    hash containing key value pairs used to populate metadata.



82
83
84
85
86
87
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 82

def from_hash(hash)
  return unless hash
  metas = []
  hash.each_pair {|k,v| metas << {:key => k, :value => v} }
  load(metas)
end

#get(key) ⇒ Fog::Compute::RackspaceV2::Metadatum

Retrieves specific metadata from server

Parameters:

  • key (String)

    for metadatum

Returns:



29
30
31
32
33
34
35
36
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 29

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



74
75
76
77
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 74

def new(attributes = {})
  requires :parent
  super({ :parent => parent }.merge!(attributes))
end

#saveObject

Saves the current metadata on server



68
69
70
71
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 68

def save
  requires :parent
  service.(collection_name, parent.id, to_hash)          
end

#to_hashHash

Converts metadata object to hash

Returns:

  • (Hash)

    hash of metadata key value pairs



91
92
93
94
95
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 91

def to_hash
  h = {}
  self.each { |datum| h[datum.key] = datum.value }
  h
end