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

57 lines
1.4 KiB
Ruby
Raw Normal View History

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
attr_accessor :collection, :connection
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
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
data << "\n#{Thread.current[:formatador].indentation}>"
data
2009-08-02 19:37:54 -04:00
end
def reload
2010-10-27 19:09:00 -04: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-21 22:18:34 -04:00
def wait_for(timeout=600, interval=1, &block)
reload
2010-07-21 22:18:34 -04:00
Fog.wait_for(timeout, interval) do
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
instance_eval(&block)
end
end
2009-08-02 19:37:54 -04:00
end
end