Class: Datamappify::Repository::UnitOfWork::PersistentStates

Inherits:
Object
  • Object
show all
Defined in:
lib/datamappify/repository/unit_of_work/persistent_states.rb,
lib/datamappify/repository/unit_of_work/persistent_states/object.rb

Overview

Tracks dirty entity attributes

Defined Under Namespace

Classes: Object

Instance Method Summary collapse

Constructor Details

#initializePersistentStates

Returns a new instance of PersistentStates.



8
9
10
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 8

def initialize
  @pool = {}
end

Instance Method Details

#attach(entity) ⇒ Entity

Attaches an entity

Parameters:

Returns:



35
36
37
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 35

def attach(entity)
  @pool[entity.object_id] = Object.new(entity)
end

#find(entity) ⇒ Entity

Finds or attaches an entity

Parameters:

Returns:



17
18
19
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 17

def find(entity)
  @pool.has_key?(entity.object_id) ? refresh(entity) : attach(entity)
end

#mark_as_dirty(entity, *attributes) ⇒ void

This method returns an undefined value.

Parameters:

  • entity (Entity)
  • attrs (Symbol)

    An array or a hash of which the keys are attribute symbols

See Also:



59
60
61
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 59

def mark_as_dirty(entity, *attributes)
  find(entity).mark_as_dirty(*attributes)
end

#refresh(entity) ⇒ Entity

Refreshes the states stored for an entity

Parameters:

Returns:



26
27
28
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 26

def refresh(entity)
  @pool[entity.object_id].tap { |o| o.update_values(entity) }
end

#update(entity, &block) ⇒ Entity

Executes a block then reattaches the entity

Parameters:

Returns:



44
45
46
47
48
49
50
# File 'lib/datamappify/repository/unit_of_work/persistent_states.rb', line 44

def update(entity, &block)
  find(entity)

  block.call

  attach(entity)
end