Class: DCA::Models::BaseModel
- Inherits:
-
Object
- Object
- DCA::Models::BaseModel
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Validations, Binder, Storage
- Defined in:
- lib/dca/models/base_model.rb
Direct Known Subclasses
Constant Summary
Constants included from Binder
DCA::Models::Binder::COMPLEX_TYPE
Instance Attribute Summary collapse
-
#base_id ⇒ Object
Returns the value of attribute base_id.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #attributes ⇒ Object
- #before_create ⇒ Object
- #before_update ⇒ Object
-
#initialize(params = {}) ⇒ BaseModel
constructor
A new instance of BaseModel.
- #persisted? ⇒ Boolean
- #to_hash ⇒ Object
- #validate_associations ⇒ Object
Methods included from Storage
Methods included from Binder
Constructor Details
#initialize(params = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
18 19 20 |
# File 'lib/dca/models/base_model.rb', line 18 def initialize(params={}) params.each { |attr, value| self.instance_variable_set "@#{attr}", value } if params end |
Instance Attribute Details
#base_id ⇒ Object
Returns the value of attribute base_id.
16 17 18 |
# File 'lib/dca/models/base_model.rb', line 16 def base_id @base_id end |
#created_at ⇒ Object
Returns the value of attribute created_at.
16 17 18 |
# File 'lib/dca/models/base_model.rb', line 16 def created_at @created_at end |
#id ⇒ Object
Returns the value of attribute id.
16 17 18 |
# File 'lib/dca/models/base_model.rb', line 16 def id @id end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
16 17 18 |
# File 'lib/dca/models/base_model.rb', line 16 def updated_at @updated_at end |
Instance Method Details
#attributes ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/dca/models/base_model.rb', line 32 def attributes return @attributes unless @attributes.nil? @attributes = Hash[instance_variables.map { |var| [var.to_s.delete('@'), instance_variable_get(var)]}] @attributes.delete 'errors' @attributes.delete 'validation_context' @attributes end |
#before_create ⇒ Object
46 47 48 |
# File 'lib/dca/models/base_model.rb', line 46 def before_create self.created_at = Time.now.utc end |
#before_update ⇒ Object
42 43 44 |
# File 'lib/dca/models/base_model.rb', line 42 def before_update self.updated_at = Time.now.utc end |
#persisted? ⇒ Boolean
22 23 24 |
# File 'lib/dca/models/base_model.rb', line 22 def persisted? true end |
#to_hash ⇒ Object
26 27 28 29 30 |
# File 'lib/dca/models/base_model.rb', line 26 def to_hash include = [] self.class.associations(true).each { |field, | include << field.to_s} self.serializable_hash include: include end |
#validate_associations ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dca/models/base_model.rb', line 50 def validate_associations self.class.associations.each do |field, | object = self.send(field) next if object.nil? if object.is_a? Array object.each { |item| validate_child item, field } else validate_child object, field end end end |