Skip to content

Commit e3b0c0c

Browse files
Remove container and small refactor
1 parent a4eebfc commit e3b0c0c

11 files changed

+113
-90
lines changed

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- uses: actions/checkout@v2
1717
- uses: ruby/setup-ruby@v1
1818
with:
19-
ruby-version: "3.3"
19+
ruby-version: "3.4"
2020
bundler-cache: true
2121
- name: Run Linter
2222
run: bundle exec ci-helper RubocopLint
@@ -43,7 +43,7 @@ jobs:
4343
strategy:
4444
fail-fast: false
4545
matrix:
46-
ruby: ["3.1", "3.2"]
46+
ruby: ["3.1", "3.2", "3.3"]
4747
experimental: [false]
4848
include:
4949
- ruby: head

Gemfile.lock

+34-32
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ PATH
33
specs:
44
resol (1.0.0)
55
dry-configurable (~> 1.2.0)
6-
dry-container (~> 0.11)
76

87
GEM
98
remote: https://rubygems.org/
109
specs:
11-
activesupport (7.2.2.1)
10+
activesupport (8.0.1)
1211
base64
1312
benchmark (>= 0.3)
1413
bigdecimal
@@ -20,10 +19,11 @@ GEM
2019
minitest (>= 5.1)
2120
securerandom (>= 0.3)
2221
tzinfo (~> 2.0, >= 2.0.5)
22+
uri (>= 0.13.1)
2323
ast (2.4.2)
2424
base64 (0.2.0)
2525
benchmark (0.4.0)
26-
bigdecimal (3.1.8)
26+
bigdecimal (3.1.9)
2727
bundler-audit (0.9.2)
2828
bundler (>= 1.2.0, < 3)
2929
thor (~> 1.0)
@@ -34,41 +34,40 @@ GEM
3434
coderay (1.1.3)
3535
colorize (1.1.0)
3636
concurrent-ruby (1.3.4)
37-
connection_pool (2.4.1)
37+
connection_pool (2.5.0)
3838
diff-lcs (1.5.1)
3939
docile (1.4.1)
4040
drb (2.2.1)
4141
dry-configurable (1.2.0)
4242
dry-core (~> 1.0, < 2)
4343
zeitwerk (~> 2.6)
44-
dry-container (0.11.0)
45-
concurrent-ruby (~> 1.0)
46-
dry-core (1.0.2)
44+
dry-core (1.1.0)
4745
concurrent-ruby (~> 1.0)
4846
logger
4947
zeitwerk (~> 2.6)
50-
dry-inflector (1.1.0)
51-
dry-initializer (3.1.1)
48+
dry-inflector (1.2.0)
49+
dry-initializer (3.2.0)
5250
i18n (1.14.6)
5351
concurrent-ruby (~> 1.0)
54-
json (2.9.0)
52+
json (2.9.1)
5553
language_server-protocol (3.17.0.3)
56-
logger (1.6.3)
54+
logger (1.6.5)
5755
method_source (1.1.0)
5856
minitest (5.25.4)
5957
parallel (1.26.3)
6058
parser (3.3.6.0)
6159
ast (~> 2.4.1)
6260
racc
63-
pry (0.15.0)
61+
pry (0.15.2)
6462
coderay (~> 1.1)
6563
method_source (~> 1.0)
66-
qonfig (0.28.0)
64+
qonfig (0.30.0)
65+
base64 (>= 0.2)
6766
racc (1.8.1)
6867
rack (3.1.8)
6968
rainbow (3.1.1)
7069
rake (13.2.1)
71-
regexp_parser (2.9.3)
70+
regexp_parser (2.10.0)
7271
rspec (3.13.0)
7372
rspec-core (~> 3.13.0)
7473
rspec-expectations (~> 3.13.0)
@@ -82,45 +81,45 @@ GEM
8281
diff-lcs (>= 1.2.0, < 2.0)
8382
rspec-support (~> 3.13.0)
8483
rspec-support (3.13.2)
85-
rubocop (1.66.1)
84+
rubocop (1.69.2)
8685
json (~> 2.3)
8786
language_server-protocol (>= 3.17.0)
8887
parallel (~> 1.10)
8988
parser (>= 3.3.0.2)
9089
rainbow (>= 2.2.2, < 4.0)
91-
regexp_parser (>= 2.4, < 3.0)
92-
rubocop-ast (>= 1.32.2, < 2.0)
90+
regexp_parser (>= 2.9.3, < 3.0)
91+
rubocop-ast (>= 1.36.2, < 2.0)
9392
ruby-progressbar (~> 1.7)
94-
unicode-display_width (>= 2.4.0, < 3.0)
93+
unicode-display_width (>= 2.4.0, < 4.0)
9594
rubocop-ast (1.37.0)
9695
parser (>= 3.3.1.0)
97-
rubocop-config-umbrellio (1.66.0.99)
98-
rubocop (~> 1.66.0)
96+
rubocop-config-umbrellio (1.69.0.101)
97+
rubocop (~> 1.69.0)
9998
rubocop-factory_bot (~> 2.26.0)
100-
rubocop-performance (~> 1.22.0)
101-
rubocop-rails (~> 2.26.0)
99+
rubocop-performance (~> 1.23.0)
100+
rubocop-rails (~> 2.28.0)
102101
rubocop-rake (~> 0.6.0)
103-
rubocop-rspec (~> 3.0.0)
104-
rubocop-sequel (~> 0.3.3)
102+
rubocop-rspec (~> 3.3.0)
103+
rubocop-sequel (~> 0.3.0)
105104
rubocop-factory_bot (2.26.1)
106105
rubocop (~> 1.61)
107-
rubocop-performance (1.22.1)
106+
rubocop-performance (1.23.1)
108107
rubocop (>= 1.48.1, < 2.0)
109108
rubocop-ast (>= 1.31.1, < 2.0)
110-
rubocop-rails (2.26.2)
109+
rubocop-rails (2.28.0)
111110
activesupport (>= 4.2.0)
112111
rack (>= 1.1)
113112
rubocop (>= 1.52.0, < 2.0)
114113
rubocop-ast (>= 1.31.1, < 2.0)
115114
rubocop-rake (0.6.0)
116115
rubocop (~> 1.0)
117-
rubocop-rspec (3.0.5)
116+
rubocop-rspec (3.3.0)
118117
rubocop (~> 1.61)
119-
rubocop-sequel (0.3.7)
118+
rubocop-sequel (0.3.8)
120119
rubocop (~> 1.0)
121120
ruby-progressbar (1.13.0)
122-
securerandom (0.4.0)
123-
sequel (5.87.0)
121+
securerandom (0.4.1)
122+
sequel (5.88.0)
124123
bigdecimal
125124
simplecov (0.22.0)
126125
docile (~> 1.1)
@@ -141,8 +140,11 @@ GEM
141140
concurrent-ruby (~> 1.0)
142141
umbrellio-sequel-plugins (0.17.0)
143142
sequel
144-
unicode-display_width (2.6.0)
145-
zeitwerk (2.6.18)
143+
unicode-display_width (3.1.3)
144+
unicode-emoji (~> 4.0, >= 4.0.4)
145+
unicode-emoji (4.0.4)
146+
uri (1.0.2)
147+
zeitwerk (2.7.1)
146148

