Class: NexusCqrs::CommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_cqrs/command_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_bus) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.

Parameters:



6
7
8
9
# File 'lib/nexus_cqrs/command_executor.rb', line 6

def initialize(command_bus)
  @bus = command_bus
  @logger = logger
end

Instance Method Details

#autoregister(base_class, dir = 'app/domain/commands', ignore_strings = ['.rb', 'app/domain/']) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nexus_cqrs/command_executor.rb', line 16

def autoregister(base_class, dir = 'app/domain/commands', ignore_strings = ['.rb', 'app/domain/'])
  if defined?(Rails)
    # Iterate over the directory passed and find all ruby files.
    Dir["#{dir}/**/*.rb"].each do |file|
      # Constantize class name to constant to force rails to autoload this class.
      # Note that autoloading won't work when testing this gem standalone, but this does trigger the necessary
      # loading when in a rails environment.
      ignore_strings.each do |i|
        file.slice!(i)
      end
      @logger.debug { "Attempting constantize of #{file}" }
      file.camelize.constantize
    rescue NameError => e
      @logger.warn { "Failed autoregister #{file.camelize}, received NameError: #{e}" }
    end
  end

  ObjectSpace.each_object(Class).select { |klass| klass < base_class }.each do |c|
    @logger.debug { "Attempting auto registration of #{c}" }
    handler_name = (c.to_s + "Handler")
    begin
      handler = handler_name.constantize.new
    rescue NameError
      @logger.warn { "Command `#{c}` tried to autoregister `#{handler_name}` but the class was not found" }
      next
    end

    if handler.respond_to?(:call)
      register_command(c, handler)
    else
      @logger.warn { "Command `#{c}` tried to autoregister `#{handler_name}` but `call` method did not exist" }
    end
  end
end

#command_busNexusCqrs::CommandBus

Returns the bus used by this executor

Returns:



54
55
56
# File 'lib/nexus_cqrs/command_executor.rb', line 54

def command_bus
  @bus
end

#configure_handlers(&block) ⇒ Object

Configuration method for allowing handlers to be registered in bulk

Examples:

Registering all queries

executor.configure_handlers do |executor|
  executor.autoregister(NexusCqrs::BaseQuery, 'app/domain/queries')
end

Registering a query manually

executor.configure_handlers do |executor|
  executor.register_command(NexusCqrs::BaseQuery, NexusCqrs::BaseQueryHandler)
end

Parameters:

  • block (Proc)

    Block to execute to register handlers

See Also:



95
96
97
98
# File 'lib/nexus_cqrs/command_executor.rb', line 95

def configure_handlers(&block)
  @register_handlers = block
  @register_handlers.call(self)
end

#execute(message) ⇒ Object

Executes a specific handler on the bus, passing in the provided message (Query/Command)

Parameters:



61
62
63
# File 'lib/nexus_cqrs/command_executor.rb', line 61

def execute(message)
  @bus.call(message)
end

#loggerObject

Get logger instance depending on runtime environment.



12
13
14
# File 'lib/nexus_cqrs/command_executor.rb', line 12

def logger
  defined?(Rails) && defined?(Rails.logger) ? Rails.logger : Logger.new(STDOUT)
end

#register_command(message, handler) ⇒ Object

Helper method for registering a handler on the bus



70
71
72
# File 'lib/nexus_cqrs/command_executor.rb', line 70

def register_command(message, handler)
  @bus.register(message, handler)
end

#reregister_handlersObject

Clears all handlers from the bus and invokes the block passed to ‘configure_handlers` to re-register all the handlers again. This can be useful in tests - as we don’t want state to persist across handlers



76
77
78
79
80
81
# File 'lib/nexus_cqrs/command_executor.rb', line 76

def reregister_handlers
  return if @register_handlers.nil?

  @bus.clear_handlers
  @register_handlers.call(self)
end