nexus_cqrs 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24bafc3e27cbf1ef65d1e507bdb2798b67283c92c64b274bbf8818cfffa9618a
4
- data.tar.gz: e653d3e7b76bc2173a780cf6128974afaecaba8278c0f39e56527ae442395cff
3
+ metadata.gz: c4e6e4a145007fe8658503e9745e3be716436749d9bea2c714bca1d47b1ea4ab
4
+ data.tar.gz: aedbcb7c36185faf7d546e3ea5c268cba6642d0880a0a2c2cdf612e761f5096c
5
5
  SHA512:
6
- metadata.gz: 38a848d5da2a2a61116f1bb2f04df2fae1c0cb368e6482854e0f7896a9bfcd0b2baab39e58c223c2b1f5a1a0c62bc028c0dddf1d2daccebc7268010e9506559c
7
- data.tar.gz: 6f7b0c32343f39b5b3eb61b9c4afb5cc338bbce81eeb5d998b0d0e1293fe1fa35984264f86ed8fbc8d1a0a29106602566a0ee00096951b4d54fc63ddb3d9f3d7
6
+ metadata.gz: 83c240bd9f056d0d82e5437f52c45527be6092e83f6356c765bfa127153d8d5c5c660437598707fb470879606ede7c9fc1a28f11e42355401b177c29b37e7c45
7
+ data.tar.gz: 0dab29f1d52e3f4110cfb948ffbe425c342f682c01f1945ceced2385aa022201e84c7025b1cc7eb633110029efdf4e7b5e0366baf2b7466183a164a040df5ff3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nexus_cqrs (0.1.0)
4
+ nexus_cqrs (0.3.0)
5
5
  generator_spec
6
6
  ibsciss-middleware
7
7
  thread_safe
@@ -2,7 +2,7 @@ Description:
2
2
  Generates a new Query object, along with the QueryHandler
3
3
 
4
4
  Example:
5
- rails generate cqrs:query GetUserById
5
+ rails generate cqrs:query GetUserById --permission
6
6
 
7
7
  This will create:
8
8
  app/domain/queries/get_user_by_id.rb
@@ -1,30 +1,30 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module NexusCqrs
4
5
  class CommandGenerator < Rails::Generators::NamedBase
5
6
  source_root File.expand_path('templates', __dir__)
7
+ class_option :permission, type: :string, default: nil
6
8
 
7
9
  def copy_command_file
8
10
  file_path = class_name.underscore
9
11
 
12
+ @permission = options['permission']
13
+
10
14
  template('command.rb', "app/domain/commands/#{file_path}.rb")
11
15
  template('command_handler.rb', "app/domain/commands/#{file_path}_handler.rb")
12
16
 
13
- register_command(class_name)
17
+ register_command
14
18
  end
15
19
 
16
20
  private
17
21
 
18
- def register_command(full_name)
22
+ def register_command
19
23
  handler_config = 'config/initializers/register_cqrs_handlers.rb'
20
24
 
21
25
  unless File.exist?('config/initializers/register_cqrs_handlers.rb')
22
26
  template('register_cqrs_handlers.rb', handler_config)
23
27
  end
24
-
25
- code_to_inject = "$COMMAND_EXECUTOR.register_command(#{full_name}, #{full_name}Handler.new)\n"
26
-
27
- inject_into_file(handler_config, code_to_inject, after: "# Register Commands\n")
28
28
  end
29
29
  end
30
30
  end
@@ -1,30 +1,30 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module NexusCqrs
4
5
  class QueryGenerator < Rails::Generators::NamedBase
5
6
  source_root File.expand_path('templates', __dir__)
7
+ class_option :permission, type: :string, default: nil
6
8
 
7
9
  def copy_query_file
8
10
  file_path = class_name.underscore
9
11
 
12
+ @permission = options['permission']
13
+
10
14
  template('query.rb', "app/domain/queries/#{file_path}.rb")
11
15
  template('query_handler.rb', "app/domain/queries/#{file_path}_handler.rb")
12
16
 
13
- register_query(class_name)
17
+ register_query
14
18
  end
15
19
 
16
20
  private
17
21
 
18
- def register_query(full_name)
22
+ def register_query
19
23
  handler_config = 'config/initializers/register_cqrs_handlers.rb'
20
24
 
21
25
  unless File.exist?('config/initializers/register_cqrs_handlers.rb')
22
26
  template('register_cqrs_handlers.rb', handler_config)
23
27
  end
24
-
25
- code_to_inject = "$QUERY_EXECUTOR.register_command(#{full_name}, #{full_name}Handler.new)\n"
26
-
27
- inject_into_file(handler_config, code_to_inject, after: "# Register Queries\n")
28
28
  end
29
29
  end
30
30
  end
@@ -1,9 +1,26 @@
1
+ # frozen_string_literal: true
1
2
  module Commands
2
- class <%= class_name %>Handler < NexusCqrs::BaseCommandHandler
3
+ # Command handler
4
+ class <%= class_name %>Handler < BaseCommandHandler
5
+ <% if @permission %>include NexusCqrs::Helpers<% end %>
3
6
 
4
- # call is where the Command is executed
7
+ # @param [<%= class_name %>] command
5
8
  def call(command)
9
+ <% if @permission %>authorize(command, Model)<% end %>
6
10
 
