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

moving s3 models to more abstract collection reference

This commit is contained in:
Wesley Beary 2009-10-23 09:42:51 -07:00
parent 922d3a4248
commit e3e826ad69
6 changed files with 31 additions and 37 deletions

View file

@ -8,10 +8,6 @@ module Fog
attribute :name, 'Name' attribute :name, 'Name'
attribute :owner attribute :owner
def buckets
@buckets
end
def destroy def destroy
connection.delete_bucket(@name) connection.delete_bucket(@name)
true true
@ -24,6 +20,10 @@ module Fog
data.body['LocationConstraint'] data.body['LocationConstraint']
end end
def location=(new_location)
@location = new_location
end
def objects def objects
@objects ||= begin @objects ||= begin
Fog::AWS::S3::Objects.new( Fog::AWS::S3::Objects.new(
@ -44,7 +44,7 @@ module Fog
end end
def reload def reload
new_attributes = buckets.get(@name).attributes new_attributes = collection.get(@name).attributes
merge_attributes(new_attributes) merge_attributes(new_attributes)
end end
@ -57,12 +57,6 @@ module Fog
true true
end end
private
def buckets=(new_buckets)
@buckets = new_buckets
end
end end
end end

View file

@ -14,7 +14,7 @@ module Fog
buckets = Fog::AWS::S3::Buckets.new(:connection => connection) buckets = Fog::AWS::S3::Buckets.new(:connection => connection)
data['Buckets'].each do |bucket| data['Buckets'].each do |bucket|
buckets << Fog::AWS::S3::Bucket.new({ buckets << Fog::AWS::S3::Bucket.new({
:buckets => buckets, :collection => buckets,
:connection => connection, :connection => connection,
:owner => owner :owner => owner
}.merge!(bucket)) }.merge!(bucket))
@ -34,7 +34,7 @@ module Fog
}) })
data = connection.get_bucket(name, options).body data = connection.get_bucket(name, options).body
bucket = Fog::AWS::S3::Bucket.new({ bucket = Fog::AWS::S3::Bucket.new({
:buckets => self, :collection => self,
:connection => connection, :connection => connection,
:name => data['Name'] :name => data['Name']
}) })
@ -50,7 +50,7 @@ module Fog
bucket.objects << Fog::AWS::S3::Object.new({ bucket.objects << Fog::AWS::S3::Object.new({
:bucket => bucket, :bucket => bucket,
:connection => connection, :connection => connection,
:objects => self, :collection => bucket.objects,
:owner => owner :owner => owner
}.merge!(object)) }.merge!(object))
end end
@ -62,8 +62,8 @@ module Fog
def new(attributes = {}) def new(attributes = {})
Fog::AWS::S3::Bucket.new( Fog::AWS::S3::Bucket.new(
attributes.merge!( attributes.merge!(
:connection => connection, :collection => self,
:buckets => self :connection => connection
) )
) )
end end

View file

@ -37,12 +37,8 @@ module Fog
true true
end end
def objects
@objects
end
def reload def reload
new_attributes = objects.get(@key).attributes new_attributes = collection.get(@key).attributes
merge_attributes(new_attributes) merge_attributes(new_attributes)
end end
@ -58,10 +54,6 @@ module Fog
@bucket = new_bucket @bucket = new_bucket
end end
def objects=(new_objects)
@objects = new_objects
end
end end
end end

View file

@ -11,7 +11,7 @@ module Fog
attribute :prefix, 'Prefix' attribute :prefix, 'Prefix'
def all(options = {}) def all(options = {})
bucket.buckets.get( bucket.collection.get(
bucket.name, bucket.name,
options.reject {|key, value| !['delimiter', 'marker', 'max-keys', 'prefix'].include?(key)} options.reject {|key, value| !['delimiter', 'marker', 'max-keys', 'prefix'].include?(key)}
).objects ).objects
@ -40,8 +40,8 @@ module Fog
end end
object = Fog::AWS::S3::Object.new({ object = Fog::AWS::S3::Object.new({
:bucket => bucket, :bucket => bucket,
:connection => connection, :collection => self,
:objects => self :connection => connection
}.merge!(object_data)) }.merge!(object_data))
object object
rescue Fog::Errors::NotFound rescue Fog::Errors::NotFound
@ -64,8 +64,8 @@ module Fog
end end
object = Fog::AWS::S3::Object.new({ object = Fog::AWS::S3::Object.new({
:bucket => bucket, :bucket => bucket,
:connection => connection, :collection => self,
:objects => self :connection => connection
}.merge!(object_data)) }.merge!(object_data))
object object
rescue Fog::Errors::NotFound rescue Fog::Errors::NotFound
@ -75,8 +75,8 @@ module Fog
def new(attributes = {}) def new(attributes = {})
Fog::AWS::S3::Object.new({ Fog::AWS::S3::Object.new({
:bucket => bucket, :bucket => bucket,
:connection => connection, :collection => self,
:objects => self :connection => connection
}.merge!(attributes)) }.merge!(attributes))
end end

View file

@ -39,6 +39,10 @@ module Fog
attributes attributes
end end
def collection
@collection
end
def merge_attributes(new_attributes = {}) def merge_attributes(new_attributes = {})
for key, value in new_attributes for key, value in new_attributes
if aliased_key = self.class.aliases[key] if aliased_key = self.class.aliases[key]
@ -52,6 +56,10 @@ module Fog
private private
def collection=(new_collection)
@collection = new_collection
end
def connection=(new_connection) def connection=(new_connection)
@connection = new_connection @connection = new_connection
end end

View file

@ -16,15 +16,15 @@ describe 'Fog::AWS::S3::Bucket' do
end end
describe "#buckets" do describe "#collection" do
it "should return a Fog::AWS::S3::Buckets" do it "should return a Fog::AWS::S3::Buckets" do
s3.buckets.new.buckets.should be_a(Fog::AWS::S3::Buckets) s3.buckets.new.collection.should be_a(Fog::AWS::S3::Buckets)
end end
it "should be the buckets the bucket is related to" do it "should be the buckets the bucket is related to" do
buckets = s3.buckets buckets = s3.buckets
buckets.new.buckets.should == buckets buckets.new.collection.should == buckets
end end
end end
@ -115,12 +115,12 @@ describe 'Fog::AWS::S3::Bucket' do
end end
it "should not exist in buckets before save" do it "should not exist in buckets before save" do
@bucket.buckets.all.map {|bucket| bucket.name}.include?(@bucket.name).should be_false s3.buckets.all.map {|bucket| bucket.name}.include?(@bucket.name).should be_false
end end
it "should exist in buckets after save" do it "should exist in buckets after save" do
@bucket.save @bucket.save
@bucket.buckets.all.map {|bucket| bucket.name}.include?(@bucket.name).should be_true s3.buckets.all.map {|bucket| bucket.name}.include?(@bucket.name).should be_true
@bucket.destroy @bucket.destroy
end end