2012-12-19 22:28:04 +00:00
|
|
|
require "fog/core/deprecated_connection_accessors"
|
|
|
|
|
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
|
2012-12-19 22:28:04 +00:00
|
|
|
include Fog::Core::DeprecatedConnectionAccessors
|
2009-11-21 13:46:46 -08:00
|
|
|
|
2012-12-19 22:28:04 +00:00
|
|
|
attr_accessor :collection
|
|
|
|
attr_reader :service
|
2009-10-29 23:35:28 -07:00
|
|
|
|
2009-08-30 14:14:11 -07:00
|
|
|
def initialize(new_attributes = {})
|
2012-12-19 22:28:04 +00:00
|
|
|
# TODO Remove compatibility with old connection option
|
2013-01-11 14:28:13 +08:00
|
|
|
@service = new_attributes.delete(:service)
|
2012-12-19 22:28:04 +00:00
|
|
|
if @service.nil? && new_attributes[:connection]
|
|
|
|
Fog::Logger.deprecation("Passing :connection option is deprecated, use :service instead [light_black](#{caller.first})[/]")
|
|
|
|
@service = new_attributes[:connection]
|
|
|
|
end
|
2009-08-30 14:14:11 -07:00
|
|
|
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-10-27 16:09:00 -07:00
|
|
|
requires :identity
|
2011-04-13 10:47:30 -07:00
|
|
|
|
|
|
|
return unless data = begin
|
|
|
|
collection.get(identity)
|
|
|
|
rescue Excon::Errors::SocketError
|
|
|
|
nil
|
2010-04-27 16:34:04 -07:00
|
|
|
end
|
2011-04-13 10:47:30 -07:00
|
|
|
|
|
|
|
new_attributes = data.attributes
|
|
|
|
merge_attributes(new_attributes)
|
|
|
|
self
|
2009-10-23 10:27:23 -07:00
|
|
|
end
|
|
|
|
|
2011-07-15 23:58:41 +02:00
|
|
|
def to_json(options = {})
|
2012-04-25 10:31:28 -04:00
|
|
|
Fog::JSON.encode(attributes)
|
2010-03-27 18:06:32 -07:00
|
|
|
end
|
|
|
|
|
2012-03-08 15:20:11 +00: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)
|
2013-01-08 14:01:16 -08:00
|
|
|
reload_has_succeeded = false
|
|
|
|
duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
|
2010-12-16 12:05:15 -08:00
|
|
|
if reload
|
2013-01-08 14:01:16 -08:00
|
|
|
reload_has_succeeded = true
|
|
|
|
instance_eval(&block)
|
|
|
|
else
|
|
|
|
false
|
2010-12-16 12:05:15 -08:00
|
|
|
end
|
2013-01-08 14:01:16 -08:00
|
|
|
end
|
|
|
|
if reload_has_succeeded
|
|
|
|
return duration # false if timeout; otherwise {:duration => elapsed time }
|
|
|
|
else
|
|
|
|
raise Fog::Errors::Error.new("Reload failed, #{self.class} #{self.identity} not present.")
|
2010-05-07 09:51:58 -07:00
|
|
|
end
|
2009-12-07 22:23:55 -08:00
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
end
|