Class: Fog::Storage::Rackspace::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/models/storage/metadata.rb

Constant Summary collapse

OBJECT_META_PREFIX =
"X-Object-Meta-"
OBJECT_REMOVE_META_PREFIX =
"X-Remove-Object-Meta-"
CONTAINER_META_PREFIX =
"X-Container-Meta-"
CONTAINER_REMOVE_META_PREFIX =
"X-Remove-Container-Meta-"
DUMMY_VALUE =

Cloud Files will ignore headers without a value

1
CONTAINER_KEY_REGEX =
/^#{CONTAINER_META_PREFIX}(.*)/
OBJECT_KEY_REGEX =
/^#{OBJECT_META_PREFIX}(.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, hash = {}) ⇒ Metadata

Initialize

Parameters:



27
28
29
30
31
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 27

def initialize(parent, hash={})
  @data = hash || {}
  @deleted_hash = {}
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Invoked by Ruby when obj is sent a message it cannot handle.



100
101
102
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 100

def method_missing(method, *args, &block)
  data.send(method, *args, &block)
end

Instance Attribute Details

#dataHash

Returns underlying data store for metadata class.

Returns:

  • (Hash)

    underlying data store for metadata class



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

def data
  @data
end

#parentFog::Storage::Rackspace::Directory, Fog::Storage::Rackspace::File

Returns the parent object of the metadata.

Returns:



22
23
24
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 22

def parent
  @parent
end

Class Method Details

.from_headers(parent, headers) ⇒ Object

Creates metadata object from Cloud File Headers

Parameters:



82
83
84
85
86
87
88
89
90
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 82

def self.from_headers(parent, headers)
   = Metadata.new(parent)
  headers.each_pair do |k, v|
    key = .send(:to_key, k)
    next unless key
    .data[key] = v
  end
  
end

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

Parameters:

  • key (#key)

Returns:

  • (#value)


59
60
61
62
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 59

def [](key)
  return nil unless key
  @data[key.to_s] || @data[key.to_sym]
end

#[]=(key, value) ⇒ String

Set value for key.

  • If key is of type String, this method will set/add the value to Metadata

Parameters:

  • key (#key)

Returns:



68
69
70
71
72
73
74
75
76
77
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 68

def []=(key, value)
  return nil unless key
  if @data[key.to_s]
    @data[key.to_s] = value
  elsif @data[key.to_sym]
    @data[key.to_sym] = value
  else
    @data[key] = value
  end
end

#delete(key) ⇒ Object

Note:

Metadata must be deleted using this method in order to properly remove it from Cloud Files

Delete key value pair from metadata

Parameters:

  • key (String)

    to be deleted

Returns:

  • (Object)

    returns value for key



37
38
39
40
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 37

def delete(key)
  data.delete(key)
  @deleted_hash[key] = nil
end

#respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns true if method is implemented by Metadata class

Parameters:

  • method_sym (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


95
96
97
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 95

def respond_to?(method_sym, include_private = false)
  super(method_sym, include_private) || data.respond_to?(method_sym, include_private)
end

#to_headersHash

Returns metadata in a format expected by Cloud Files

Returns:

  • (Hash)

    Metadata in a format expected by Cloud Files



44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/rackspace/models/storage/metadata.rb', line 44

def to_headers
  headers = {}
  h = data.merge(@deleted_hash)
  h.each_pair do |k,v|
    key = to_header_key(k,v)
    headers[key] = v || DUMMY_VALUE
  end

  headers
end