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