1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

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
This commit is contained in:
Evan Smith 2012-11-02 16:24:12 -07:00
parent 86322ce4bf
commit 449b227f56
2 changed files with 94 additions and 2 deletions

View file

@ -12,6 +12,7 @@ module Fog
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 @@ module Fog
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 @@ module Fog
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 @@ module Fog
@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

View file

@ -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