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)


51
52
53
54
55
56
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 51

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)

Parameters:

  • key (#key)

Returns:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 63

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

#allFog::Compute::RackspaceV2::Metadatum

Retrieves all of the metadata from server



23
24
25
26
27
28
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 23

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.



93
94
95
96
97
98
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 93

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



37
38
39
40
41
42
43
44
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 37

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



86
87
88
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 86

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

#saveObject

Saves the current metadata on server



80
81
82
83
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 80

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



102
103
104
105
106
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 102

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