Class: Realm::EventFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/realm/event_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(events_module) ⇒ EventFactory

Returns a new instance of EventFactory.



5
6
7
8
# File 'lib/realm/event_factory.rb', line 5

def initialize(events_module)
  @events_module = events_module
  @event_class_map = collect_event_classes(events_module)
end

Instance Method Details

#create_event(event_type, correlate: nil, cause: nil, **attributes) ⇒ Object



10
11
12
13
14
15
# File 'lib/realm/event_factory.rb', line 10

def create_event(event_type, correlate: nil, cause: nil, **attributes)
  head = enhance_head(attributes.fetch(:head, {}), correlate: correlate, cause: cause)
  body = attributes.fetch(:body, attributes.except(:head))

  event_class_for(event_type).new(head: head, body: body)
end

#event_class_for(event_type) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/realm/event_factory.rb', line 17

def event_class_for(event_type)
  return event_type if event_type.respond_to?(:new)

  @event_class_map.fetch(event_type.to_s) do
    raise EventClassMissing.new(event_type, @events_module)
  end
end