147149
PLATFORMS
148150
arm64-darwin-21

lib/resol.rb

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

33
require "dry-configurable"
4-
require "dry-container"
4+
require_relative "resol/version"
5+
6+
require_relative "resol/injector"
7+
require_relative "resol/plugins"
8+
require_relative "resol/service"
59

610
module Resol
711
extend self
@@ -10,11 +14,3 @@ module Resol
1014

1115
setting :classes_allowed_to_patch, default: ["Resol::Service"]
1216
end
13-
14-
require_relative "resol/version"
15-
16-
require_relative "resol/injector"
17-
require_relative "resol/plugins"
18-
require_relative "resol/service"
19-
20-
require_relative "resol/dependency_container"

lib/resol/dependency_container.rb

-27
This file was deleted.

lib/resol/injector.rb

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
# frozen_string_literal: true
22

33
module Resol
4+
# The Injector class is responsible for injecting initialization logic provider.
5+
#
6+
# Supported providers:
7+
# - :smart -> uses the `smart_core/initializer`
8+
# - :dry -> uses the `dry-initializer`
49
class Injector
510
InjectMarker = Module.new
11+
NAME_TO_METHOD_MAPPING = {
12+
smart: :inject_smart,
13+
dry: :inject_dry,
14+
}.freeze
615

7-
def initialize(proc_register)
8-
self.proc_register = proc_register
16+
def self.inject_smart(service)
17+
require "smart_core/initializer"
18+
19+
service.include(SmartCore::Initializer)
20+
end
21+
22+
def self.inject_dry(service)
23+
require "dry/initializer"
24+
25+
service.extend(Dry::Initializer)
26+
end
27+
28+
def initialize(initializer_name)
29+
self.called_method = NAME_TO_METHOD_MAPPING.fetch(initializer_name.to_sym)
930
end
1031

1132
def inject!(service_class)
1233
error!("parent or this class already injected") if service_class.include?(InjectMarker)
1334

14-
service_class.instance_eval(&proc_register)
35+
self.class.public_send(called_method, service_class)
1536
service_class.include(InjectMarker)
1637
end
1738

