Permalink
Browse files

added support for Storage::Rackspace::File#metadata

Added rackspace/models/storage/file_tests.
Added tests for Fog::Storage::Rackspace::File#metadata
Added Fog::Rackspace::Storage::File#metadata
  • Loading branch information...
1 parent 86322ce commit 449b227f56dbac1cbcc5b82bc263a0984e70864f @pastorius pastorius committed Nov 2, 2012
Showing with 94 additions and 2 deletions.
  1. +30 −2 lib/fog/rackspace/models/storage/file.rb
  2. +64 −0 tests/rackspace/models/storage/file_tests.rb
@@ -12,6 +12,7 @@ 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
@@ -43,6 +44,16 @@ 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 || {})
+ end
+
def owner=(new_owner)
if new_owner
attributes[:owner] = {
@@ -64,8 +75,9 @@ def public_url
def save(options = {})
requires :body, :directory, :key
options['Content-Type'] = content_type if content_type
- data = connection.put_object(directory.key, key, body, options)
- merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
+ options.merge!(metadata)
+ data = connection.put_object(directory.key, key, body, options)
+ refresh_attributes(data)
self.content_length = Fog::Storage.get_body_size(body)
self.content_type ||= Fog::Storage.get_content_type(body)
true
@@ -77,6 +89,22 @@ def directory=(new_directory)
@directory = new_directory
end
+ def metadata_attribute?(key)
+ key.to_s =~ /^X-Object-Meta-/
+ end
+
+ def refresh_attributes(data)
+ update_attributes_from(data)
+ remove_empty_metadata_attributes
+ end
+
+ def remove_empty_metadata_attributes
+ attributes.delete_if {|key, value| metadata_attribute?(key) && value.nil?}
+ end
+
+ def update_attributes_from(data)
+ merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
+ end
end
end
@@ -0,0 +1,64 @@
+Shindo.tests('Fog::Rackspace::Storage | file', ['rackspace']) do
+
+ file_attributes = {
+ :key => 'fog_file_tests',
+ :body => lorem_file
+ }
+
+ directory_attributes = {
+ # Add a random suffix to prevent collision
+ :key => "fogfilestests-#{rand(65536)}"
+ }
+
+ @directory = Fog::Storage[:rackspace].
+ directories.
+ create(directory_attributes)
+
+ model_tests(@directory.files, file_attributes, Fog.mocking?) do
+
+ tests("#metadata") do
+
+ tests("#metadata should default to empty").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')
+
+ @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
+
+ end
+
+ end
+
+ model_tests(@directory.files, file_attributes.merge('X-Object-Meta-Fog-Foo' => 'foo'), Fog.mocking?) do
+
+ tests("#save") do
+
+ tests("#save should merge metadata").returns('foo') do
+ @instance.metadata['X-Object-Meta-Fog-Foo']
+ 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
+
+ end
+
+ @directory.destroy
+end

0 comments on commit 449b227

Please sign in to comment.