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

another pass at cleaning/normalizing models

This commit is contained in:
Wesley Beary 2009-08-05 22:55:09 -07:00
parent e424864bd2
commit b7dd4eae7d
4 changed files with 67 additions and 54 deletions

View file

@ -4,7 +4,7 @@ module Fog
class Bucket < Fog::Model
attr_accessor :creation_date, :location, :name, :owner
attr_accessor :creation_date, :location, :name, :objects, :owner
def initialize(attributes = {})
remap_attributes(attributes, {
@ -12,7 +12,10 @@ module Fog
'Name' => :name
})
super
@objects ||= []
@objects ||= Fog::AWS::S3::Objects.new({
:bucket => bucket,
:connection => connection
})
end
def delete
@ -29,29 +32,6 @@ module Fog
end
end
def objects
data = connection.get_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))
bucket.objects << Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:owner => owner
}.merge!(object))
end
objects
end
def payer
@payer ||= begin
data = connection.get_request_payment(name)

View file

@ -19,6 +19,14 @@ module Fog
super
end
def body
@body ||= get.body
end
def content_length
@content_length ||= head.content_length
end
def copy(target_bucket_name, target_object_key)
data = connection.copy_object(bucket, key, target_bucket_name, target_object_key).body
copy = self.dup
@ -39,6 +47,14 @@ module Fog
true
end
def etag
@etag ||= head.etag
end
def last_modified
@last_modified ||= head.last_modified
end
def save(options = {})
data = connection.put_object(bucket, key, body, options)
@etag = data.headers['ETag']
@ -48,6 +64,28 @@ module Fog
private
def get
data = connection.get_object(bucket.name, key, options)
object_data = { :body => data.body}
for key, value in data.headers
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
update_attributes(object_data)
end
def head
data = connection.head_object(bucket.name, key, options)
object_data = {}
for key, value in data.headers
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
update_attributes(object_data)
end
def bucket=(new_bucket)
@bucket = new_bucket
end

View file

@ -16,41 +16,35 @@ module Fog
super
end
def all
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))
bucket.objects << Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:owner => owner
}.merge!(object))
end
objects
end
def create(attributes = {})
object = new(attributes)
object.save
object
end
def get(key, options = {})
data = connection.get_object(bucket.name, key, options)
object_data = {}
for key, value in data.headers
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
object = Fog::AWS::S3::Object.new({
:bucket => bucket,
:body => data.body,
:connection => connection
}.merge!(object_data))
end
def head(key, options = {})
data = connection.head_object(bucket.name, key, options)
object_data = {}
for key, value in data.headers
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
object = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection
}.merge!(object_data))
end
def new(attributes = {})
Fog::AWS::S3::Object.new(attributes.merge!(:connection => connection))
end

View file

@ -17,6 +17,7 @@ module Fog
for key, value in attributes
send(:"#{key}=", value)
end
self
end
private