fog--fog/lib/fog/core/model.rb

59 lines
1.4 KiB
Ruby
Raw Normal View History

2009-08-02 23:37:54 +00:00
module Fog
class Model
2010-06-08 01:22:31 +00:00
extend Fog::Attributes::ClassMethods
include Fog::Attributes::InstanceMethods
attr_accessor :connection
def collection
@collection
end
2009-08-30 21:14:11 +00:00
def initialize(new_attributes = {})
merge_attributes(new_attributes)
2009-08-02 23:37:54 +00:00
end
def inspect
Thread.current[:formatador] ||= Formatador.new
data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
Thread.current[:formatador].indent do
unless self.class.attributes.empty?
data << "\n#{Thread.current[:formatador].indentation}"
data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
end
2009-08-02 23:37:54 +00:00
end
data << "\n#{Thread.current[:formatador].indentation}>"
data
2009-08-02 23:37:54 +00:00
end
def reload
2010-10-27 23:09:00 +00:00
requires :identity
if data = collection.get(identity)
new_attributes = data.attributes
merge_attributes(new_attributes)
self
end
end
def to_json
attributes.to_json
end
2010-07-22 02:18:34 +00:00
def wait_for(timeout=600, interval=1, &block)
reload
2010-07-22 02:18:34 +00:00
Fog.wait_for(timeout, interval) do
2010-09-16 22:41:19 +00:00
reload or raise Fog::Errors::Error.new("Reload failed, #{self.class} #{self.identity} went away.")
instance_eval(&block)
end
end
2009-08-02 23:37:54 +00:00
private
def collection=(new_collection)
@collection = new_collection
end
2009-08-02 23:37:54 +00:00
end
end