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(*attributes) ⇒ void
Mark selected or all attributes as dirty, useful for a non-persisted object or for manually trigger attributes update.
-
#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
120 121 122 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 120 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
62 63 64 65 66 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 62 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
98 99 100 101 102 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 98 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
73 74 75 76 77 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 73 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.
87 88 89 90 91 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 87 def construct_setter(name) define_singleton_method "#{name}=" do |value| send(:attribute_will_change!, name) unless send(name) == value end end |
#mark_as_dirty(*attributes) ⇒ void
This method returns an undefined value.
Mark selected or all attributes as dirty, useful for a non-persisted object or for manually trigger attributes update
43 44 45 46 47 48 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 43 def mark_as_dirty(*attributes) attributes = attributes.any? ? attributes : attributes_for(@entity) attributes.each do |name, _| send(:attribute_will_change!, name) end end |
#new? ⇒ Boolean
Is the object new (not persisted yet)?
53 54 55 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 53 def new? @entity.id.nil? end |
#set_value(name, value) ⇒ any (private)
Sets an attribute value by making a copy of the data
111 112 113 |
# File 'lib/datamappify/repository/unit_of_work/persistent_states/object.rb', line 111 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 |