Class: Philiprehberger::AuditTrail::Tracker

Inherits:
Object
  • Object
show all
Includes:
Batchable, Exportable, Prunable, Queryable, Summarizable
Defined in:
lib/philiprehberger/audit_trail/tracker.rb

Overview

Main tracker class for recording and querying audit events.

Constant Summary

Constants included from Summarizable

Summarizable::VALID_GROUP_KEYS

Constants included from Exportable

Exportable::EXPORT_HEADERS

Instance Method Summary collapse

Methods included from Summarizable

#summary

Methods included from Exportable

#export

Methods included from Batchable

#record_batch

Methods included from Prunable

#prune

Methods included from Queryable

#query

Constructor Details

#initialize(store: MemoryStore.new) ⇒ Tracker

Returns a new instance of Tracker.

Parameters:

  • store (#push, #select, #all, #clear!, #size) (defaults to: MemoryStore.new)

    pluggable event storage



14
15
16
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 14

def initialize(store: MemoryStore.new)
  @store = store
end

Instance Method Details

#clear!void

This method returns an undefined value.

Remove all events from the store.



93
94
95
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 93

def clear!
  @store.clear!
end

#count(**filters) ⇒ Integer

Count stored events, optionally filtered.

Accepts the same keywords as Queryable#query (‘actor:`, `action:`, `entity_id:`, `entity_type:`, `before:`, `after:`). When called with no filters, returns the total number of events.

Parameters:

  • filters (Hash)

    optional filter criteria

Returns:

  • (Integer)

    number of matching events



84
85
86
87
88
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 84

def count(**filters)
  return @store.size if filters.empty?

  query(**filters).size
end

#diff(before, after) ⇒ Hash

Compute the diff between two hashes.

Parameters:

  • before (Hash)

    the original state

  • after (Hash)

    the new state

Returns:

  • (Hash)

    changed fields



38
39
40
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 38

def diff(before, after)
  Differ.call(before: before, after: after)
end

#eventsArray<Event>

Return all stored events.

Returns:

  • (Array<Event>)

    all events



72
73
74
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 72

def events
  @store.all
end

#history(entity_id:, entity_type: nil) ⇒ Array<Event>

Query events for a specific entity.

Parameters:

  • entity_id (String)

    identifier to filter by

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

    optional type filter

Returns:

  • (Array<Event>)

    matching events



63
64
65
66
67
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 63

def history(entity_id:, entity_type: nil)
  @store.select do |event|
    matches_entity?(event, entity_id, entity_type)
  end
end

#record(entity_id:, entity_type:, action:, **opts) ⇒ Event

Record an audit event.

Parameters:

  • entity_id (String)

    identifier of the audited entity

  • entity_type (String)

    type/class of the audited entity

  • action (Symbol)

    the action performed

  • opts (Hash)

    additional options (changes, actor, metadata)

Returns:

  • (Event)

    the recorded event



25
26
27
28
29
30
31
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 25

def record(entity_id:, entity_type:, action:, **opts)
  event = Event.new(
    entity_id: entity_id, entity_type: entity_type,
    action: action, **opts
  )
  @store.push(event)
end

#record_change(entity_id:, entity_type:, before:, after:, **opts) ⇒ Event

Diff two hashes and record the change as an :update event.

Parameters:

  • entity_id (String)

    identifier of the audited entity

  • entity_type (String)

    type/class of the audited entity

  • before (Hash)

    the original state

  • after (Hash)

    the new state

  • opts (Hash)

    additional options (actor, metadata)

Returns:

  • (Event)

    the recorded event



50
51
52
53
54
55
56
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 50

def record_change(entity_id:, entity_type:, before:, after:, **opts)
  changes = Differ.call(before: before, after: after)
  record(
    entity_id: entity_id, entity_type: entity_type,
    action: :update, changes: changes, **opts
  )
end