Class: Tazworks::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/tazworks/model.rb
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ids:, attributes: {}, attributes_loaded: false) ⇒ Model
Returns a new instance of Model.
23
24
25
26
27
|
# File 'lib/tazworks/model.rb', line 23
def initialize(ids:, attributes: {}, attributes_loaded: false)
@attributes = attributes
@ids = ids
@attributes_loaded = attributes_loaded
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6
7
8
|
# File 'lib/tazworks/model.rb', line 6
def attributes
@attributes
end
|
#attributes_loaded ⇒ Object
Returns the value of attribute attributes_loaded.
7
8
9
|
# File 'lib/tazworks/model.rb', line 7
def attributes_loaded
@attributes_loaded
end
|
#ids ⇒ Object
Returns the value of attribute ids.
6
7
8
|
# File 'lib/tazworks/model.rb', line 6
def ids
@ids
end
|
Class Method Details
.find(ids) ⇒ Object
9
10
11
|
# File 'lib/tazworks/model.rb', line 9
def self.find(ids)
initialize_from_response(ids, get(ids))
end
|
.get(_ids) ⇒ Object
13
14
15
|
# File 'lib/tazworks/model.rb', line 13
def self.get(_ids)
raise 'Must Implement Me in the Concrete Class'
end
|
.initialize_from_response(ids, response) ⇒ Object
29
30
31
32
|
# File 'lib/tazworks/model.rb', line 29
def self.initialize_from_response(ids, response)
json_response = JSON.parse(response.body, { symbolize_names: true })
new(ids:, attributes: json_response, attributes_loaded: true)
end
|
Instance Method Details
#attributes_loaded? ⇒ Boolean
34
35
36
|
# File 'lib/tazworks/model.rb', line 34
def attributes_loaded?
!!@attributes_loaded
end
|
#clientGuid ⇒ Object
38
39
40
|
# File 'lib/tazworks/model.rb', line 38
def clientGuid
@ids[:clientGuid]
end
|
#load_attributes ⇒ Object
17
18
19
20
21
|
# File 'lib/tazworks/model.rb', line 17
def load_attributes
response = self.class.get(ids)
@attributes = JSON.parse(response.body, { symbolize_names: true })
@attributes_loaded = true
end
|
#loaded_attributes ⇒ Object
ensures attributes are loaded from the server
43
44
45
46
|
# File 'lib/tazworks/model.rb', line 43
def loaded_attributes
load_attributes unless attributes_loaded?
@attributes
end
|
#reload ⇒ Object
48
49
50
|
# File 'lib/tazworks/model.rb', line 48
def reload
load_attributes
end
|