Module: Junkfood::Ceb::Bus

Extended by:
ActiveSupport::Concern
Included in:
BaseCommand
Defined in:
lib/junkfood/ceb/bus.rb

Overview

Module to implement the bus framework for Ceb.

Notes for implementers:

  • Executors MAY reraise all raised errors

    • This is for problems in the “process” of executing commands,

    • not actual errors in the command’s execution itself.

  • Messages SHOULD handle their errors, publishing Error Events when a command fails.

  • Callers of this method SHOULD check the command to see if validation errors occurred.

Examples:


class WidgetCreateCommand < Junkfood::Ceb::BaseCommand
  def perform
    # Do Stuff.
  end
end

class ApplicationController < ActionController::Base
  include Junkfood::Ceb::Bus
  acts_as_command_bus
end

class WidgetController < ApplicationController
  def create
    # params['widget'] is our posted form data the same as how Rails
    # does it for ActiveModel. This eliminates hassle for
    # custom parsing, data handling.

    # Create a command (WidgetCreateCommand) and puts it onto the bus
    # for execution. The command created is returned along with
    # published events by the command.
    command, events = send_command 'widget_create', params['widget']

    if command.valid?
      # The command is correctly formed, so the backend executors will
      # execute it in the future (or already has executed it).
    end

    # Commands SHOULD return the Event, or list of Events,
    # that the command published when its `perform` method was called.
    case result
    when Junkfood::Ceb::BaseEvent
      # The single published event.
      # Can assume that the command was executed.
    when Array
      # An array of Events published.
      # Can assume that the command was executed.
    when nil, false
      # No data was passed back. Nothing can be assumed about the
      # execution of the command.
    end
  end
end

See Also:

Defined Under Namespace

Modules: ClassMethods