diff --git a/lib/fog/aws/models/s3/bucket.rb b/lib/fog/aws/models/s3/bucket.rb index 7bab5cfd0..f3228210d 100644 --- a/lib/fog/aws/models/s3/bucket.rb +++ b/lib/fog/aws/models/s3/bucket.rb @@ -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 diff --git a/lib/fog/aws/models/s3/object.rb b/lib/fog/aws/models/s3/object.rb index ca1fd83d2..73fdb197b 100644 --- a/lib/fog/aws/models/s3/object.rb +++ b/lib/fog/aws/models/s3/object.rb @@ -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) diff --git a/lib/fog/aws/models/s3/objects.rb b/lib/fog/aws/models/s3/objects.rb index f5f7ae35b..aa72a6189 100644 --- a/lib/fog/aws/models/s3/objects.rb +++ b/lib/fog/aws/models/s3/objects.rb @@ -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 diff --git a/lib/fog/collection.rb b/lib/fog/collection.rb index e53c50c81..3e05ad3a7 100644 --- a/lib/fog/collection.rb +++ b/lib/fog/collection.rb @@ -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) diff --git a/lib/fog/model.rb b/lib/fog/model.rb index 2b3ea3833..15915003f 100644 --- a/lib/fog/model.rb +++ b/lib/fog/model.rb @@ -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