Class: Tazworks::Model

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

Overview

Base model

Direct Known Subclasses

Applicant, Client, ClientProduct, Order

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

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/tazworks/model.rb', line 6

def attributes
  @attributes
end

#attributes_loadedObject (readonly)

Returns the value of attribute attributes_loaded.



7
8
9
# File 'lib/tazworks/model.rb', line 7

def attributes_loaded
  @attributes_loaded
end

#idsObject

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/tazworks/model.rb', line 34

def attributes_loaded?
  !!@attributes_loaded
end

#clientGuidObject



38
39
40
# File 'lib/tazworks/model.rb', line 38

def clientGuid
  @ids[:clientGuid]
end

#load_attributesObject



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_attributesObject

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

#reloadObject



48
49
50
# File 'lib/tazworks/model.rb', line 48

def reload
  load_attributes
end