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
|
2011-04-13 13:47:30 -04:00
|
|
|
|
|
|
|
return unless data = begin
|
|
|
|
collection.get(identity)
|
|
|
|
rescue Excon::Errors::SocketError
|
|
|
|
nil
|
2010-04-27 19:34:04 -04:00
|
|
|
end
|
2011-04-13 13:47:30 -04:00
|
|
|
|
|
|
|
new_attributes = data.attributes
|
|
|
|
merge_attributes(new_attributes)
|
|
|
|
self
|
2009-10-23 13:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-07-15 17:58:41 -04:00
|
|
|
def to_json(options = {})
|
2012-04-25 10:31:28 -04:00
|
|
|
Fog::JSON.encode(attributes)
|
2010-03-27 21:06:32 -04:00
|
|
|
end
|
|
|
|
|
2012-03-08 10:20:11 -05:00
|
|
|
def symbolize_keys(hash)
|
|
|
|
return nil if hash.nil?
|
|
|
|
hash.inject({}) do |options, (key, value)|
|
|
|
|
options[(key.to_sym rescue key) || key] = value
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-11 18:06:47 -04:00
|
|
|
def wait_for(timeout=Fog.timeout, interval=1, &block)
|
2010-05-01 18:08:44 -04:00
|
|
|
reload
|
2011-04-13 13:41:37 -04:00
|
|
|
retries = 3
|
2010-07-21 22:18:34 -04:00
|
|
|
Fog.wait_for(timeout, interval) do
|
2010-12-16 15:05:15 -05:00
|
|
|
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
|