Module: RD3::Model
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/rd3/model.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #class_attributes ⇒ Object
-
#dirty? ⇒ Boolean
tracks whether or not the instance has been updated/changed post initialization.
-
#errors=(value) ⇒ Object
add errors to errors collection allows errors to bubble up from a child model to its parent model when working with repositories.
-
#initialize(*args) ⇒ Object
NOTE: 1) we can create a new instance with a hash in such a case, default values will be set 2) we can also create a new instance by COPYING properties from one object to another via its attributes in such a case, default values will NOT be set.
- #persisted? ⇒ Boolean
Instance Method Details
#attributes ⇒ Object
91 92 93 |
# File 'lib/rd3/model.rb', line 91 def attributes self.instance_values.select { |key| class_attributes.include? key.to_sym }.symbolize_keys end |
#class_attributes ⇒ Object
87 88 89 |
# File 'lib/rd3/model.rb', line 87 def class_attributes return self.class.class_attributes end |
#dirty? ⇒ Boolean
tracks whether or not the instance has been updated/changed post initialization
79 80 81 |
# File 'lib/rd3/model.rb', line 79 def dirty? return @dirty ||= false end |
#errors=(value) ⇒ Object
add errors to errors collection allows errors to bubble up from a child model to its parent model when working with repositories
99 100 101 102 103 104 105 |
# File 'lib/rd3/model.rb', line 99 def errors=(value) unless value.blank? value.each do |k, v| self.errors[k] = v end end end |
#initialize(*args) ⇒ Object
NOTE: 1) we can create a new instance with a hash
in such a case, default values will be set
2) we can also create a new instance by COPYING
properties from one object to another via
its attributes
in such a case, default values will NOT be set
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rd3/model.rb', line 49 def initialize(*args) # default to an empty hash if Class.new is called, i.e., args = nil obj = args[0] || {} # an instance SHOULD be marked as dirty if: # 1) it's initialized w/ a non-empty hash # # it SHOULD NOT be marked as dirty if: # 1) Class.new is called (empty hash) # 2) we're mapping from one object to another is_dirty = obj.instance_of?(Hash) && !obj.blank? # grab attributes hash if we're dealing w/ mapping # an object instance obj = obj.attributes unless obj.instance_of? Hash # map properties obj.each do |k,v| self.send("#{k}=", v) if self.class_attributes.include? k end # reset dirty flag @dirty = is_dirty # return self for method chaining self end |
#persisted? ⇒ Boolean
83 84 85 |
# File 'lib/rd3/model.rb', line 83 def persisted? id.present? end |