Class: Philiprehberger::AuditTrail::Tracker
- Inherits:
-
Object
- Object
- Philiprehberger::AuditTrail::Tracker
- 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
Instance Method Summary collapse
-
#clear! ⇒ void
Remove all events from the store.
-
#count(**filters) ⇒ Integer
Count stored events, optionally filtered.
-
#diff(before, after) ⇒ Hash
Compute the diff between two hashes.
-
#events ⇒ Array<Event>
Return all stored events.
-
#history(entity_id:, entity_type: nil) ⇒ Array<Event>
Query events for a specific entity.
-
#initialize(store: MemoryStore.new) ⇒ Tracker
constructor
A new instance of Tracker.
-
#record(entity_id:, entity_type:, action:, **opts) ⇒ Event
Record an audit event.
-
#record_change(entity_id:, entity_type:, before:, after:, **opts) ⇒ Event
Diff two hashes and record the change as an :update event.
Methods included from Summarizable
Methods included from Exportable
Methods included from Batchable
Methods included from Prunable
Methods included from Queryable
Constructor Details
#initialize(store: MemoryStore.new) ⇒ Tracker
Returns a new instance of Tracker.
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.
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.
38 39 40 |
# File 'lib/philiprehberger/audit_trail/tracker.rb', line 38 def diff(before, after) Differ.call(before: before, after: after) end |
#events ⇒ Array<Event>
Return all stored 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.
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.
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.
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 |