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/array_type.rb,
lib/acta/model_type.rb,
lib/acta/projection.rb,
lib/acta/web/engine.rb,
lib/acta/reactor_job.rb,
lib/acta/testing/dsl.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, ArrayType, Command, CommandError, ConfigurationError, Current, Error, Event, EventsQuery, Handler, InvalidCommand, InvalidEvent, MissingActor, Model, ModelType, Projection, ProjectionError, ProjectionWriteError, Railtie, Reactor, ReactorJob, Record, ReplayError, TruncateOrderError, UnknownEventType, VersionConflict

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.adapterObject



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

def self.adapter
  @adapter ||= Adapters.for(Record.connection)
end

.dispatch(event, kind: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/acta.rb', line 64

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

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/acta.rb', line 37

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

.eventsObject



269
270
271
# File 'lib/acta.rb', line 269

def self.events
  EventsQuery.new(adapter.fetch_records)
end

.handlersObject



60
61
62
# File 'lib/acta.rb', line 60

def self.handlers
  @handlers ||= Hash.new { |h, k| h[k] = [] }
end

.projection_classesObject



135
136
137
# File 'lib/acta.rb', line 135

def self.projection_classes
  @projection_classes ||= []
end

.rebuild!Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/acta.rb', line 143

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



139
140
141
# File 'lib/acta.rb', line 139

def self.register_projection(klass)
  projection_classes << klass unless projection_classes.include?(klass)
end

.reset_adapter!Object



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

def self.reset_adapter!
  @adapter = nil
end

.reset_handlers!Object



130
131
132
133
# File 'lib/acta.rb', line 130

def self.reset_handlers!
  @handlers = Hash.new { |h, k| h[k] = [] }
  @projection_classes = []
end

.subscribe(event_class, handler_class, &block) ⇒ Object



56
57
58
# File 'lib/acta.rb', line 56

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.



246
247
248
249
250
# File 'lib/acta.rb', line 246

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