Class: Whodunit::Chronicles::LedgerEntry

Inherits:
Data
  • Object
show all
Defined in:
lib/whodunit/chronicles/ledger_entry.rb,
sig/whodunit/chronicles/ledger_entry.rbs

Overview

Immutable audit-domain representation of one CDC::Core::ChangeEvent.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



16
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 16

def initialize: (

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.

Returns:

  • (Object)


11
12
13
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 11

def actor
  @actor
end

#changesObject (readonly)

Returns the value of attribute changes.

Returns:

  • (Object)


12
13
14
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 12

def changes
  @changes
end

#entityString (readonly)

Returns the value of attribute entity.

Returns:

  • (String)


8
9
10
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 8

def entity
  @entity
end

#event_idString (readonly)

Returns the value of attribute event_id.

Returns:

  • (String)


4
5
6
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 4

def event_id
  @event_id
end

#identityObject (readonly)

Returns the value of attribute identity.

Returns:

  • (Object)


9
10
11
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 9

def identity
  @identity
end

#metadataHash[untyped, untyped] (readonly)

Returns the value of attribute metadata.

Returns:

  • (Hash[untyped, untyped])


13
14
15
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 13

def 
  @metadata
end

#namespaceString (readonly)

Returns the value of attribute namespace.

Returns:

  • (String)


7
8
9
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 7

def namespace
  @namespace
end

#occurred_atObject (readonly)

Returns the value of attribute occurred_at.

Returns:

  • (Object)


5
6
7
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 5

def occurred_at
  @occurred_at
end

#operationSymbol (readonly)

Returns the value of attribute operation.

Returns:

  • (Symbol)


10
11
12
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 10

def operation
  @operation
end

#payloadHash[untyped, untyped] (readonly)

Returns the value of attribute payload.

Returns:

  • (Hash[untyped, untyped])


14
15
16
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 14

def payload
  @payload
end

#recorded_atObject (readonly)

Returns the value of attribute recorded_at.

Returns:

  • (Object)


6
7
8
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 6

def recorded_at
  @recorded_at
end

Class Method Details

.from_change_event(event, clock: Time) ⇒ LedgerEntry Also known as: from_event

Build a ledger entry from a normalized CDC change event.

Parameters:

  • event (CDC::Core::ChangeEvent)

    normalized CDC event

  • clock (#now) (defaults to: Time)

    clock used for recorded_at and fallback timestamps

  • clock: (Object) (defaults to: Time)

Returns:

Raises:

  • (TypeError)

    if event is not a CDC::Core::ChangeEvent



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/whodunit/chronicles/ledger_entry.rb', line 27

def self.from_change_event(event, clock: Time)
  validate_change_event!(event)

  new(
    event_id: event_id_for(event),
    occurred_at: event.occurred_at || clock.now,
    recorded_at: clock.now,
    namespace: namespace_for(event),
    entity: entity_for(event),
    identity: event.primary_key,
    operation: event.operation,
    actor: actor_for(event),
    changes: changes_for(event),
    metadata: (event),
    payload: event.to_h
  )
end

.newObject



30
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 30

def self.new: (

Instance Method Details

#membersArray[Symbol]

Returns:

  • (Array[Symbol])


48
# File 'sig/whodunit/chronicles/ledger_entry.rbs', line 48

def members: () -> Array[Symbol]

#ordering_identityString

Build a stable ordering identity for per-record ordering.

Returns:

  • (String)

    namespace/entity/identity tuple rendered as a string



59
60
61
# File 'lib/whodunit/chronicles/ledger_entry.rb', line 59

def ordering_identity
  [namespace, entity, identity].compact.join(':')
end

#to_hHash<Symbol, Object>

Convert this entry to a Hash keyed by attribute name.

Returns:

  • (Hash<Symbol, Object>)

    entry attributes



52
53
54
# File 'lib/whodunit/chronicles/ledger_entry.rb', line 52

def to_h
  members.to_h { |name| [name, public_send(name)] }
end