1839
private
1940

20-
attr_accessor :proc_register
41+
attr_accessor :called_method
2142

2243
def error!(msg)
2344
raise msg

lib/resol/plugins.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def plugin(caller_class, plugin_name)
2424

2525
plugin_module = find_plugin_module(plugin_name)
2626
if defined?(plugin_module::InstanceMethods)
27-
caller_class.prepend(plugin_module::InstanceMethods)
27+
caller_class.include(plugin_module::InstanceMethods)
2828
end
2929

3030
if defined?(plugin_module::ClassMethods)
31-
caller_class.singleton_class.prepend(plugin_module::ClassMethods)
31+
caller_class.extend(plugin_module::ClassMethods)
3232
end
3333

3434
plugins << plugin_name

lib/resol/plugins/return_in_service.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ def handle_catch(_service)
1212
end
1313

1414
module InstanceMethods
15-
private
15+
module PrependedMethods
16+
private
17+
18+
def proceed_return(_service, data) = data
19+
end
1620

17-
def proceed_return(_service, data) = data
21+
def self.included(service)
22+
service.prepend(PrependedMethods)
23+
end
1824
end
1925
end
2026
end

lib/resol/service.rb

+34-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,33 @@ def inherited(klass)
3838
super
3939
end
4040

41-
def inject_initializer!(injector_name)
42-
injector = DependencyContainer.resolve("external_libs.#{injector_name}")
43-
injector.inject!(self)
41+
# Allows you to configure an initializer library for this service,
42+
# such as `dry-initializer` or `smart_core/initializer`.
43+
#
44+
# @param initializer_name [String, Symbol]
45+
# The name of the initializer (e.g., `:dry` or `:smart`).
46+
# @return [void]
47+
def use_initializer!(initializer_name)
48+
Resol::Injector.new(initializer_name).inject!(self)
4449
end
4550

4651
def plugin(...)
4752
manager.plugin(self, ...)
4853
end
4954

55+
# Calls the service using class-level invocation. Builds the service object,
56+
# runs any callbacks, and invokes the `#call` instance method.
57+
#
58+
# @param args [Array] Positional arguments for building service instance.
59+
# @param kwargs [Hash] Keyword arguments for building service instance.
60+
# @yield [block] An optional block passed into service's `#call`.
61+
#
62+
# @return [Resol::Success, Resol::Failure]
63+
# Returns a `Resol::Success` if the service completed via `success!`,
64+
# or a `Resol::Failure` if exited with`fail!` calling.
65+
#
66+
# @raise [InvalidCommandImplementation]
67+
# If neither `#success!` nor `#fail!` is called inside the `#call` method.
5068
def call(*args, **kwargs, &)
5169
service = build(*args, **kwargs)
5270

@@ -63,23 +81,33 @@ def call(*args, **kwargs, &)
6381
Resol::Failure(e)
6482
end
6583

84+
# Same as {call}, but attempts to resolve value from monad.
85+
# If the result is a failure, it raises the error instead of returning a `Resol::Failure`.
86+
#
87+
# @param args [Array] Positional arguments for building the service instance.
88+
# @param kwargs [Hash] Keyword arguments for building the service instance.
89+
# @yield [block] An optional block passed to the service's `#call`.
90+
#
91+
# @return [Object]
92+
# Returns a value, with which instance of service interrupts with {#success!}.
93+
#
94+
# @raise [self::Failure]
95+
# Raises an error if the service fails.
6696
def call!(...)
6797
call(...).value_or { |error| raise error }
6898
end
6999

70100
private
71101

72102
def manager
73-
@manager ||= DependencyContainer.resolve("lib.plugin_manager")
103+
@manager ||= Plugins::Manager.new
74104
end
75105

76106
def handle_catch(service, &)
77107
catch(service, &)
78108
end
79109
end
80110

81-
# @!method call
82-
83111
attr_accessor :__result_method__called__
84112

85113
private

resol.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ Gem::Specification.new do |spec|
1818
spec.require_paths = ["lib"]
1919

2020
spec.add_dependency "dry-configurable", "~>1.2.0"
21-
spec.add_dependency "dry-container", "~>0.11"
2221
end

spec/service_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def call
346346

347347
context "when inherited from already injected service" do
348348
let(:child_service) { Class.new(SmartService) }
349-
let(:injecting_proc) { proc { inject_initializer!(:dry_injector) } }
349+
let(:injecting_proc) { proc { use_initializer!(:dry) } }
350350

351351
it "tries to inject initializer" do
352352
expect { child_service.class_eval(&injecting_proc) }.to raise_error do |error|

0 commit comments

Comments
 (0)