class MyJohnDeereApi::Model::Base
Attributes
Public Class Methods
arguments:
- client
-
the client, because it contains all the config info. The alternative would be a true Config block, but then settings would be app-wide. This allows one app to have multiple clients with different settings.
- record
-
a JSON object of type 'Field', returned from the API.
# File lib/my_john_deere_api/model/base.rb, line 18 def initialize(client, record) verify_record_type(record['@type']) @id = record['id'] @record = record @record_type = record['@type'] @client = client @unsaved = false map_attributes(record) @links = {} record['links'].each do |association| @links[underscore(association['rel'])] = uri_path(association['uri']) end end
Public Instance Methods
The client accessor
# File lib/my_john_deere_api/model/base.rb, line 39 def accessor return @accessor if defined?(@accessor) @accessor = client&.accessor end
Private Instance Methods
Expected record type. Override in child classes.
# File lib/my_john_deere_api/model/base.rb, line 58 def expected_record_type 'Base' end
This method receives the full record hash and extracts whatever extra attributes are needed for the given base class. This is intended to be overridden by child classes instead of monkeypatching initialize.
# File lib/my_john_deere_api/model/base.rb, line 51 def map_attributes(record) end
Mark as saved, so we don't try to save it later
# File lib/my_john_deere_api/model/base.rb, line 81 def mark_as_saved @unsaved = false end
Mark as unsaved, so we know to save it later
# File lib/my_john_deere_api/model/base.rb, line 74 def mark_as_unsaved @unsaved = true end
Are changes to this model synced with JD?
# File lib/my_john_deere_api/model/base.rb, line 87 def saved? !@unsaved end
Are there pending changes to send to JD?
# File lib/my_john_deere_api/model/base.rb, line 94 def unsaved? @unsaved end
Raise an error if this is not the type of record we expect to receive
# File lib/my_john_deere_api/model/base.rb, line 65 def verify_record_type(type) unless type == expected_record_type raise TypeMismatchError, "Expected record of type '#{expected_record_type}', but received type '#{type}'" end end