Class: Fog::Model

Inherits:
Object
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods, Core::DeprecatedConnectionAccessors
Defined in:
lib/fog/core/model.rb

Direct Known Subclasses

Compute::Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

Methods included from Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(new_attributes = {}) ⇒ Model

Returns a new instance of Model.



13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/core/model.rb', line 13

def initialize(new_attributes = {})
  # TODO Remove compatibility with old connection option
  attribs = new_attributes.clone
  @service = attribs.delete(:service)
  if @service.nil? && attribs[:connection]
    Fog::Logger.deprecation("Passing :connection option is deprecated, use :service instead [light_black](#{caller.first})[/]")
    @service = attribs[:connection]
  end
  merge_attributes(attribs)
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



10
11
12
# File 'lib/fog/core/model.rb', line 10

def collection
  @collection
end

#serviceObject (readonly)

Returns the value of attribute service.



11
12
13
# File 'lib/fog/core/model.rb', line 11

def service
  @service
end

Instance Method Details

#inspectObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/core/model.rb', line 24

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
  end
  data << "\n#{Thread.current[:formatador].indentation}>"
  data
end

#reloadObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/core/model.rb', line 37

def reload
  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

#symbolize_keys(hash) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/fog/core/model.rb', line 55

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

#to_json(options = {}) ⇒ Object



51
52
53
# File 'lib/fog/core/model.rb', line 51

def to_json(options = {})
  Fog::JSON.encode(attributes)
end

#wait_for(timeout = Fog.timeout, interval = 1, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/core/model.rb', line 63

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