Class: Datamappify::Repository::UnitOfWork::PersistentStates::Object
- Inherits:
-
Object
- Object
- Datamappify::Repository::UnitOfWork::PersistentStates::Object
- Includes:
- ActiveModel::Dirty
- Defined in:
- lib/datamappify/repository/unit_of_work/persistent_states/object.rb
Overview
an object that mirrors an entity’s attributes and their initial (clean) values
Instance Method Summary collapse
-
#attributes_for(entity) ⇒ Hash
private
Entity attributes, based on whether the entity is lazy loaded.
-
#construct_attribute(name) ⇒ void
private
Constructs an attribute with a getter, setter and ‘_changed?’ method.
-
#construct_changed(name) ⇒ void
private
Constructs the
attr_changed?method. -
#construct_getter(name) ⇒ void
private
Constructs an attribute getter.
-
#construct_setter(name) ⇒ void
private
Constructs an attribute setter, the setter itself does NOT need to set the value as the value is never going to be used.
-
#initialize(entity) ⇒ Object
constructor
A new instance of Object.
-
#mark_as_dirty ⇒ void
private
Mark all attributes as dirty, useful for a non-persisted object.
-
#new? ⇒ Boolean
Is the object new (not persisted yet)?.
-
#set_value(name, value) ⇒ any
private
Sets an attribute value by making a copy of the data.
-
#update_values(entity) ⇒ void
Updates all the attribute values according to the entity.
Constructor Details
#initialize(entity) ⇒ Object
Returns a new instance of Object.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 10 def initialize(entity) @entity = entity attributes = attributes_for(@entity) attributes.each do |name, value| construct_attribute(name) set_value(name, value) end self.class.define_attribute_methods(attributes.keys) mark_as_dirty if new? end |
Instance Method Details
#attributes_for(entity) ⇒ Hash (private)
Entity attributes, based on whether the entity is lazy loaded
115 116 117 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 115 def attributes_for(entity) entity.lazy_loaded? ? entity.cached_attributes : entity.attributes end |
#construct_attribute(name) ⇒ void (private)
This method returns an undefined value.
Constructs an attribute with a getter, setter and ‘_changed?’ method
48 49 50 51 52 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 48 def construct_attribute(name) construct_getter(name) construct_setter(name) construct_changed(name) end |
#construct_changed(name) ⇒ void (private)
This method returns an undefined value.
Constructs the attr_changed? method
84 85 86 87 88 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 84 def construct_changed(name) define_singleton_method "#{name}_changed?" do changed_attributes.include?(name) end end |
#construct_getter(name) ⇒ void (private)
This method returns an undefined value.
Constructs an attribute getter
59 60 61 62 63 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 59 def construct_getter(name) define_singleton_method name do instance_variable_get "@#{name}" end end |
#construct_setter(name) ⇒ void (private)
This method returns an undefined value.
Constructs an attribute setter, the setter itself does NOT need to set the value as the value is never going to be used.
The setter sets the attr_will_change! flag when necessary.
73 74 75 76 77 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 73 def construct_setter(name) define_singleton_method "#{name}=" do |value| send(:attribute_will_change!, name) unless send(name) == value end end |
#mark_as_dirty ⇒ void (private)
This method returns an undefined value.
Mark all attributes as dirty, useful for a non-persisted object
104 105 106 107 108 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 104 def mark_as_dirty attributes_for(@entity).each do |name, _| send(:attribute_will_change!, name) end end |
#new? ⇒ Boolean
Is the object new (not persisted yet)?
39 40 41 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 39 def new? @entity.id.nil? end |
#set_value(name, value) ⇒ any (private)
Sets an attribute value by making a copy of the data
97 98 99 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 97 def set_value(name, value) instance_variable_set "@#{name}", Marshal.load(Marshal.dump(value)) end |
#update_values(entity) ⇒ void
This method returns an undefined value.
Updates all the attribute values according to the entity
29 30 31 32 33 34 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 29 def update_values(entity) attributes_for(entity).each do |name, value| construct_attribute(name) send("#{name}=", value) end end |