active_cqrs 0.1.1
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 +7 -0
- data/lib/active_cqrs/auto_registrar.rb +54 -0
- data/lib/active_cqrs/command.rb +3 -0
- data/lib/active_cqrs/command_bus.rb +14 -0
- data/lib/active_cqrs/handler_registry.rb +15 -0
- data/lib/active_cqrs/logger.rb +21 -0
- data/lib/active_cqrs/query.rb +3 -0
- data/lib/active_cqrs/query_bus.rb +14 -0
- data/lib/active_cqrs/railtie.rb +9 -0
- data/lib/active_cqrs/version.rb +3 -0
- data/lib/active_cqrs.rb +17 -0
- data/lib/generators/cqrs/command/command_generator.rb +18 -0
- data/lib/generators/cqrs/install/install_generator.rb +14 -0
- data/lib/generators/cqrs/install/templates/initializer.rb +15 -0
- data/lib/generators/cqrs/query/query_generator.rb +18 -0
- metadata +71 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c2e4253bd612863165532f83c066668977d3f6caa0c078dacb47bc5376ac29ae
|
|
4
|
+
data.tar.gz: ad25655a55d929a16d6a043a62d19150434edbf2dba06b6907cc941d2cfc764f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9ed47015cb1271d144e4a0c2ddfbea7eef8c28da0b1809750bb56fe495fd11189092cf9db28ef01d9e72a01ea0fed31d3cedfd75c8bd200a1e9c598cdb835128
|
|
7
|
+
data.tar.gz: 560a0487bc47c17c97632982f1b187e5864c96134331e8e67f6a36438ab2c0917446d3a8b07f221cfbfff985cdcc63818d7e552ad603c623e2f9db1b744bd90e
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require "active_cqrs/logger"
|
|
2
|
+
|
|
3
|
+
module ActiveCqrs
|
|
4
|
+
class AutoRegistrar
|
|
5
|
+
def initialize(command_bus:, query_bus:, root: Rails.root)
|
|
6
|
+
@command_bus = command_bus
|
|
7
|
+
@query_bus = query_bus
|
|
8
|
+
@root = root
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
load_handlers
|
|
13
|
+
register_handlers
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def load_handlers
|
|
19
|
+
command_files = Dir[@root.join("app/handlers/commands/**/*.rb")]
|
|
20
|
+
query_files = Dir[@root.join("app/handlers/queries/**/*.rb")]
|
|
21
|
+
|
|
22
|
+
(command_files + query_files).each do |file|
|
|
23
|
+
require_dependency file
|
|
24
|
+
Logger.log "Loaded handler file: #{file.gsub(@root.to_s + "/", "")}"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def register_handlers
|
|
29
|
+
ObjectSpace.each_object(Class).select { |klass| klass.name&.end_with?("Handler") }.each do |handler_class|
|
|
30
|
+
next unless handler_class.name
|
|
31
|
+
|
|
32
|
+
base_name = handler_class.name.sub(/Handler$/, "")
|
|
33
|
+
command_class = safe_constantize("#{base_name}Command")
|
|
34
|
+
query_class = safe_constantize("#{base_name}Query")
|
|
35
|
+
|
|
36
|
+
if command_class
|
|
37
|
+
@command_bus.registry.register(command_class, handler_class.new)
|
|
38
|
+
Logger.log "Registered #{handler_class} for #{command_class}"
|
|
39
|
+
elsif query_class
|
|
40
|
+
@query_bus.registry.register(query_class, handler_class.new)
|
|
41
|
+
Logger.log "Registered #{handler_class} for #{query_class}"
|
|
42
|
+
else
|
|
43
|
+
Logger.log "Skipped #{handler_class} — no matching Command or Query class found"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def safe_constantize(name)
|
|
49
|
+
Object.const_get(name)
|
|
50
|
+
rescue NameError
|
|
51
|
+
nil
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module ActiveCqrs
|
|
2
|
+
class CommandBus
|
|
3
|
+
attr_reader :registry
|
|
4
|
+
|
|
5
|
+
def initialize(registry = HandlerRegistry.new)
|
|
6
|
+
@registry = registry
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(command)
|
|
10
|
+
handler = @registry.resolve(command.class)
|
|
11
|
+
handler.call(command)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module ActiveCqrs
|
|
2
|
+
class HandlerRegistry
|
|
3
|
+
def initialize
|
|
4
|
+
@handlers = {}
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def register(klass, handler)
|
|
8
|
+
@handlers[klass] = handler
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def resolve(klass)
|
|
12
|
+
@handlers[klass] || raise("No handler registered for #{klass}")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "logger"
|
|
2
|
+
|
|
3
|
+
module ActiveCqrs
|
|
4
|
+
module Logger
|
|
5
|
+
class << self
|
|
6
|
+
attr_accessor :enabled
|
|
7
|
+
|
|
8
|
+
def log(message)
|
|
9
|
+
return unless enabled
|
|
10
|
+
|
|
11
|
+
if defined?(Rails)
|
|
12
|
+
::Rails.logger.info("[CQRS] #{message}")
|
|
13
|
+
else
|
|
14
|
+
puts "[CQRS] #{message}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
self.enabled = true
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/active_cqrs.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_cqrs/version"
|
|
4
|
+
require "active_cqrs/command"
|
|
5
|
+
require "active_cqrs/query"
|
|
6
|
+
require "active_cqrs/command_bus"
|
|
7
|
+
require "active_cqrs/query_bus"
|
|
8
|
+
require "active_cqrs/handler_registry"
|
|
9
|
+
require "active_cqrs/logger"
|
|
10
|
+
require "active_cqrs/auto_registrar"
|
|
11
|
+
require "active_cqrs/railtie" if defined?(Rails)
|
|
12
|
+
require_relative "active_cqrs/version"
|
|
13
|
+
|
|
14
|
+
module ActiveCqrs
|
|
15
|
+
class Error < StandardError; end
|
|
16
|
+
# Your code goes here...
|
|
17
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
|
|
3
|
+
module Cqrs
|
|
4
|
+
module Generators
|
|
5
|
+
class CommandGenerator < Rails::Generators::NamedBase
|
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
|
7
|
+
desc "Generates a command and corresponding handler"
|
|
8
|
+
|
|
9
|
+
def create_command_file
|
|
10
|
+
template "command.rb.tt", File.join("app/commands", class_path, "#{file_name}_command.rb")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def create_handler_file
|
|
14
|
+
template "command_handler.rb.tt", File.join("app/handlers/commands", class_path, "#{file_name}_handler.rb")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
|
|
3
|
+
module Cqrs
|
|
4
|
+
module Generators
|
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
|
7
|
+
desc "Creates a CQRS initializer"
|
|
8
|
+
|
|
9
|
+
def copy_initializer
|
|
10
|
+
template "initializer.rb", "config/initializers/cqrs.rb"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_cqrs"
|
|
4
|
+
|
|
5
|
+
Rails.application.config.to_prepare do
|
|
6
|
+
CQRS_COMMAND_BUS = ActiveCqrs::CommandBus.new
|
|
7
|
+
CQRS_QUERY_BUS = ActiveCqrs::QueryBus.new
|
|
8
|
+
|
|
9
|
+
ActiveCqrs::Logger.enabled = Rails.env.development?
|
|
10
|
+
|
|
11
|
+
ActiveCqrs::AutoRegistrar.new(
|
|
12
|
+
command_bus: CQRS_COMMAND_BUS,
|
|
13
|
+
query_bus: CQRS_QUERY_BUS
|
|
14
|
+
).call
|
|
15
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
|
|
3
|
+
module Cqrs
|
|
4
|
+
module Generators
|
|
5
|
+
class QueryGenerator < Rails::Generators::NamedBase
|
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
|
7
|
+
desc "Generates a query and corresponding handler"
|
|
8
|
+
|
|
9
|
+
def create_query_file
|
|
10
|
+
template "query.rb.tt", File.join("app/queries", class_path, "#{file_name}_query.rb")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def create_handler_file
|
|
14
|
+
template "query_handler.rb.tt", File.join("app/handlers/queries", class_path, "#{file_name}_handler.rb")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: active_cqrs
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- kiebor81
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-07-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6.0'
|
|
27
|
+
description: active_cqrs is a lightweight Ruby gem that introduces CQRS into Rails
|
|
28
|
+
applications
|
|
29
|
+
email:
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- lib/active_cqrs.rb
|
|
35
|
+
- lib/active_cqrs/auto_registrar.rb
|
|
36
|
+
- lib/active_cqrs/command.rb
|
|
37
|
+
- lib/active_cqrs/command_bus.rb
|
|
38
|
+
- lib/active_cqrs/handler_registry.rb
|
|
39
|
+
- lib/active_cqrs/logger.rb
|
|
40
|
+
- lib/active_cqrs/query.rb
|
|
41
|
+
- lib/active_cqrs/query_bus.rb
|
|
42
|
+
- lib/active_cqrs/railtie.rb
|
|
43
|
+
- lib/active_cqrs/version.rb
|
|
44
|
+
- lib/generators/cqrs/command/command_generator.rb
|
|
45
|
+
- lib/generators/cqrs/install/install_generator.rb
|
|
46
|
+
- lib/generators/cqrs/install/templates/initializer.rb
|
|
47
|
+
- lib/generators/cqrs/query/query_generator.rb
|
|
48
|
+
homepage: https://github.com/kiebor81/active_cqrs
|
|
49
|
+
licenses:
|
|
50
|
+
- MIT
|
|
51
|
+
metadata: {}
|
|
52
|
+
post_install_message:
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
require_paths:
|
|
55
|
+
- lib
|
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 3.0.0
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubygems_version: 3.5.17
|
|
68
|
+
signing_key:
|
|
69
|
+
specification_version: 4
|
|
70
|
+
summary: CQRS interface for Rails using ActiveRecord
|
|
71
|
+
test_files: []
|