2009-08-02 16:37:54 -07:00
|
|
|
module Fog
|
|
|
|
class Model
|
|
|
|
|
2010-06-07 18:22:31 -07:00
|
|
|
extend Fog::Attributes::ClassMethods
|
|
|
|
include Fog::Attributes::InstanceMethods
|
2009-11-21 13:46:46 -08:00
|
|
|
|
2009-12-05 14:53:42 -08:00
|
|
|
attr_accessor :connection
|
|
|
|
|
2009-10-29 23:35:28 -07:00
|
|
|
def collection
|
|
|
|
@collection
|
|
|
|
end
|
|
|
|
|
2009-08-30 14:14:11 -07:00
|
|
|
def initialize(new_attributes = {})
|
|
|
|
merge_attributes(new_attributes)
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2010-02-14 14:34:33 -08:00
|
|
|
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 16:37:54 -07:00
|
|
|
end
|
2010-02-15 20:18:52 -08:00
|
|
|
data << "\n#{Thread.current[:formatador].indentation}>"
|
2010-02-14 14:34:33 -08:00
|
|
|
data
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
|
2009-10-23 10:27:23 -07:00
|
|
|
def reload
|
2010-04-27 16:34:04 -07:00
|
|
|
if data = collection.get(identity)
|
|
|
|
new_attributes = data.attributes
|
|
|
|
merge_attributes(new_attributes)
|
2010-05-06 16:41:02 -07:00
|
|
|
self
|
2010-04-27 16:34:04 -07:00
|
|
|
end
|
2009-10-23 10:27:23 -07:00
|
|
|
end
|
|
|
|
|
2010-03-27 18:06:32 -07:00
|
|
|
def to_json
|
|
|
|
attributes.to_json
|
|
|
|
end
|
|
|
|
|
2009-12-07 22:23:55 -08:00
|
|
|
def wait_for(timeout = 600, &block)
|
2010-05-01 15:08:44 -07:00
|
|
|
reload
|
2010-05-07 09:51:58 -07:00
|
|
|
Fog.wait_for(timeout) do
|
|
|
|
reload or raise StandardError.new("Reload failed, #{self.class} #{self.identity} went away.")
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
2009-12-07 22:23:55 -08:00
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
private
|
|
|
|
|
2009-10-23 09:42:51 -07:00
|
|
|
def collection=(new_collection)
|
|
|
|
@collection = new_collection
|
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
end
|