class Harvesting::Models::Base
Attributes
@return [Hash]
@return [Harvesting::Model::Client]
Public Class Methods
Retrieves an instance of the object by ID
@param id [Integer] the id of the object to retrieve @param opts [Hash] options to pass along to the `Harvesting::Client`
instance
# File lib/harvesting/models/base.rb, line 67 def self.get(id, opts = {}) client = opts[:harvest_client] || Harvesting::Client.new(opts) self.new({ 'id' => id }, opts).fetch end
# File lib/harvesting/models/base.rb, line 9 def initialize(attrs, opts = {}) @models = {} @attributes = attrs.dup @harvest_client = opts[:harvest_client] || Harvesting::Client.new(opts) end
Protected Class Methods
Class method to define attribute methods for accessing attributes for a record
It needs to be used like this:
class Contact < HarvestRecord attributed :id, :title, :first_name ... end
@param attribute_names [Array] A list of attributes
# File lib/harvesting/models/base.rb, line 87 def self.attributed(*attribute_names) attribute_names.each do |attribute_name| define_method(attribute_name) do @attributes[__method__.to_s] end define_method("#{attribute_name}=") do |value| @attributes[__method__.to_s.chop] = value end end end
Class method to define nested resources for a record.
It needs to be used like this:
class Contact < HarvestRecord modeled client: Client ... end
@param opts [Hash] key = symbol that needs to be the same as the one returned by the Harvest API. value = model class for the nested resource.
# File lib/harvesting/models/base.rb, line 108 def self.modeled(opts = {}) opts.each do |attribute_name, model| attribute_name_string = attribute_name.to_s Harvesting::Models::Base.send :define_method, attribute_name_string do @models[attribute_name_string] ||= model.new(@attributes[attribute_name_string] || {}, harvest_client: harvest_client) end end end
Public Instance Methods
It creates the record.
@see Client#create @return [Harvesting::Models::Base]
# File lib/harvesting/models/base.rb, line 28 def create @harvest_client.create(self) end
It removes the record.
@see Client#delete @return [Harvesting::Models::Base]
# File lib/harvesting/models/base.rb, line 44 def delete @harvest_client.delete(self) end
It loads a new record from your Harvest account.
@return [Harvesting::Models::Base]
# File lib/harvesting/models/base.rb, line 58 def fetch self.class.new(@harvest_client.get(path), harvest_client: @harvest_client) end
It calls `create` or `update` depending on the record's ID. If the ID is present, then it calls `update`. Otherwise it calls `create`
@see Client#create @see Client#update
# File lib/harvesting/models/base.rb, line 20 def save id.nil? ? create : update end
It returns keys and values for all the attributes of this record.
@return [Hash]
# File lib/harvesting/models/base.rb, line 51 def to_hash @attributes end
It updates the record.
@see Client#update @return [Harvesting::Models::Base]
# File lib/harvesting/models/base.rb, line 36 def update @harvest_client.update(self) end