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) ⇒ 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) ⇒ Base
Returns a new instance of Base.
7 8 9 10 |
# File 'lib/forecasting/models/base.rb', line 7 def initialize(attrs) @attributes = attrs.dup @models = {} end |
Instance Attribute Details
#attributes ⇒ Hash
5 6 7 |
# File 'lib/forecasting/models/base.rb', line 5 def attributes @attributes 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
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/forecasting/models/base.rb', line 33 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
54 55 56 57 58 59 60 61 |
# File 'lib/forecasting/models/base.rb', line 54 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] || {}) end end end |
Instance Method Details
#to_hash ⇒ Hash
It returns keys and values for all the attributes of this record.
15 16 17 |
# File 'lib/forecasting/models/base.rb', line 15 def to_hash @attributes end |