nexus_cqrs 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/generators/nexus_cqrs/USAGE +1 -1
- data/lib/generators/nexus_cqrs/command_generator.rb +6 -6
- data/lib/generators/nexus_cqrs/query_generator.rb +6 -6
- data/lib/generators/nexus_cqrs/templates/command_handler.rb +19 -2
- data/lib/generators/nexus_cqrs/templates/query_handler.rb +3 -2
- data/lib/generators/nexus_cqrs/templates/register_cqrs_handlers.rb +23 -6
- data/lib/nexus_cqrs/base_command.rb +1 -0
- data/lib/nexus_cqrs/base_command_handler.rb +1 -0
- data/lib/nexus_cqrs/base_message.rb +1 -0
- data/lib/nexus_cqrs/base_middleware.rb +1 -0
- data/lib/nexus_cqrs/base_query.rb +1 -0
- data/lib/nexus_cqrs/base_query_handler.rb +1 -0
- data/lib/nexus_cqrs/command_bus.rb +1 -0
- data/lib/nexus_cqrs/command_executor.rb +40 -0
- data/lib/nexus_cqrs/helpers.rb +1 -0
- data/lib/nexus_cqrs/version.rb +2 -1
- data/lib/nexus_cqrs.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c4e6e4a145007fe8658503e9745e3be716436749d9bea2c714bca1d47b1ea4ab
|
|
4
|
+
data.tar.gz: aedbcb7c36185faf7d546e3ea5c268cba6642d0880a0a2c2cdf612e761f5096c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 83c240bd9f056d0d82e5437f52c45527be6092e83f6356c765bfa127153d8d5c5c660437598707fb470879606ede7c9fc1a28f11e42355401b177c29b37e7c45
|
|
7
|
+
data.tar.gz: 0dab29f1d52e3f4110cfb948ffbe425c342f682c01f1945ceced2385aa022201e84c7025b1cc7eb633110029efdf4e7b5e0366baf2b7466183a164a040df5ff3
|
data/Gemfile.lock
CHANGED
|
@@ -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
|
|
17
|
+
register_command
|
|
14
18
|
end
|
|
15
19
|
|
|
16
20
|
private
|
|
17
21
|
|
|
18
|
-
def register_command
|
|
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
|
|
17
|
+
register_query
|
|
14
18
|
end
|
|
15
19
|
|
|
16
20
|
private
|
|
17
21
|
|
|
18
|
-
def register_query
|
|
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
|
-
|
|
3
|
+
# Command handler
|
|
4
|
+
class <%= class_name %>Handler < BaseCommandHandler
|
|
5
|
+
<% if @permission %>include NexusCqrs::Helpers<% end %>
|
|
3
6
|
|
|
4
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
Rails.configuration.to_prepare do
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
middleware_stack = Middleware::Builder.new do |b|
|
|
5
|
+
b.use(NexusCqrsAuth::AuthMiddleware)
|
|
6
|
+
end
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
command_bus = NexusCqrs::CommandBus.new(middleware: middleware_stack)
|
|
9
|
+
query_bus = NexusCqrs::CommandBus.new(middleware: middleware_stack)
|
|
8
10
|
|
|
9
|
-
|
|
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 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)
|
data/lib/nexus_cqrs/helpers.rb
CHANGED
data/lib/nexus_cqrs/version.rb
CHANGED
data/lib/nexus_cqrs.rb
CHANGED
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.
|
|
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-
|
|
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.
|
|
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
|