Class: Acta::EventsQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/acta/events_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ EventsQuery

Returns a new instance of EventsQuery.



5
6
7
# File 'lib/acta/events_query.rb', line 5

def initialize(scope)
  @scope = scope
end

Instance Method Details

#allObject

Iterates the full scope through the upcaster pipeline with a SINGLE shared context across every record, matching ‘Acta.rebuild!` semantics. Stateful upcasters (those that resolve later events from state seeded by earlier ones) depend on this. Single-record lookups (`find_by_uuid`, `first`, `last`) deliberately use a fresh context —there is no prior history to seed it with — and may produce incomplete output for stateful upcasters. See `docs/upcasters.md`.



28
29
30
31
# File 'lib/acta/events_query.rb', line 28

def all
  context = Upcaster::Context.new
  @scope.flat_map { |record| upcast_and_hydrate(record, context) }
end

#countObject



33
34
35
# File 'lib/acta/events_query.rb', line 33

def count
  @scope.count
end

#eachObject



37
38
39
# File 'lib/acta/events_query.rb', line 37

def each(&)
  all.each(&)
end

#find_by_uuid(uuid) ⇒ Object



17
18
19
# File 'lib/acta/events_query.rb', line 17

def find_by_uuid(uuid)
  upcast_and_hydrate_one(@scope.find_by(uuid:))
end

#firstObject



13
14
15
# File 'lib/acta/events_query.rb', line 13

def first
  upcast_and_hydrate_one(@scope.first)
end

#for_stream(type:, key:) ⇒ Object



43
44
45
46
47
48
# File 'lib/acta/events_query.rb', line 43

def for_stream(type:, key:)
  filtered = @scope
               .where(stream_type: type.to_s, stream_key: key)
               .reorder(:stream_sequence)
  self.class.new(filtered)
end

#lastObject



9
10
11
# File 'lib/acta/events_query.rb', line 9

def last
  upcast_and_hydrate_one(@scope.last)
end

#upcast_and_hydrate(record, context) ⇒ Object

Run a single record through the upcaster pipeline and hydrate every output into a typed Acta::Event. Returns an Array (length 0..N) —callers that expect one event (the historic shape) should use the find_by_uuid/first/last helpers above, which apply a fresh context per call and unwrap to a single event (raising if upcasters drop or fan out, since those shapes aren’t meaningful for one-record reads).

Acta.rebuild! supplies a single shared context for the full pass.



58
59
60
# File 'lib/acta/events_query.rb', line 58

def upcast_and_hydrate(record, context)
  Upcaster.upcast(record, context).map { |view| hydrate(view) }
end