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

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

Instance Method Summary collapse

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)

Parameters:

  • key (#key)

Returns:

  • (#value)


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)

Parameters:

  • key (#key)

Returns:



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

#allFog::Compute::RackspaceV2::Metadatum

Retrieves all of the metadata from server

Returns:

Raises:



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

Note:

This will remove existing data

Resets metadata using data from hash

Parameters:

  • hash

    hash containing key value pairs used to populate metadata.



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

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:

Raises:



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

#saveObject

Saves the current metadata on server

Raises:



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_hashHash

Converts metadata object to hash

Returns:

  • (Hash)

    hash of metadata key value pairs



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