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.


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

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

#inspectObject


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/model.rb', line 58

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


71
72
73
74
75
76
77
78
79
80
# File 'lib/fog/model.rb', line 71

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)

82
83
84
# File 'lib/fog/model.rb', line 82

def new_record?
  !identity
end

#reloadObject


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

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

#requires(*args) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fog/model.rb', line 91

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


105
106
107
# File 'lib/fog/model.rb', line 105

def to_json
  attributes.to_json
end

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


109
110
111
112
113
114
115
116
117
118
# File 'lib/fog/model.rb', line 109

def wait_for(timeout = 600, &block)
  start = Time.now
  until instance_eval(&block)
    if Time.now - start > timeout
      break
    end
    reload
    sleep(1)
  end
end