class EdgycircleToolbox::CQRS::CommandBus

Attributes

command_builder[R]
container[R]

Public Class Methods

new() click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 10
def initialize
  @container = Dry::Container.new
  @command_builder = CommandBuilder.new
end

Public Instance Methods

build(original_parameters, context = nil) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 33
def build(original_parameters, context = nil)
  result = CommandResult.new
  validation = command_builder.validate(original_parameters)

  if validation.failure?
    build_validation_errors(validation).each { |error| result.add_error(error) }
  else
    validated_parameters = validation.output
    command = command_builder.build(validated_parameters, context)
    result.set_command(command)
  end

  result
end
decorate_command(command_class, decorator_class) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 23
def decorate_command(command_class, decorator_class)
  command_builder.decorate(command_class, decorator_class)
end
register(command, handler) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 15
def register(command, handler)
  command_builder.register(command)

  container.namespace(:handlers) do
    register(command, handler)
  end
end
submit(command) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 27
def submit(command)
  result = container.resolve("handlers.#{command.class.name}").new.call(command)
  result.events.each { |event| MessageBus.publish(event) }
  result
end

Private Instance Methods

build_validation_errors(validation) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 55
def build_validation_errors(validation)
  validation.messages(full: true).map do |attribute, messages|
    messages.map { |message| ValidationError.new(attribute, message) }
  end.flatten
end
command_class_for(name) click to toggle source
# File lib/edgycircle_toolbox/cqrs/command_bus.rb, line 51
def command_class_for(name)
  container.resolve("commands.#{name}")
end