Class: Arbor::Model::Abstract
- Inherits:
-
Object
- Object
- Arbor::Model::Abstract
show all
- Defined in:
- lib/arbor/model/abstract.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes) ⇒ Abstract
Returns a new instance of Abstract.
8
9
10
11
|
# File 'lib/arbor/model/abstract.rb', line 8
def initialize(attributes)
@attribute_names = []
load_attributes(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/arbor/model/abstract.rb', line 13
def method_missing(meth, *args, &block)
attribute_lock ? super : begin
refresh_data
send(meth, *args, &block)
end
end
|
Instance Attribute Details
#api_client ⇒ Object
Returns the value of attribute api_client.
6
7
8
|
# File 'lib/arbor/model/abstract.rb', line 6
def api_client
@api_client
end
|
#attribute_lock ⇒ Object
Returns the value of attribute attribute_lock.
6
7
8
|
# File 'lib/arbor/model/abstract.rb', line 6
def attribute_lock
@attribute_lock
end
|
#attribute_names ⇒ Object
Returns the value of attribute attribute_names.
6
7
8
|
# File 'lib/arbor/model/abstract.rb', line 6
def attribute_names
@attribute_names
end
|
#entity_type ⇒ Object
Returns the value of attribute entity_type.
6
7
8
|
# File 'lib/arbor/model/abstract.rb', line 6
def entity_type
@entity_type
end
|
#href ⇒ Object
Returns the value of attribute href.
6
7
8
|
# File 'lib/arbor/model/abstract.rb', line 6
def href
@href
end
|
Instance Method Details
#attach_client(client) ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/arbor/model/abstract.rb', line 56
def attach_client(client)
self.api_client = client
self.attribute_names.each do |name|
if send(name).respond_to?("api_client=")
send(name).attach_client(client)
end
end
end
|
#attribute(name) ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/arbor/model/abstract.rb', line 46
def attribute(name)
if instance_variable_defined?("@#{name}")
instance_variable_get("@#{name}")
else
raise Errors::UnknownAttributeError if attribute_lock
refresh_data
attribute(name)
end
end
|
#attributes ⇒ Object
69
70
71
|
# File 'lib/arbor/model/abstract.rb', line 69
def attributes
Hash[attribute_names.map { |a| [a, send(a)] }]
end
|
#inspect ⇒ Object
73
74
75
|
# File 'lib/arbor/model/abstract.rb', line 73
def inspect
"#<#{self.class.name} #{attribute_names.map { |a| "#{a}: #{send(a).inspect}" }.join(', ')}>"
end
|
#load_attributes(attributes) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/arbor/model/abstract.rb', line 35
def load_attributes(attributes)
attributes.each do |name, value|
unless self.respond_to?("#{name}=")
self.class.instance_eval { define_method(name) { attribute(name) } }
self.class.send(:attr_writer, name)
end
@attribute_names |= [name]
self.send("#{name}=", value)
end
end
|
#refresh_data ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/arbor/model/abstract.rb', line 20
def refresh_data
raise "No API client configured for this resource" if api_client.nil?
raise "No entity_type set for this resource" if entity_type.nil?
raise "No known endpoint for this resource" if href.nil?
data = api_client.get(href)
entity_type_lower = entity_type.tr('_', '-').camelize(:lower).tr('-', '_')
parsed_attributes = Serialiser.parse_attributes(data[entity_type_lower])
load_attributes(parsed_attributes)
attach_client(self.api_client)
@attribute_lock = true
end
|
#to_json ⇒ Object
77
78
79
80
|
# File 'lib/arbor/model/abstract.rb', line 77
def to_json
Hash[instance_variables.map{ |iv| [iv[1..-1], instance_variable_get(iv)] }]
.reject{ |n, _v| n == 'api_client' }.to_json
end
|
#unlock_attributes ⇒ Object
65
66
67
|
# File 'lib/arbor/model/abstract.rb', line 65
def unlock_attributes
@attribute_lock = false
end
|