From e54c007af9e8469ff9856439a0a2b2e322766d3f Mon Sep 17 00:00:00 2001 From: dean <dean.lovett@nexusmods.com> Date: Tue, 25 Aug 2020 11:32:18 +0100 Subject: [PATCH] fix: added namespace support and improved testing --- .gitignore | 2 +- Gemfile | 2 +- Gemfile.lock | 78 ++++++++++++++++++- README.md | 8 ++ .../nexus_cqrs/command_generator.rb | 15 ++-- lib/generators/nexus_cqrs/query_generator.rb | 22 +++++- lib/nexus_cqrs/base_command_handler.rb | 2 + lib/nexus_cqrs/base_query_handler.rb | 2 + lib/nexus_cqrs/command_executor.rb | 2 +- lib/nexus_cqrs/version.rb | 2 +- spec/lib/generators/command_generator_spec.rb | 42 ++++++++++ spec/lib/generators/query_generator_spec.rb | 42 ++++++++++ 12 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 spec/lib/generators/command_generator_spec.rb create mode 100644 spec/lib/generators/query_generator_spec.rb diff --git a/.gitignore b/.gitignore index 4c16e05..ea360d7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ /spec/reports/ /tmp/ /.gem - +/spec/lib/tmp *.gem diff --git a/Gemfile b/Gemfile index 4088afa..48e3200 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,3 @@ -source "https://rubygems.org" source 'https://rubygems.org' # Specify your gem's dependencies in cqrs-core.gemspec @@ -8,4 +7,5 @@ gem 'rubocop' gem 'rubocop-shopify', "~> 1.0.4", require: false gem 'rspec' +gem "generator_spec" gem 'thread_safe' diff --git a/Gemfile.lock b/Gemfile.lock index 615f3a7..0b3fd63 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,17 +1,73 @@ PATH remote: . specs: - nexus_cqrs (0.0.6) + nexus_cqrs (0.0.8) GEM remote: https://rubygems.org/ specs: + actionpack (6.0.3.2) + actionview (= 6.0.3.2) + activesupport (= 6.0.3.2) + rack (~> 2.0, >= 2.0.8) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actionview (6.0.3.2) + activesupport (= 6.0.3.2) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activesupport (6.0.3.2) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + zeitwerk (~> 2.2, >= 2.2.2) ast (2.4.1) + builder (3.2.4) + concurrent-ruby (1.1.7) + crass (1.0.6) diff-lcs (1.4.4) + erubi (1.9.0) + generator_spec (0.9.4) + activesupport (>= 3.0.0) + railties (>= 3.0.0) + i18n (1.8.5) + concurrent-ruby (~> 1.0) + loofah (2.6.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + method_source (1.0.0) + mini_portile2 (2.4.0) + minitest (5.14.1) + multi_json (1.15.0) + net-scp (3.0.0) + net-ssh (>= 2.6.5, < 7.0.0) + net-ssh (6.1.0) + net-telnet (0.1.1) + nokogiri (1.10.10) + mini_portile2 (~> 2.4.0) parallel (1.19.2) parser (2.7.1.4) ast (~> 2.4.1) + rack (2.2.3) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (6.0.3.2) + actionpack (= 6.0.3.2) + activesupport (= 6.0.3.2) + method_source + rake (>= 0.8.7) + thor (>= 0.20.3, < 2.0) rainbow (3.0.0) + rake (13.0.1) regexp_parser (1.7.1) rexml (3.2.4) rspec (3.9.0) @@ -23,6 +79,9 @@ GEM rspec-expectations (3.9.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) + rspec-its (1.3.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) rspec-mocks (3.9.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) @@ -41,17 +100,34 @@ GEM rubocop-shopify (1.0.4) rubocop (>= 0.85, < 0.87) ruby-progressbar (1.10.1) + serverspec (2.41.5) + multi_json + rspec (~> 3.0) + rspec-its + specinfra (~> 2.72) + sfl (2.3) + specinfra (2.82.19) + net-scp + net-ssh (>= 2.7) + net-telnet (= 0.1.1) + sfl + thor (1.0.1) thread_safe (0.3.6) + tzinfo (1.2.7) + thread_safe (~> 0.1) unicode-display_width (1.7.0) + zeitwerk (2.4.0) PLATFORMS ruby DEPENDENCIES + generator_spec nexus_cqrs! rspec rubocop rubocop-shopify (~> 1.0.4) + serverspec thread_safe BUNDLED WITH diff --git a/README.md b/README.md index 00ce539..fb4a2be 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,11 @@ Generators can be used to aide in the creation of Commands and Queries: Once installed, a CommandBus is required to control the flow of Commands and/or Queries: +## Development + +To contribute to this gem, simple pull the repository, run `bundle install` and run tests: + +```shell script + bundle exec rspec + bundle exec rubocop +``` \ No newline at end of file diff --git a/lib/generators/nexus_cqrs/command_generator.rb b/lib/generators/nexus_cqrs/command_generator.rb index 10b5daf..18a565e 100644 --- a/lib/generators/nexus_cqrs/command_generator.rb +++ b/lib/generators/nexus_cqrs/command_generator.rb @@ -5,15 +5,16 @@ module NexusCqrs source_root File.expand_path('templates', __dir__) def copy_command_file - # TODO: add namespace support - full_name = file_name + file_path = class_name.underscore - template('command.rb', "app/domain/commands/#{full_name}.rb") - template('command_handler.rb', "app/domain/commands/#{full_name}_handler.rb") + template('command.rb', "app/domain/commands/#{file_path}.rb") + template('command_handler.rb', "app/domain/commands/#{file_path}_handler.rb") - register_command(full_name) + register_command(class_name) end + private + def register_command(full_name) handler_config = 'config/initializers/register_cqrs_handlers.rb' @@ -21,9 +22,9 @@ module NexusCqrs template('register_cqrs_handlers.rb', handler_config) end - code_to_inject = "$QUERY_EXECUTOR.register_command(#{full_name}, #{full_name}Handler)" + code_to_inject = "$COMMAND_EXECUTOR.register_command(#{full_name}, #{full_name}Handler)" - inject_into_file(handler_config, code_to_inject, after: '# Register Commands') + inject_into_file(handler_config, code_to_inject, after: "# Register Commands\n") end end end diff --git a/lib/generators/nexus_cqrs/query_generator.rb b/lib/generators/nexus_cqrs/query_generator.rb index a48cd46..3486077 100644 --- a/lib/generators/nexus_cqrs/query_generator.rb +++ b/lib/generators/nexus_cqrs/query_generator.rb @@ -5,8 +5,26 @@ module NexusCqrs source_root File.expand_path('templates', __dir__) def copy_query_file - template('query.rb', "app/domain/queries/#{file_name}.rb") - template('query_handler.rb', "app/domain/queries/#{file_name}_handler.rb") + file_path = class_name.underscore + + template('query.rb', "app/domain/queries/#{file_path}.rb") + template('query_handler.rb', "app/domain/queries/#{file_path}_handler.rb") + + register_query(class_name) + end + + private + + def register_query(full_name) + handler_config = 'config/initializers/register_cqrs_handlers.rb' + + unless File.exist?('config/initializers/register_cqrs_handlers.rb') + template('register_cqrs_handlers.rb', handler_config) + end + + code_to_inject = "$QUERY_EXECUTOR.register_command(#{full_name}, #{full_name}Handler)" + + inject_into_file(handler_config, code_to_inject, after: "# Register Queries\n") end end end diff --git a/lib/nexus_cqrs/base_command_handler.rb b/lib/nexus_cqrs/base_command_handler.rb index d59af4d..1d79f1f 100644 --- a/lib/nexus_cqrs/base_command_handler.rb +++ b/lib/nexus_cqrs/base_command_handler.rb @@ -1,4 +1,6 @@ module NexusCqrs class BaseCommandHandler + def call(command) + end end end diff --git a/lib/nexus_cqrs/base_query_handler.rb b/lib/nexus_cqrs/base_query_handler.rb index 2993923..3fc7759 100644 --- a/lib/nexus_cqrs/base_query_handler.rb +++ b/lib/nexus_cqrs/base_query_handler.rb @@ -1,4 +1,6 @@ module NexusCqrs class BaseQueryHandler + def call(command) + end end end diff --git a/lib/nexus_cqrs/command_executor.rb b/lib/nexus_cqrs/command_executor.rb index 97860c3..02a53f6 100644 --- a/lib/nexus_cqrs/command_executor.rb +++ b/lib/nexus_cqrs/command_executor.rb @@ -12,7 +12,7 @@ module NexusCqrs def register_command(klass, handler) Rails.logger.debug("Registered #{klass} to #{handler}") - @bus.register(klass, handler) + @bus.register(klass.class, handler) end private diff --git a/lib/nexus_cqrs/version.rb b/lib/nexus_cqrs/version.rb index 08aaaa3..74ce5ad 100644 --- a/lib/nexus_cqrs/version.rb +++ b/lib/nexus_cqrs/version.rb @@ -1,3 +1,3 @@ module NexusCqrs - VERSION = '0.0.6' + VERSION = '0.0.8' end diff --git a/spec/lib/generators/command_generator_spec.rb b/spec/lib/generators/command_generator_spec.rb new file mode 100644 index 0000000..6a32fcf --- /dev/null +++ b/spec/lib/generators/command_generator_spec.rb @@ -0,0 +1,42 @@ +require "generator_spec" +require_relative '../../../lib/generators/nexus_cqrs/command_generator' + +describe NexusCqrs::CommandGenerator, type: :generator do + destination File.expand_path("../../tmp", __FILE__) + + before(:all) do + prepare_destination + run_generator ["TestCommand"] + end + + it "creates the registration config file for handlers" do + assert_file "config/initializers/register_cqrs_handlers.rb" + assert_file "app/domain/commands/test_command.rb" + assert_file "app/domain/commands/test_command_handler.rb" + end + + it "creates the registration config file for handlers" do + line = "$COMMAND_EXECUTOR.register_command(TestCommand, TestCommandHandler)" + base_path = File.expand_path("../../tmp", __FILE__) + expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line)) + end + + context 'namespacing class names' do + before(:all) do + prepare_destination + run_generator ["Test::TestCommand"] + end + + it "creates the registration config file for handlers" do + assert_file "config/initializers/register_cqrs_handlers.rb" + assert_file "app/domain/commands/test/test_command.rb" + assert_file "app/domain/commands/test/test_command_handler.rb" + end + + it "creates the registration config file for handlers" do + line = "$COMMAND_EXECUTOR.register_command(Test::TestCommand, Test::TestCommandHandler)" + base_path = File.expand_path("../../tmp", __FILE__) + expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line)) + end + end +end diff --git a/spec/lib/generators/query_generator_spec.rb b/spec/lib/generators/query_generator_spec.rb new file mode 100644 index 0000000..3022312 --- /dev/null +++ b/spec/lib/generators/query_generator_spec.rb @@ -0,0 +1,42 @@ +require "generator_spec" +require_relative '../../../lib/generators/nexus_cqrs/query_generator' + +describe NexusCqrs::QueryGenerator, type: :generator do + destination File.expand_path("../../tmp", __FILE__) + + before(:all) do + prepare_destination + run_generator ["TestQuery"] + end + + it "creates the registration config file for handlers" do + assert_file "config/initializers/register_cqrs_handlers.rb" + assert_file "app/domain/queries/test_query.rb" + assert_file "app/domain/queries/test_query_handler.rb" + end + + it "creates the registration config file for handlers" do + line = "$QUERY_EXECUTOR.register_command(TestQuery, TestQueryHandler)" + base_path = File.expand_path("../../tmp", __FILE__) + expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line)) + end + + context 'namespacing class names' do + before(:all) do + prepare_destination + run_generator ["Test::TestQuery"] + end + + it "creates the registration config file for handlers" do + assert_file "config/initializers/register_cqrs_handlers.rb" + assert_file "app/domain/queries/test/test_query.rb" + assert_file "app/domain/queries/test/test_query_handler.rb" + end + + it "creates the registration config file for handlers" do + line = "$QUERY_EXECUTOR.register_command(Test::TestQuery, Test::TestQueryHandler)" + base_path = File.expand_path("../../tmp", __FILE__) + expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line)) + end + end +end -- GitLab