class Realm::EventFactory
Public Class Methods
new(events_module)
click to toggle source
# 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
Public Instance Methods
create_event(event_type, correlate: nil, cause: nil, **attributes)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
collect_event_classes(root_module)
click to toggle source
# File lib/realm/event_factory.rb, line 27 def collect_event_classes(root_module) root_module_str = root_module.to_s root_module.constants.each_with_object({}) do |const_sym, all| const = root_module.const_get(const_sym) next unless const.is_a?(Module) && const.to_s.start_with?(root_module_str) all[const.type] = const if const < Event all.merge!(collect_event_classes(const)) end end
enhance_head(head, correlate:, cause:)
click to toggle source
# File lib/realm/event_factory.rb, line 38 def enhance_head(head, correlate:, cause:) head[:correlation_id] = correlate.head.correlation_id if correlate if cause.is_a?(Event) head[:cause_event_id] = cause.head.id head[:correlation_id] ||= cause.head.correlation_id elsif cause head[:cause] = cause end head end