Browse files

Rackspace Cloud Files. can load metadata from existing file. can set …

…metadata for new file. can unset metadata for existing file.

refactored rackspace/models/storage/file_tests.rb

Storage::Rackspace::File renamed some metadata key mapping methods. added more key mapping tests.

reorganized tests
  • Loading branch information...
1 parent 5770589 commit b65167e73e9d9d9029455d142a95e3a57eebe893 @pastorius pastorius committed Nov 15, 2012
Showing with 121 additions and 47 deletions.
  1. +47 −17 lib/fog/rackspace/models/storage/file.rb
  2. +74 −30 tests/rackspace/models/storage/file_tests.rb
View
64 lib/fog/rackspace/models/storage/file.rb
@@ -12,7 +12,6 @@ class File < Fog::Model
attribute :content_type, :aliases => ['content_type', 'Content-Type']
attribute :etag, :aliases => ['hash', 'Etag']
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
- attribute :metadata
def body
attributes[:body] ||= if last_modified
@@ -44,14 +43,8 @@ def destroy
true
end
- remove_method :metadata
def metadata
- attributes.reject {|key, value| !metadata_attribute?(key)}
- end
-
- remove_method :metadata=
- def metadata=(new_metadata)
- merge_attributes(new_metadata || {})
+ @metadata ||= headers_to_metadata
end
def owner=(new_owner)
@@ -75,9 +68,10 @@ def public_url
def save(options = {})
requires :body, :directory, :key
options['Content-Type'] = content_type if content_type
- options.merge!(metadata)
+ options.merge!(metadata_to_headers)
data = connection.put_object(directory.key, key, body, options)
- refresh_attributes(data)
+ update_attributes_from(data)
+ refresh_metadata
self.content_length = Fog::Storage.get_body_size(body)
self.content_type ||= Fog::Storage.get_content_type(body)
true
@@ -89,17 +83,53 @@ def directory=(new_directory)
@directory = new_directory
end
- def metadata_attribute?(key)
- key.to_s =~ /^X-Object-Meta-/
+ def refresh_metadata
+ metadata.reject! {|k, v| v.nil? }
end
- def refresh_attributes(data)
- update_attributes_from(data)
- remove_empty_metadata_attributes
+ def headers_to_metadata
+ key_map = key_mapping
+ Hash[metadata_attributes.map {|k, v| [key_map[k], v] }]
+ end
+
+ def key_mapping
+ key_map = metadata_attributes
+ key_map.each_pair {|k, v| key_map[k] = header_to_key(k)}
+ end
+
+ def header_to_key(opt)
+ opt.gsub(metadata_prefix, '').split('-').map {|k| k[0, 1].downcase + k[1..-1]}.join('_').to_sym
+ end
+
+ def metadata_to_headers
+ header_map = header_mapping
+ Hash[metadata.map {|k, v| [header_map[k], v] }]
+ end
+
+ def header_mapping
+ header_map = metadata.dup
+ header_map.each_pair {|k, v| header_map[k] = key_to_header(k)}
+ end
+
+ def key_to_header(key)
+ metadata_prefix + key.to_s.split(/[-_]/).map(&:capitalize).join('-')
+ end
+
+ def metadata_attributes
+ if etag
+ headers = connection.head_object(directory.key, self.key).headers
+ headers.reject! {|k, v| !metadata_attribute?(k)}
+ else
+ {}
+ end
+ end
+
+ def metadata_attribute?(key)
+ key.to_s =~ /^#{metadata_prefix}/
end
- def remove_empty_metadata_attributes
- attributes.delete_if {|key, value| metadata_attribute?(key) && value.nil?}
+ def metadata_prefix
+ "X-Object-Meta-"
end
def update_attributes_from(data)
View
104 tests/rackspace/models/storage/file_tests.rb
@@ -1,5 +1,15 @@
Shindo.tests('Fog::Rackspace::Storage | file', ['rackspace']) do
+ def object_meta_attributes
+ @instance.connection.head_object(@directory.key, @instance.key).headers.reject {|k, v| !(k =~ /X-Object-Meta-/)}
+ end
+
+ def clear_metadata
+ @instance.metadata.tap do |metadata|
+ metadata.each_pair {|k, v| metadata[k] = nil }
+ end
+ end
+
file_attributes = {
:key => 'fog_file_tests',
:body => lorem_file
@@ -16,47 +26,81 @@
model_tests(@directory.files, file_attributes, Fog.mocking?) do
- tests("#metadata") do
-
- tests("#metadata should default to empty").returns({}) do
+ tests("#metadata should load empty metadata").returns({}) do
@instance.metadata
end
- @instance.attributes['X-Object-Meta-Fog-Foo'] = 'foo'
- tests("#metadata should return only metadata attributes").returns({'X-Object-Meta-Fog-Foo' => 'foo'}) do
- @instance.metadata
- end
- @instance.attributes.delete('X-Object-Meta-Fog-Foo')
+ tests('#save') do
- @instance.attributes['X-Object-Meta-Fog-Foo'] = 'foo'
- tests("#metadata= should update metadata").returns('bar') do
- @instance.metadata = {'X-Object-Meta-Fog-Foo' => 'bar'}
- @instance.attributes['X-Object-Meta-Fog-Foo']
- end
-
- tests("#metadata= should not blow up on nil") do
- @instance.metadata = nil
- end
+ tests('#metadata') do
- end
+ before do
+ @instance.metadata[:foo] = 'bar'
+ @instance.save
+ end
- end
+ after do
+ clear_metadata
+ @instance.save
+ end
- model_tests(@directory.files, file_attributes.merge('X-Object-Meta-Fog-Foo' => 'foo'), Fog.mocking?) do
+ tests("should update metadata").returns('bar') do
+ object_meta_attributes['X-Object-Meta-Foo']
+ end
- tests("#save") do
+ tests('should cache metadata').returns('bar') do
+ @instance.metadata[:foo]
+ end
- tests("#save should merge metadata").returns('foo') do
- @instance.metadata['X-Object-Meta-Fog-Foo']
- end
+ tests('should remove empty metadata').returns({}) do
+ @instance.metadata[:foo] = nil
+ @instance.save
+ object_meta_attributes
+ end
- @instance.attributes['X-Object-Meta-Fog-Foo'] = nil
- tests("#save should remove empty metadata").returns(false) do
- @instance.save
- @instance.attributes.has_key?('X-Object-Meta-Fog-Foo')
- end
+ end
+
+ tests('#metadata keys') do
+
+ after do
+ clear_metadata
+ @instance.save
+ end
+
+ @instance.metadata[:foo_bar] = 'baz'
+ tests("should support compound key names").returns('baz') do
+ @instance.save
+ object_meta_attributes['X-Object-Meta-Foo-Bar']
+ end
+
+ @instance.metadata['foo'] = 'bar'
+ tests("should support string keys").returns('bar') do
+ @instance.save
+ object_meta_attributes['X-Object-Meta-Foo']
+ end
+
+ @instance.metadata['foo_bar'] = 'baz'
+ tests("should support compound string key names").returns('baz') do
+ @instance.save
+ object_meta_attributes['X-Object-Meta-Foo-Bar']
+ end
+
+ @instance.metadata['foo-bar'] = 'baz'
+ tests("should support hyphenated keys").returns('baz') do
+ @instance.save
+ object_meta_attributes['X-Object-Meta-Foo-Bar']
+ end
+
+ @instance.metadata['foo-bar'] = 'baz'
+ @instance.metadata[:'foo_bar'] = 'bref'
+ tests("should only support one value per metadata key").returns('bref') do
+ @instance.save
+ object_meta_attributes['X-Object-Meta-Foo-Bar']
+ end
+
+ end
- end
+ end
end

0 comments on commit b65167e

Please sign in to comment.