11
+ domain_event(command)
7
12
  end
13
+
14
+ private
15
+
16
+ def domain_event(command)
17
+ NexusDomainEvents::Event.new(
18
+ message_name: :ENTITY_WAS_ACTIONED,
19
+ actor: command.metadata[:current_user].member_id,
20
+ payload: {
21
+ object_id: command.id,
22
+ }
23
+ )
24
+ end
8
25
  end
9
26
  end
@@ -1,9 +1,10 @@
1
1
  module Queries
2
2
  class <%= class_name %>Handler < NexusCqrs::BaseQueryHandler
3
+ <% if @permission %>include NexusCqrs::Helpers<% end %>
3
4
 
4
- # call is where the Query is executed
5
+ # @param [<%= class_name %>] command
5
6
  def call(command)
6
-
7
+ <% if @permission %>authorize(command, Model)<% end %>
7
8
  end
8
9
  end
9
10
  end
@@ -1,9 +1,26 @@
1
- command_bus = NexusCqrs::CommandBus.new
2
- query_bus = NexusCqrs::CommandBus.new
1
+ # frozen_string_literal: true
2
+ Rails.configuration.to_prepare do
3
3
 
4
- $COMMAND_EXECUTOR = NexusCqrs::CommandExecutor.new command_bus
5
- $QUERY_EXECUTOR = NexusCqrs::CommandExecutor.new query_bus
4
+ middleware_stack = Middleware::Builder.new do |b|
5
+ b.use(NexusCqrsAuth::AuthMiddleware)
6
+ end
6
7
 
7
- # Register Queries
8
+ command_bus = NexusCqrs::CommandBus.new(middleware: middleware_stack)
9
+ query_bus = NexusCqrs::CommandBus.new(middleware: middleware_stack)
8
10
 
9
- # Register Commands
11
+ $COMMAND_EXECUTOR = NexusCqrs::CommandExecutor.new(command_bus)
12
+ $QUERY_EXECUTOR = NexusCqrs::CommandExecutor.new(query_bus)
13
+
14
+ # Register Commands
15
+ $QUERY_EXECUTOR.configure_handlers do |executor|
16
+ # Manually registered queries should always go above the autoregister
17
+
18
+ executor.autoregister(NexusCqrs::BaseQuery, 'app/domain/queries')
19
+ end
20
+
21
+ $COMMAND_EXECUTOR.configure_handlers do |executor|
22
+ # Manually registered commands should always go above the autoregister
23
+
24
+ executor.autoregister(NexusCqrs::BaseCommand, 'app/domain/commands')
25
+ end
26
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseCommand < BaseMessage
3
4
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseCommandHandler
3
4
  def call(command)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseMessage
3
4
  def set_metadata(key, value)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseMiddleware
3
4
  def initialize(next_)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseQuery < BaseMessage
3
4
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class BaseQueryHandler
3
4
  def call(query)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'thread_safe'
2
3
  require 'middleware'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  class CommandExecutor
3
4
  # @param [NexusCqrs::CommandBus] command_bus
@@ -5,6 +6,45 @@ module NexusCqrs
5
6
  @bus = command_bus
6
7
  end
7
8
 
9
+ def autoregister(base_class, dir = 'app/domain/commands', ignore_strings = ['.rb', 'app/domain/'])
10
+
11
+ # Iterate over the director passed and find all ruby files, removing unwanted parts of the string.
12
+ Dir["#{dir}/*.rb"].each do |file|
13
+ ignore_strings.each { |i|
14
+ file.slice!(i)
15
+ }
16
+ begin
17
+ # Constantize class name to constant to force rails to autoload this class
18
+ file.camelize.constantize
19
+ rescue NameError => e
20
+ puts "WARN: Tried to autoregister #{file.camelize} but class could not be found"
21
+ end
22
+ end
23
+
24
+ ObjectSpace.each_object(Class).select { |klass| klass < base_class }.each do |c|
25
+ handler_name = (c.to_s + "Handler")
26
+ begin
27
+ handler = handler_name.constantize.new
28
+ rescue NameError => e
29
+ Rails.logger.error "WARN: A command/query called `#{c}` tried to autoregister `#{handler_name}` but the class was not found"
30
+ next
31
+ end
32
+
33
+ if handler.respond_to?(:call)
34
+ register_command(c, handler)
35
+ else
36
+ Rails.logger.error "WARN: A command/query called `#{c}` tried to autoregister `#{handler_name}` but the class did not have a `call` method"
37
+ end
38
+ end
39
+ end
40
+
41
+ # Returns the bus used by this executor
42
+ #
43
+ # @return [NexusCqrs::CommandBus] command_bus
44
+ def command_bus
45
+ @bus
46
+ end
47
+
8
48
  # @param [NexusCqrs::BaseCommand] command
9
49
  def execute(command)
10
50
  @bus.call(command)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
3
  module Helpers
3
4
  # Executes a CQRS Command
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrs
2
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
3
4
  end
data/lib/nexus_cqrs.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'nexus_cqrs/base_message'
2
3
  require 'nexus_cqrs/base_middleware'
3
4
  require 'nexus_cqrs/base_command'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_cqrs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Lovett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2021-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: generator_spec
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.2.26
111
+ rubygems_version: 3.2.31
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Core package for the nexus cqrs gem