Class: Forecasting::Models::Base
- Inherits:
-
Object
- Object
- Forecasting::Models::Base
- Defined in:
- lib/forecasting/models/base.rb
Direct Known Subclasses
Account, Address, Card, ColorLabel, Discount, ForecastRecord, FtuxState, FutureScheduledHours, RemainingBudgetedHours, Subscription, User, UserConnection
Instance Attribute Summary collapse
Class Method Summary collapse
-
.attributed(*attribute_names) ⇒ Object
Class method to define attribute methods for accessing attributes for a record.
-
.modeled(opts = {}) ⇒ Object
Class method to define nested resources for a record.
Instance Method Summary collapse
-
#initialize(attrs, opts = {}) ⇒ Base
constructor
A new instance of Base.
-
#to_hash ⇒ Hash
It returns keys and values for all the attributes of this record.
Constructor Details
#initialize(attrs, opts = {}) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 |
# File 'lib/forecasting/models/base.rb', line 10 def initialize(attrs, opts = {}) @forecast_client = opts[:forecast_client] || Forecasting::Client.new(**opts) @attributes = attrs.dup @models = {} end |
Instance Attribute Details
#attributes ⇒ Hash
8 9 10 |
# File 'lib/forecasting/models/base.rb', line 8 def attributes @attributes end |
#forecast_client ⇒ Forecasting::Client (readonly)
5 6 7 |
# File 'lib/forecasting/models/base.rb', line 5 def forecast_client @forecast_client end |
Class Method Details
.attributed(*attribute_names) ⇒ Object
Class method to define attribute methods for accessing attributes for a record
It needs to be used like this:
class User < ForecastRecord
attributed :id,
:title,
:first_name
...
end
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/forecasting/models/base.rb', line 36 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 |
.modeled(opts = {}) ⇒ Object
Class method to define nested resources for a record.
It needs to be used like this:
class Project < Base
modeled client: Client
...
end
57 58 59 60 61 62 63 64 |
# File 'lib/forecasting/models/base.rb', line 57 def self.modeled(opts = {}) opts.each do |attribute_name, model| attribute_name_string = attribute_name.to_s define_method(attribute_name_string) do @models[attribute_name_string] ||= model.new(@attributes[attribute_name_string] || {}, forecast_client: forecast_client) end end end |
Instance Method Details
#to_hash ⇒ Hash
It returns keys and values for all the attributes of this record.
19 20 21 |
# File 'lib/forecasting/models/base.rb', line 19 def to_hash @attributes end |