Class: Harvesting::Models::Base
- Inherits:
-
Object
- Object
- Harvesting::Models::Base
- Defined in:
- lib/harvesting/models/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
Class Method Summary collapse
-
.get(id, opts = {}) ⇒ Object
Retrieves an instance of the object by ID.
Instance Method Summary collapse
-
#create ⇒ Harvesting::Models::Base
It creates the record.
-
#delete ⇒ Harvesting::Models::Base
It removes the record.
-
#fetch ⇒ Harvesting::Models::Base
It loads a new record from your Harvest account.
-
#initialize(attrs, opts = {}) ⇒ Base
constructor
A new instance of Base.
-
#save ⇒ Object
It calls ‘create` or `update` depending on the record’s ID.
-
#to_hash ⇒ Hash
It returns keys and values for all the attributes of this record.
-
#update ⇒ Harvesting::Models::Base
It updates the record.
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
#attributes ⇒ Hash
5 6 7 |
# File 'lib/harvesting/models/base.rb', line 5 def attributes @attributes end |
#harvest_client ⇒ Harvesting::Model::Client (readonly)
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
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
#create ⇒ Harvesting::Models::Base
It creates the record.
28 29 30 |
# File 'lib/harvesting/models/base.rb', line 28 def create @harvest_client.create(self) end |
#delete ⇒ Harvesting::Models::Base
It removes the record.
44 45 46 |
# File 'lib/harvesting/models/base.rb', line 44 def delete @harvest_client.delete(self) end |
#fetch ⇒ Harvesting::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 |
#save ⇒ Object
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_hash ⇒ Hash
It returns keys and values for all the attributes of this record.
51 52 53 |
# File 'lib/harvesting/models/base.rb', line 51 def to_hash @attributes end |
#update ⇒ Harvesting::Models::Base
It updates the record.
36 37 38 |
# File 'lib/harvesting/models/base.rb', line 36 def update @harvest_client.update(self) end |