Module: Acta
- Defined in:
- lib/acta.rb,
lib/acta/web.rb,
lib/acta/actor.rb,
lib/acta/event.rb,
lib/acta/model.rb,
lib/acta/errors.rb,
lib/acta/record.rb,
lib/acta/schema.rb,
lib/acta/command.rb,
lib/acta/current.rb,
lib/acta/handler.rb,
lib/acta/railtie.rb,
lib/acta/reactor.rb,
lib/acta/testing.rb,
lib/acta/version.rb,
lib/acta/adapters.rb,
lib/acta/projection.rb,
lib/acta/web/engine.rb,
lib/acta/reactor_job.rb,
lib/acta/testing/dsl.rb,
lib/acta/types/array.rb,
lib/acta/types/model.rb,
lib/acta/events_query.rb,
lib/acta/serializable.rb,
lib/acta/adapters/base.rb,
lib/acta/adapters/sqlite.rb,
lib/acta/web/events_query.rb,
lib/acta/adapters/postgres.rb,
lib/acta/projection_managed.rb,
lib/acta/types/encrypted_string.rb,
app/helpers/acta/web/application_helper.rb,
app/controllers/acta/web/events_controller.rb,
lib/generators/acta/install/install_generator.rb,
app/controllers/acta/web/application_controller.rb
Defined Under Namespace
Modules: Adapters, Generators, ProjectionManaged, Schema, Serializable, Testing, Types, Web
Classes: Actor, AdapterError, Command, CommandError, ConfigurationError, Current, Error, Event, EventsQuery, Handler, InvalidCommand, InvalidEvent, MissingActor, Model, Projection, ProjectionError, ProjectionWriteError, Railtie, Reactor, ReactorJob, Record, ReplayError, TruncateOrderError, UnknownEventType, VersionConflict
Constant Summary
collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.reactor_queue ⇒ Object
37
38
39
|
# File 'lib/acta.rb', line 37
def self.reactor_queue
@reactor_queue
end
|
Class Method Details
.adapter ⇒ Object
41
42
43
|
# File 'lib/acta.rb', line 41
def self.adapter
@adapter ||= Adapters.for(Record.connection)
end
|
.dispatch(event, kind: nil) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/acta.rb', line 76
def self.dispatch(event, kind: nil)
handlers.each do |event_class, registrations|
next unless event.is_a?(event_class)
registrations.each do |registration|
next if kind && registration[:kind] != kind
invoke(event, registration)
end
end
end
|
.emit(event, actor: nil, if_version: nil) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/acta.rb', line 49
def self.emit(event, actor: nil, if_version: nil)
event.actor = actor if actor
raise MissingActor, "No actor for emit of #{event.event_type} (set Acta::Current.actor or pass actor:)" if event.actor.nil?
assert_version!(event, if_version) unless if_version.nil?
ActiveSupport::Notifications.instrument("acta.event_emitted", event:, event_type: event.event_type) do
Record.transaction(requires_new: true) do
record = adapter.insert_event(record_attributes_for(event))
event.recorded_at = record.recorded_at
dispatch(event, kind: :projection)
end
dispatch(event, kind: :handler)
dispatch(event, kind: :reactor)
end
event
end
|
.events ⇒ Object
284
285
286
|
# File 'lib/acta.rb', line 284
def self.events
EventsQuery.new(adapter.fetch_records)
end
|
.handlers ⇒ Object
72
73
74
|
# File 'lib/acta.rb', line 72
def self.handlers
@handlers ||= Hash.new { |h, k| h[k] = [] }
end
|
.projection_classes ⇒ Object
150
151
152
|
# File 'lib/acta.rb', line 150
def self.projection_classes
@projection_classes ||= []
end
|
.rebuild! ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/acta.rb', line 158
def self.rebuild!
Projection.applying! { truncate_projections! }
Record.order(:id).find_each do |record|
event = events.find_by_uuid(record.uuid)
dispatch(event, kind: :projection)
rescue ProjectionError
raise
rescue StandardError => e
raise ReplayError.new(record:, original: e)
end
end
|
.register_projection(klass) ⇒ Object
154
155
156
|
# File 'lib/acta.rb', line 154
def self.register_projection(klass)
projection_classes << klass unless projection_classes.include?(klass)
end
|
.reset_adapter! ⇒ Object
45
46
47
|
# File 'lib/acta.rb', line 45
def self.reset_adapter!
@adapter = nil
end
|
.reset_handlers! ⇒ Object
145
146
147
148
|
# File 'lib/acta.rb', line 145
def self.reset_handlers!
@handlers = Hash.new { |h, k| h[k] = [] }
@projection_classes = []
end
|
.subscribe(event_class, handler_class, &block) ⇒ Object
68
69
70
|
# File 'lib/acta.rb', line 68
def self.subscribe(event_class, handler_class, &block)
handlers[event_class] << { handler_class:, block:, kind: handler_kind(handler_class) }
end
|
.version_of(stream_type:, stream_key:) ⇒ Object
Public: read the current high-water mark for a stream. Returns 0 for streams that have never been emitted to. Use the result with ‘Acta.emit(…, if_version: version)` for optimistic locking.
261
262
263
264
265
|
# File 'lib/acta.rb', line 261
def self.version_of(stream_type:, stream_key:)
Record
.where(stream_type: stream_type.to_s, stream_key: stream_key)
.maximum(:stream_sequence) || 0
end
|