Module: Philiprehberger::AuditTrail::Replayable

Included in:
Tracker
Defined in:
lib/philiprehberger/audit_trail/replayable.rb

Overview

Reconstructs entity state by replaying recorded events in chronological order.

Instance Method Summary collapse

Instance Method Details

#replay(entity_id:, entity_type: nil, until_time: nil) ⇒ Hash

Replay events for an entity to rebuild its current (or point-in-time) state.

Walks recorded events oldest-first and folds each ‘:changes` hash into a state accumulator. `:create` and `:update` actions write `:to` values; `:delete` clears the field. Other actions are skipped.

Parameters:

  • entity_id (String)

    identifier of the entity to replay

  • entity_type (String, nil) (defaults to: nil)

    optional type filter

  • until_time (Time, nil) (defaults to: nil)

    cutoff timestamp; events at or before this time are included

Returns:

  • (Hash)

    reconstructed state hash



17
18
19
20
21
# File 'lib/philiprehberger/audit_trail/replayable.rb', line 17

def replay(entity_id:, entity_type: nil, until_time: nil)
  events_for_replay(entity_id, entity_type, until_time).each_with_object({}) do |event, state|
    apply_event(state, event)
  end
end