2012-12-19 17:28:04 -05:00
|
|
|
require "fog/core/deprecated_connection_accessors"
|
|
|
|
|
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
|
2012-12-19 17:28:04 -05:00
|
|
|
include Fog::Core::DeprecatedConnectionAccessors
|
2009-11-21 16:46:46 -05:00
|
|
|
|
2012-12-19 17:28:04 -05:00
|
|
|
attr_accessor :collection
|
|
|
|
attr_reader :service
|
2009-10-30 02:35:28 -04:00
|
|
|
|
2009-08-30 17:14:11 -04:00
|
|
|
def initialize(new_attributes = {})
|
2012-12-19 17:28:04 -05:00
|
|
|
# TODO Remove compatibility with old connection option
|
2013-01-11 01:28:13 -05:00
|
|
|
@service = new_attributes.delete(:service)
|
2012-12-19 17:28:04 -05: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 17:14:11 -04:00
|
|
|
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)
|
2013-01-08 17:01:16 -05:00
|
|
|
reload_has_succeeded = false
|
|
|
|
duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
|
2010-12-16 15:05:15 -05:00
|
|
|
if reload
|
2013-01-08 17:01:16 -05:00
|
|
|
reload_has_succeeded = true
|
|
|
|
instance_eval(&block)
|
|
|
|
else
|
|
|
|
false
|
2010-12-16 15:05:15 -05:00
|
|
|
end
|
2013-01-08 17:01:16 -05: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 12:51:58 -04:00
|
|
|
end
|
2009-12-08 01:23:55 -05:00
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
end
|