class AggregateRoot::DefaultApplyStrategy

Attributes

on_methods[R]
strict[R]

Public Class Methods

new(strict: true) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 7
def initialize(strict: true)
  @strict = strict
end

Public Instance Methods

call(aggregate, event) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 11
def call(aggregate, event)
  name = handler_name(aggregate, event)
  if aggregate.respond_to?(name, true)
    aggregate.method(name).call(event)
  else
    raise MissingHandler.new("Missing handler method #{name} on aggregate #{aggregate.class}") if strict
  end
end

Private Instance Methods

apply_handler_name(event_type) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 30
def apply_handler_name(event_type)
  "apply_#{Transform.to_snake_case(event_type(event_type))}"
end
event_type(event_type) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 34
def event_type(event_type)
  event_type.split(%r{::|\.}).last
end
handler_name(aggregate, event) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 22
def handler_name(aggregate, event)
  on_dsl_handler_name(aggregate, event.event_type) || apply_handler_name(event.event_type)
end
on_dsl_handler_name(aggregate, event_type) click to toggle source
# File lib/aggregate_root/default_apply_strategy.rb, line 26
def on_dsl_handler_name(aggregate, event_type)
  aggregate.class.on_methods[event_type] if aggregate.class.respond_to?(:on_methods)
end