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

cleanup attribute definition/usage in collections/models

This commit is contained in:
Wesley Beary 2009-08-30 14:43:01 -07:00
parent 19f3408ec7
commit 37f7339df8
5 changed files with 83 additions and 68 deletions

View file

@ -4,16 +4,12 @@ module Fog
class Bucket < Fog::Model
attr_accessor :creation_date,
:location,
:name,
:owner
attribute :creation_date, 'CreationDate'
attribute :location
attribute :name, 'Name'
attribute :owner
def initialize(attributes = {})
remap_attributes(attributes, {
'CreationDate' => :creation_date,
'Name' => :name
})
super
end

View file

@ -4,26 +4,16 @@ module Fog
class Object < Fog::Model
attr_accessor :body,
:content_length,
:content_type,
:etag,
:key,
:last_modified,
:owner,
:size,
:storage_class
attribute :body
attribute :content_length, 'Content-Length'
attribute :content_type, 'Content-Type'
attribute :etag, 'Etag'
attribute :key, 'Key'
attribute :last_modified, ['Last-Modified', 'LastModified']
attribute :size, 'Size'
attribute :storage_class, 'StorageClass'
def initialize(attributes = {})
remap_attributes(attributes, {
'Content-Length' => :content_length,
'ETag' => :etag,
'Key' => :key,
'LastModified' => :last_modified,
'Last-Modified' => :last_modified,
'Size' => :size,
'StorageClass' => :storage_class
})
super
end
@ -34,17 +24,7 @@ module Fog
def copy(target_bucket_name, target_object_key)
data = connection.copy_object(bucket.name, key, target_bucket_name, target_object_key).body
target_bucket = connection.buckets.new(:name => target_bucket_name)
target_object = target_bucket.objects.new(
:body => body,
:content_length => content_length,
:content_type => content_type,
:etag => etag,
:key => key,
:last_modified => last_modified,
:owner => owner,
:size => size,
:storage_class => storage_class
)
target_object = target_bucket.objects.new(attributes)
copy_data = {}
for key, value in data
if ['ETag', 'LastModified'].include?(key)

View file

@ -4,18 +4,12 @@ module Fog
class Objects < Fog::Collection
attr_accessor :is_truncated,
:marker,
:max_keys,
:prefix
attribute :is_truncated, 'IsTruncated'
attribute :marker, 'Marker'
attribute :max_keys, 'MaxKeys'
attribute :prefix, 'Prefix'
def initialize(attributes = {})
remap_attributes(attributes, {
'IsTruncated' => :is_truncated,
'Marker' => :marker,
'MaxKeys' => :max_keys,
'Prefix' => :prefix
})
super
end
@ -26,19 +20,8 @@ module Fog
end
def all(options = {})
options = {
:is_truncated => is_trucated,
:marker => marker,
:max_keys => max_keys,
:prefix => prefix
}.merge!(options)
remap_attributes(options, {
:is_truncated => 'IsTruncated',
:marker => 'Marker',
:max_keys => 'MaxKeys',
:prefix => 'Prefix'
})
bucket.buckets.get(bucket.name, options).objects
merge_attributes(options)
bucket.buckets.get(bucket.name, attributes).objects
end
def bucket

View file

@ -1,6 +1,24 @@
module Fog
class Collection < Hash
def self.attribute(name, other_names = [])
class_eval <<-EOS, __FILE__, __LINE__
attr_accessor :#{name}
EOS
attributes << name
for other_name in [*other_names]
aliases[other_name] = name
end
end
def self.aliases
@aliases ||= {}
end
def self.attributes
@attributes ||= []
end
def initialize(attributes = {})
update_attributes(attributes)
end
@ -18,9 +36,21 @@ module Fog
data << "]>"
end
def update_attributes(attributes = {})
for key, value in attributes
send(:"#{key}=", value)
def attributes
attributes = {}
for attribute in self.attributes
attributes[attribute] = send(:"#{attribute}")
end
attributes
end
def merge_attributes(new_attributes = {})
for key, value in new_attributes
if aliased_key = self.aliases[key]
send(:"#{aliased_key}=", value)
else
send(:"#{key}=", value)
end
end
self
end
@ -35,10 +65,6 @@ module Fog
@connection
end
def new_record?
!defined?(@new_record) || @new_record
end
def remap_attributes(attributes, mapping)
for key, value in mapping
if attributes.key?(key)

View file

@ -1,10 +1,36 @@
module Fog
class Model
def self.attribute(name, other_names = [])
class_eval <<-EOS, __FILE__, __LINE__
attr_accessor :#{name}
EOS
attributes << name
for other_name in [*other_names]
aliases[other_name] = name
end
end
def self.aliases
@aliases ||= {}
end
def self.attributes
@attributes ||= []
end
def initialize(new_attributes = {})
merge_attributes(new_attributes)
end
def attributes
attributes = {}
for attribute in self.attributes
attributes[attribute] = send(:"#{attribute}")
end
attributes
end
def inspect
data = "#<#{self.class.name}"
for attribute in (self.instance_variables - ['@connection'])
@ -15,7 +41,11 @@ module Fog
def merge_attributes(new_attributes = {})
for key, value in new_attributes
send(:"#{key}=", value)
if aliased_key = self.aliases[key]
send(:"#{aliased_key}=", value)
else
send(:"#{key}=", value)
end
end
self
end