Module: EventSorcerer

Defined in:
lib/event_sorcerer.rb,
lib/event_sorcerer/event.rb,
lib/event_sorcerer/version.rb,
lib/event_sorcerer/aggregate.rb,
lib/event_sorcerer/event_store.rb,
lib/event_sorcerer/message_bus.rb,
lib/event_sorcerer/event_stream.rb,
lib/event_sorcerer/unit_of_work.rb,
lib/event_sorcerer/aggregate_proxy.rb,
lib/event_sorcerer/no_unit_of_work.rb,
lib/event_sorcerer/aggregate_loader.rb,
lib/event_sorcerer/event_applicator.rb,
lib/event_sorcerer/aggregate_creator.rb,
lib/event_sorcerer/argument_hashifier.rb

Overview

Public: Defines a constant for the current version number.

Defined Under Namespace

Modules: Aggregate, ArgumentHashifier, EventApplicator, NoUnitOfWork Classes: AggregateCreator, AggregateLoader, AggregateNotFound, AggregateProxy, Event, EventArgumentError, EventStore, EventStoreError, EventStream, InvalidUUID, MessageBus, UnexpectedVersionNumber, UnitOfWork, UnsetEventStore, UnsetMessageBus

Constant Summary collapse

VERSION =
'0.1.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.event_storeObject

Public: Returns the current event store. Raises UnsetEventStore if not

set.


36
37
38
# File 'lib/event_sorcerer.rb', line 36

def event_store
  @event_store || fail(UnsetEventStore)
end

.id_generatorObject

Public: Returns the current id_generator. Defaults to a SecureRandom.uuid

based generator.


47
48
49
# File 'lib/event_sorcerer.rb', line 47

def id_generator
  @id_generator ||= proc { SecureRandom.uuid }
end

.message_busObject

Public: Returns the current message bus. Raises UnsetMessageBus if not

set.


53
54
55
# File 'lib/event_sorcerer.rb', line 53

def message_bus
  @message_bus || fail(UnsetMessageBus)
end

Class Method Details

.generate_idObject

Public: Generates a new ID using the current id_generator.



41
42
43
# File 'lib/event_sorcerer.rb', line 41

def generate_id
  id_generator.call
end

.unit_of_workObject

Public: Returns the unit_of_work for the current thread. Defaults to

NoUnitOfWork.


59
60
61
# File 'lib/event_sorcerer.rb', line 59

def unit_of_work
  Thread.current[:unit_of_work] || NoUnitOfWork
end

.with_time(time) ⇒ Object

Public: Executes a block with time frozen to a given moment.

time - Time to freeze time at.

Returns value of block.



68
69
70
71
72
73
74
# File 'lib/event_sorcerer.rb', line 68

def with_time(time)
  time_travel_lock.synchronize do
    Timecop.freeze(time) do
      yield
    end
  end
end

.with_unit_of_work(unit_of_work = UnitOfWork.new, autosave = true) ⇒ Object

Public: Creates a new UnitOfWork and sets it for the current thread

within the block. Executes the work after block completion.

Returns value of block.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/event_sorcerer.rb', line 80

def with_unit_of_work(unit_of_work = UnitOfWork.new, autosave = true)
  old_unit_of_work = Thread.current[:unit_of_work]
  new_unit_of_work = Thread.current[:unit_of_work] = unit_of_work
  begin
    result = yield
    new_unit_of_work.execute_work! if autosave
  ensure
    Thread.current[:unit_of_work] = old_unit_of_work
  end

  result
end