diff --git a/.gitignore b/.gitignore index 4c16e05fb8ac98c38afb3a0fd7dedc811697d11a..ea360d7e259bdfbc14c675837cedda4816d06b4c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ /spec/reports/ /tmp/ /.gem - +/spec/lib/tmp *.gem diff --git a/Gemfile b/Gemfile index 4088afaf008136923c9fe1a1e30878cce5a669a9..48e32009c6ec9a08809b4dc8a7d142d84eac206a 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 615f3a76654ecc9044fab34abf256f417b6aa077..0b3fd6344bbd90cf0f2c60626d43f1f34a3a543b 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 00ce539333d97482d3e3114aeac934d102bafcde..fb4a2beb4dcbf8f2ce3d37fbd34ab33ce00287ce 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 10b5daf93123728699036883d979a4069129a74e..18a565eca88a867aefca17f82a2150eb0715655a 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 a48cd468d9a18c5affca543d67e2c5900a757c63..348607768c193f4b302db3d0afe338cc5a12102a 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 d59af4d3ac65c6066c50cebc1aec700c81b5f1e6..1d79f1f4d05ffaaf760c74055d427a9ec38e5e5b 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 29939236a7f2fa433324cb40dc394e2473236554..3fc77597e1404e4f587ec67765f401ff63b362f2 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 97860c3c42a22497c2c26304f2aca1ab80bc966d..02a53f62dfb6ac8194733a58daaa1440b941e70b 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 08aaaa3351131d99a69e41686639c9a9a47076a2..74ce5ad0867fe2739ae3d87f6b4b801760753cf8 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 0000000000000000000000000000000000000000..6a32fcf8e1aefee9f91cf2fe05a2b7cbbc63c8c5 --- /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 0000000000000000000000000000000000000000..3022312d7f4f8d820a4e9d3850081994fe09eb1a --- /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