Class: Philiprehberger::AuditTrail::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/audit_trail/event.rb

Overview

Immutable data class representing a single audit event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Event.

Parameters:

  • entity_id (String)

    identifier of the audited entity

  • entity_type (String)

    type/class of the audited entity

  • action (Symbol)

    the action performed (:create, :update, :delete)

  • changes (Hash)

    hash of field changes

  • actor (String, nil)

    who performed the action

  • metadata (Hash)

    additional context

  • timestamp (Time)

    when the event occurred



16
17
18
19
20
21
22
23
24
# File 'lib/philiprehberger/audit_trail/event.rb', line 16

def initialize(entity_id:, entity_type:, action:, **opts)
  @entity_id = entity_id
  @entity_type = entity_type
  @action = action
  @changes = opts.fetch(:changes, {})
  @actor = opts[:actor]
  @metadata = opts.fetch(:metadata, {})
  @timestamp = opts.fetch(:timestamp, Time.now)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def action
  @action
end

#actorObject (readonly)

Returns the value of attribute actor.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def actor
  @actor
end

#changesObject (readonly)

Returns the value of attribute changes.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def changes
  @changes
end

#entity_idObject (readonly)

Returns the value of attribute entity_id.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def entity_id
  @entity_id
end

#entity_typeObject (readonly)

Returns the value of attribute entity_type.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def entity_type
  @entity_type
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def 
  @metadata
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/philiprehberger/audit_trail/event.rb', line 7

def timestamp
  @timestamp
end

Instance Method Details

#to_hHash

Returns a hash representation of the event.

Returns:

  • (Hash)

    all event attributes as a hash



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/philiprehberger/audit_trail/event.rb', line 29

def to_h
  {
    entity_id: @entity_id,
    entity_type: @entity_type,
    action: @action,
    changes: @changes,
    actor: @actor,
    metadata: @metadata,
    timestamp: @timestamp
  }
end