Class: NexusCqrs::CommandExecutor
- Inherits:
-
Object
- Object
- NexusCqrs::CommandExecutor
- Defined in:
- lib/nexus_cqrs/command_executor.rb
Instance Method Summary collapse
- #autoregister(base_class, dir = 'app/domain/commands', ignore_strings = ['.rb', 'app/domain/']) ⇒ Object
-
#command_bus ⇒ NexusCqrs::CommandBus
Returns the bus used by this executor.
-
#configure_handlers(&block) ⇒ Object
Configuration method for allowing handlers to be registered in bulk.
-
#execute(message) ⇒ Object
Executes a specific handler on the bus, passing in the provided message (Query/Command).
-
#initialize(command_bus) ⇒ CommandExecutor
constructor
A new instance of CommandExecutor.
-
#logger ⇒ Object
Get logger instance depending on runtime environment.
-
#register_command(message, handler) ⇒ Object
Helper method for registering a handler on the bus.
-
#reregister_handlers ⇒ Object
Clears all handlers from the bus and invokes the block passed to ‘configure_handlers` to re-register all the handlers again.
Constructor Details
#initialize(command_bus) ⇒ CommandExecutor
Returns a new instance of CommandExecutor.
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_bus ⇒ NexusCqrs::CommandBus
Returns the bus used by this executor
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
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)
61 62 63 |
# File 'lib/nexus_cqrs/command_executor.rb', line 61 def execute() @bus.call() end |
#logger ⇒ Object
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(, handler) @bus.register(, handler) end |
#reregister_handlers ⇒ Object
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 |