class MyJohnDeereApi::Model::Base

Attributes

client[R]
id[R]
record[R]
record_type[R]

Public Class Methods

new(client, record) click to toggle source

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

accessor() click to toggle source

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() click to toggle source

Expected record type. Override in child classes.

# File lib/my_john_deere_api/model/base.rb, line 58
def expected_record_type
  'Base'
end
map_attributes(record) click to toggle source

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() click to toggle source

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() click to toggle source

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
saved?() click to toggle source

Are changes to this model synced with JD?

# File lib/my_john_deere_api/model/base.rb, line 87
def saved?
  !@unsaved
end
unsaved?() click to toggle source

Are there pending changes to send to JD?

# File lib/my_john_deere_api/model/base.rb, line 94
def unsaved?
  @unsaved
end
verify_record_type(type) click to toggle source

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