1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/rackspace/models/storage/metadata.rb

68 lines
1.8 KiB
Ruby
Raw Normal View History

module Fog
module Storage
class Rackspace
class Metadata
# META_PREFIX = "X-Object-Meta-"
# REMOVE_META_PREFIX = "X-Remove-Object-Meta-"
META_PREFIX = "X-Container-Meta-"
REMOVE_META_PREFIX = "X-Remove-Container-Meta-"
KEY_REGEX = /^#{META_PREFIX}(.*)/
attr_reader :data
def initialize(hash={})
@data = hash || {}
end
def to_headers
headers = {}
@data.each_pair do |k,v|
key = to_header_key(k,v)
headers[key] = v
end
headers
end
def self.from_headers(headers)
metadata = Metadata.new
headers.each_pair do |k, v|
key = Metadata.to_key(k)
next unless key
metadata.data[key] = v
end
metadata
end
def respond_to?(method_sym, include_private = false)
super(method_sym, include_private) || @data.method_missing(method_missing, include_private)
end
def method_missing(method, *args, &block)
@data.send(method, *args, &block)
end
private
def self.to_key(key)
m = key.match KEY_REGEX
return nil unless m && m[1]
a = m[1].split('-')
a.collect!(&:downcase)
str = a.join('_')
str.to_sym
end
def to_header_key(key, value)
prefix = value.nil? ? REMOVE_META_PREFIX : META_PREFIX
prefix + key.to_s.split(/[-_]/).collect(&:capitalize).join('-')
end
end
end
end
end