Skip to content
Snippets Groups Projects
Commit 76cb0261 authored by John Harris's avatar John Harris
Browse files

Merge branch '4-autoregister-cqrs-handlers-fails-to-handle-sub-dirs' into 'master'

Resolve "autoregister cqrs handlers fails to handle sub dirs"

Closes #4

See merge request !7
parents f97ca2d7 bcf24e51
No related branches found
Tags 0.2.0
1 merge request!7Resolve "autoregister cqrs handlers fails to handle sub dirs"
Pipeline #20951 failed
......@@ -11,3 +11,6 @@
*.gem
results/rubocop.html
# Ignore IDE configuration
nexus_cqrs.iml
# frozen_string_literal: true
module NexusCqrs
class CommandExecutor
# @param [NexusCqrs::CommandBus] command_bus
def initialize(command_bus)
@bus = command_bus
@logger = logger
end
# Get logger instance depending on runtime environment.
def logger
defined?(Rails) && defined?(Rails.logger) ? Rails.logger : Logger.new(STDOUT)
end
def autoregister(base_class, dir = 'app/domain/commands', ignore_strings = ['.rb', 'app/domain/'])
# Iterate over the director passed and find all ruby files, removing unwanted parts of the string.
Dir["#{dir}/*.rb"].each do |file|
ignore_strings.each do |i|
file.slice!(i)
end
begin
# Constantize class name to constant to force rails to autoload this class
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
puts "WARN: Tried to autoregister #{file.camelize} but class could not be found"
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
Rails.logger.error(
"WARN: A command `#{c}` tried to autoregister `#{handler_name}` but the class was not found"
)
@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
Rails.logger.error(
"WARN: A command `#{c}` tried to autoregister `#{handler_name}` but `call` method did not exist"
)
@logger.warn { "Command `#{c}` tried to autoregister `#{handler_name}` but `call` method did not exist" }
end
end
end
......
......@@ -3,16 +3,26 @@ require_relative '../../lib/nexus_cqrs'
require_relative 'get_something'
require_relative 'get_something_handler'
require_relative 'commands/get_something_else'
require_relative 'commands/get_something_else_handler'
describe NexusCqrs::CommandBus do
let(:command_bus) { NexusCqrs::CommandBus.new }
let(:command_executor) { NexusCqrs::CommandExecutor.new(command_bus) }
it "autoregistering works correctly" do
command_executor.autoregister(NexusCqrs::BaseQuery, 'spec/lib', ['.rb', 'spec/lib'])
command_executor.autoregister(NexusCqrs::BaseQuery, 'spec/lib', ['.rb', 'spec/lib/'])
TestQuery
# Note we avoid asserting TestQuery generated by QueryGeneratorSpec as test will not work standalone.
expect { GetSomething }.not_to raise_error
expect { GetSomethingHandler }.not_to raise_error
expect { Commands::GetSomethingElse }.not_to raise_error
expect { Commands::GetSomethingElseHandler }.not_to raise_error
expect(command_executor.command_bus.registered_handlers[TestQuery]).not_to(be(nil))
expect(command_executor.command_bus.registered_handlers[GetSomething]).not_to(be(nil))
expect(command_executor.command_bus.registered_handlers[GetSomething]).to(be_a_kind_of(GetSomethingHandler))
expect(command_executor.command_bus.registered_handlers[GetSomething]).not_to(be_a_kind_of(Commands::GetSomethingElseHandler))
expect(command_executor.command_bus.registered_handlers[Commands::GetSomethingElse]).not_to(be(nil))
expect(command_executor.command_bus.registered_handlers[Commands::GetSomethingElse]).to(be_a_kind_of(Commands::GetSomethingElseHandler))
end
end
# frozen_string_literal: true
module Commands
# Sample command nested in a subdir, used by CommandExecutorSpec.
class GetSomethingElse < NexusCqrs::BaseQuery
attr_reader :collection_id
def initialize(collection_id)
# TODO: Just a test
end
end
end
\ No newline at end of file
# frozen_string_literal: true
module Commands
class GetSomethingElseHandler < NexusCqrs::BaseQueryHandler
# @param [Query::GetSomethingElse] query
def call(query)
super(query)
end
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment