Class: Harvesting::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/harvesting/models/base.rb

Direct Known Subclasses

HarvestRecord, HarvestRecordCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, opts = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
# 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

Instance Attribute Details

#attributesHash

Returns:

  • (Hash)


5
6
7
# File 'lib/harvesting/models/base.rb', line 5

def attributes
  @attributes
end

#harvest_clientHarvesting::Model::Client (readonly)

Returns:

  • (Harvesting::Model::Client)


7
8
9
# File 'lib/harvesting/models/base.rb', line 7

def harvest_client
  @harvest_client
end

Class Method Details

.get(id, opts = {}) ⇒ Object

Retrieves an instance of the object by ID

Parameters:

  • id (Integer)

    the id of the object to retrieve

  • opts (Hash) (defaults to: {})

    options to pass along to the ‘Harvesting::Client` instance



67
68
69
70
# 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

Instance Method Details

#createHarvesting::Models::Base

It creates the record.



28
29
30
# File 'lib/harvesting/models/base.rb', line 28

def create
  @harvest_client.create(self)
end

#deleteHarvesting::Models::Base

It removes the record.

Returns:

See Also:



44
45
46
# File 'lib/harvesting/models/base.rb', line 44

def delete
  @harvest_client.delete(self)
end

#fetchHarvesting::Models::Base

It loads a new record from your Harvest account.



58
59
60
# File 'lib/harvesting/models/base.rb', line 58

def fetch
  self.class.new(@harvest_client.get(path), harvest_client: @harvest_client)
end

#saveObject

It calls ‘create` or `update` depending on the record’s ID. If the ID is present, then it calls ‘update`. Otherwise it calls `create`



20
21
22
# File 'lib/harvesting/models/base.rb', line 20

def save
  id.nil? ? create : update
end

#to_hashHash

It returns keys and values for all the attributes of this record.

Returns:

  • (Hash)


51
52
53
# File 'lib/harvesting/models/base.rb', line 51

def to_hash
  @attributes
end

#updateHarvesting::Models::Base

It updates the record.



36
37
38
# File 'lib/harvesting/models/base.rb', line 36

def update
  @harvest_client.update(self)
end