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

81 lines
2.3 KiB
Ruby
Raw Normal View History

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
include Fog::Core::DeprecatedConnectionAccessors
attr_accessor :collection
attr_reader :service
2009-08-30 17:14:11 -04:00
def initialize(new_attributes = {})
# TODO Remove compatibility with old connection option
@service = new_attributes.delete(:service)
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
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
return unless data = begin
collection.get(identity)
rescue Excon::Errors::SocketError
nil
end
new_attributes = data.attributes
merge_attributes(new_attributes)
self
end
def to_json(options = {})
2012-04-25 10:31:28 -04:00
Fog::JSON.encode(attributes)
end
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)
reload_has_succeeded = false
duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
if reload
reload_has_succeeded = true
instance_eval(&block)
else
false
end
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.")
end
end
2009-08-02 19:37:54 -04:00
end
end