Class: Fog::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ Model

Returns a new instance of Model.



58
59
60
# File 'lib/fog/model.rb', line 58

def initialize(new_attributes = {})
  merge_attributes(new_attributes)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



36
37
38
# File 'lib/fog/model.rb', line 36

def connection
  @connection
end

Class Method Details

._load(marshalled) ⇒ Object



4
5
6
# File 'lib/fog/model.rb', line 4

def self._load(marshalled)
  new(Marshal.load(marshalled))
end

.aliasesObject



8
9
10
# File 'lib/fog/model.rb', line 8

def self.aliases
  @aliases ||= {}
end

.attribute(name, other_names = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/model.rb', line 16

def self.attribute(name, other_names = [])
  class_eval <<-EOS, __FILE__, __LINE__
    attr_accessor :#{name}
  EOS
  @attributes ||= []
  @attributes |= [name]
  for other_name in [*other_names]
    aliases[other_name] = name
  end
end

.attributesObject



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

def self.attributes
  @attributes ||= []
end

.identity(name, other_names = []) ⇒ Object



27
28
29
30
# File 'lib/fog/model.rb', line 27

def self.identity(name, other_names = [])
  @identity = name
  self.attribute(name, other_names)
end

Instance Method Details

#_dumpObject



32
33
34
# File 'lib/fog/model.rb', line 32

def _dump
  Marshal.dump(attributes)
end

#attributesObject



38
39
40
41
42
43
44
# File 'lib/fog/model.rb', line 38

def attributes
  attributes = {}
  for attribute in self.class.attributes
    attributes[attribute] = send("#{attribute}")
  end
  attributes
end

#collectionObject



46
47
48
# File 'lib/fog/model.rb', line 46

def collection
  @collection
end

#identityObject



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

def identity
  send(self.class.instance_variable_get('@identity'))
end

#identity=(new_identity) ⇒ Object



54
55
56
# File 'lib/fog/model.rb', line 54

def identity=(new_identity)
  send("#{identity}=", new_identity)
end

#inspectObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/model.rb', line 62

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

#merge_attributes(new_attributes = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/model.rb', line 75

def merge_attributes(new_attributes = {})
  for key, value in new_attributes
    if aliased_key = self.class.aliases[key]
      send("#{aliased_key}=", value)
    else
      send("#{key}=", value)
    end
  end
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/fog/model.rb', line 86

def new_record?
  !identity
end

#reloadObject



90
91
92
93
94
95
96
# File 'lib/fog/model.rb', line 90

def reload
  if data = collection.get(identity)
    new_attributes = data.attributes
    merge_attributes(new_attributes)
    self
  end
end

#requires(*args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/model.rb', line 98

def requires(*args)
  missing = []
  for arg in [:connection] | args
    missing << arg unless send("#{arg}")
  end
  unless missing.empty?
    if missing.length == 1
      raise(ArgumentError, "#{missing.first} is required for this operation")
    else
      raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
    end
  end
end

#to_jsonObject



112
113
114
# File 'lib/fog/model.rb', line 112

def to_json
  attributes.to_json
end

#wait_for(timeout = 600, &block) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/fog/model.rb', line 116

def wait_for(timeout = 600, &block)
  reload
  Fog.wait_for(timeout) do
    reload or raise StandardError.new("Reload failed, #{self.class} #{self.identity} went away.")
    instance_eval(&block)
  end
end