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) ⇒ Object



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

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) ⇒ Object



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

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
    data.value = value
  else
    self << Fog::Compute::RackspaceV2::Metadatum.new(:key => key, :value => value, :connection => connection, :parent => parent)
  end
  value
end

#allObject



17
18
19
20
21
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 17

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

#from_hash(hash) ⇒ Object



61
62
63
64
65
66
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 61

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

#get(key) ⇒ Object



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

def get(key)
  requires :parent
  data = connection.(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



56
57
58
59
# File 'lib/fog/rackspace/models/compute_v2/metadata.rb', line 56

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

#saveObject



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

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

#to_hashObject



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

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