class Datamappify::Repository::UnitOfWork::PersistentStates::Object
an object that mirrors an entity’s attributes and their initial (clean) values
Public Class Methods
@param entity [Entity]
# 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
Public Instance Methods
Mark selected or all attributes as dirty, useful for a non-persisted object or for manually trigger attributes update
@param attrs [Symbol]
An array or a hash of which the keys are attribute symbols
@return [void]
# 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
Is the object new (not persisted yet)?
@return [Boolean]
# File lib/datamappify/repository/unit_of_work/persistent_states/object.rb, line 53 def new? @entity.id.nil? end
Updates all the attribute values according to the entity
@param entity [Entity]
@return [void]
# 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
Private Instance Methods
Entity
attributes, based on whether the entity is lazy loaded
@param entity [Entity]
@return [Hash]
# 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
Constructs an attribute with a getter, setter and ‘_changed?’ method
@return [void]
# 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
Constructs the ‘attr_changed?` method
@param name [Symbol]
@return [void]
# 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
Constructs an attribute getter
@param name [Symbol]
@return [void]
# 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
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.
@param name [Symbol]
@return [void]
# 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
Sets an attribute value by making a copy of the data
@param name [Symbol]
@param value [any]
@return [any]
# 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