Class: Forecasting::Models::ForecastRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/forecasting/models/forecast_record.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attributed, #to_hash

Constructor Details

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

Returns a new instance of ForecastRecord.



7
8
9
10
# File 'lib/forecasting/models/forecast_record.rb', line 7

def initialize(attrs, opts = {})
  @forecast_client = opts[:forecast_client] || Forecasting::Client.new(**opts)
  super(attrs)
end

Instance Attribute Details

#forecast_clientForecasting::Model::Client (readonly)

Returns:

  • (Forecasting::Model::Client)


5
6
7
# File 'lib/forecasting/models/forecast_record.rb', line 5

def forecast_client
  @forecast_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 Forecasting::Client instance



47
48
49
50
# File 'lib/forecasting/models/forecast_record.rb', line 47

def self.get(id, opts = {})
  client = opts[:forecast_client] || Forecasting::Client.new(**opts)
  self.new({ 'id' => id }, opts).fetch
end

Instance Method Details

#createObject



16
17
18
# File 'lib/forecasting/models/forecast_record.rb', line 16

def create
  forecast_client.create(self)
end

#deleteObject



24
25
26
# File 'lib/forecasting/models/forecast_record.rb', line 24

def delete
  forecast_client.delete(self)
end

#fetchForecasting::Models::Base

It loads a new record from your Harvest account.



31
32
33
# File 'lib/forecasting/models/forecast_record.rb', line 31

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

#saveObject



12
13
14
# File 'lib/forecasting/models/forecast_record.rb', line 12

def save
  id.nil? ? create : update
end

#typeString

It returns the model type

Returns:

  • (String)


38
39
40
# File 'lib/forecasting/models/forecast_record.rb', line 38

def type
  self.class.name.split("::").last.downcase
end

#updateObject



20
21
22
# File 'lib/forecasting/models/forecast_record.rb', line 20

def update
  forecast_client.update(self)
end