Class: Acta::Upcaster::View

Inherits:
Object
  • Object
show all
Defined in:
lib/acta/upcaster.rb

Overview

In-memory record shape passed to upcaster blocks. Wraps a backing ‘Acta::Record` (the row as stored) with optional overlays for `event_type`, `event_version`, and `payload` — upcasters mutate only the overlays, never the stored row.

Constant Summary collapse

ENVELOPE_FIELDS =
%i[id uuid occurred_at recorded_at actor_type actor_id source metadata stream_type stream_key stream_sequence].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, event_type: nil, event_version: nil, payload: nil) ⇒ View

Returns a new instance of View.



106
107
108
109
110
111
# File 'lib/acta/upcaster.rb', line 106

def initialize(base, event_type: nil, event_version: nil, payload: nil)
  @base = base
  @event_type = event_type || base.event_type
  @event_version = event_version || base.event_version
  @payload = payload || (base.payload || {})
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



104
105
106
# File 'lib/acta/upcaster.rb', line 104

def base
  @base
end

#event_typeObject (readonly)

Returns the value of attribute event_type.



104
105
106
# File 'lib/acta/upcaster.rb', line 104

def event_type
  @event_type
end

#event_versionObject (readonly)

Returns the value of attribute event_version.



104
105
106
# File 'lib/acta/upcaster.rb', line 104

def event_version
  @event_version
end

#payloadObject (readonly)

Returns the value of attribute payload.



104
105
106
# File 'lib/acta/upcaster.rb', line 104

def payload
  @payload
end

Instance Method Details

#upcast_to(type: nil, payload: nil, schema_version:) ⇒ Object

Produce a new View with the supplied attributes overlaid. ‘type` defaults to the current event_type; `payload` defaults to the current payload; `schema_version` is required and replaces `event_version`. The original (and the underlying Record) are untouched.

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
# File 'lib/acta/upcaster.rb', line 122

def upcast_to(type: nil, payload: nil, schema_version:)
  raise ArgumentError, "schema_version required" if schema_version.nil?

  View.new(
    base,
    event_type: type || @event_type,
    event_version: schema_version,
    payload: payload || @payload
  )
end