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

hash collections seem like a better domain mapping

This commit is contained in:
Wesley Beary 2009-08-29 11:20:54 -07:00
parent e32c247720
commit ae2298ee11
5 changed files with 59 additions and 37 deletions

View file

@ -17,6 +17,10 @@ module Fog
super super
end end
def buckets
@buckets
end
def delete def delete
connection.delete_bucket(name) connection.delete_bucket(name)
true true
@ -50,15 +54,12 @@ module Fog
options['LocationConstraint'] = @location options['LocationConstraint'] = @location
end end
connection.put_bucket(name, options) connection.put_bucket(name, options)
buckets[name] = self
true true
end end
private private
def buckets
@buckets
end
def buckets=(new_buckets) def buckets=(new_buckets)
@buckets = new_buckets @buckets = new_buckets
end end

View file

@ -8,13 +8,19 @@ module Fog
class Buckets < Fog::Collection class Buckets < Fog::Collection
def [](name)
self[name] ||= begin
get(name)
end
end
def all def all
data = connection.get_service.body data = connection.get_service.body
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection)) owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
buckets = Fog::AWS::S3::Buckets.new buckets = Fog::AWS::S3::Buckets.new
data['Buckets'].each do |bucket| data['Buckets'].each do |bucket|
buckets << Fog::AWS::S3::Bucket.new({ buckets << Fog::AWS::S3::Bucket.new({
:buckets => self :buckets => buckets,
:connection => connection, :connection => connection,
:owner => owner :owner => owner
}.merge!(bucket)) }.merge!(bucket))
@ -28,6 +34,40 @@ module Fog
bucket bucket
end end
def get(name, options = {})
remap_attributes(options, {
:is_truncated => 'IsTruncated',
:marker => 'Marker',
:max_keys => 'MaxKeys',
:prefix => 'Prefix'
})
data = connection.get_bucket(name, options).body
bucket = Fog::AWS::S3::Bucket.new({
:connection => connection,
:name => data['Name']
})
objects_data = {}
for key, value in data
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
objects_data[key] = value
end
end
objects = Fog::AWS::S3::Objects.new({
:bucket => bucket,
:connection => connection
}.merge!(objects_data))
data['Contents'].each do |object|
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
objects << Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self,
:owner => owner
}.merge!(object))
end
bucket
end
def new(attributes = {}) def new(attributes = {})
Fog::AWS::S3::Bucket.new( Fog::AWS::S3::Bucket.new(
attributes.merge!( attributes.merge!(

View file

@ -52,6 +52,7 @@ module Fog
def save(options = {}) def save(options = {})
data = connection.put_object(bucket.name, key, body, options) data = connection.put_object(bucket.name, key, body, options)
@etag = data.headers['ETag'] @etag = data.headers['ETag']
objects[key] = self
true true
end end

View file

@ -19,34 +19,14 @@ module Fog
super super
end end
def [](key)
self[key] ||= begin
get(key)
end
end
def all(options = {}) def all(options = {})
remap_attributes(options, { bucket.buckets.get(bucket.name, options).objects
:is_truncated => 'IsTruncated',
:marker => 'Marker',
:max_keys => 'MaxKeys',
:prefix => 'Prefix'
})
data = connection.get_bucket(bucket.name, options).body
objects_data = {}
for key, value in data
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
objects_data[key] = value
end
end
objects = Fog::AWS::S3::Objects.new({
:bucket => bucket,
:connection => connection
}.merge!(objects_data))
data['Contents'].each do |object|
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
objects << Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self,
:owner => owner
}.merge!(object))
end
objects
end end
def bucket def bucket
@ -59,7 +39,7 @@ module Fog
object object
end end
def get def get(key, options = {})
data = connection.get_object(bucket.name, key, options) data = connection.get_object(bucket.name, key, options)
object_data = { :body => data.body} object_data = { :body => data.body}
for key, value in data.headers for key, value in data.headers
@ -74,7 +54,7 @@ module Fog
}.merge!(object_data)) }.merge!(object_data))
end end
def head def head(key, options = {})
data = connection.head_object(bucket.name, key, options) data = connection.head_object(bucket.name, key, options)
object_data = {} object_data = {}
for key, value in data.headers for key, value in data.headers

View file

@ -1,5 +1,5 @@
module Fog module Fog
class Collection < Array class Collection < Hash
def initialize(attributes = {}) def initialize(attributes = {})
update_attributes(attributes) update_attributes(attributes)
@ -11,8 +11,8 @@ module Fog
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}" data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
end end
data << " [" data << " ["
for item in self for key, value in self
data << "#{item.inspect}," data << "#{value.inspect},"
end end
data.chomp! data.chomp!
data << "]>" data << "]>"