class Datamappify::Repository::UnitOfWork::PersistentStates::Object

an object that mirrors an entity’s attributes and their initial (clean) values

Public Class Methods

new(entity) click to toggle source

@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_as_dirty(*attributes) click to toggle source

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
new?() click to toggle source

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
update_values(entity) click to toggle source

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

attributes_for(entity) click to toggle source

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
construct_attribute(name) click to toggle source

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
construct_changed(name) click to toggle source

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
construct_getter(name) click to toggle source

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
construct_setter(name) click to toggle source

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
set_value(name, value) click to toggle source

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