Skip to content

Commit 5777ce4

Browse files
Refacor, plugins feature, initializer feature
Now supports plugins like sequel, can choose initializer, remove return engine
1 parent 72d2929 commit 5777ce4

12 files changed

+142
-116
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ gem "rubocop-config-umbrellio"
1414
gem "simplecov"
1515
gem "simplecov-lcov"
1616
gem "smart_initializer"
17+
gem "qonfig", "0.28.0"

Gemfile.lock

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ GEM
5555
pry (0.15.0)
5656
coderay (~> 1.1)
5757
method_source (~> 1.0)
58-
qonfig (0.29.0)
58+
qonfig (0.28.0)
5959
racc (1.8.1)
6060
rack (3.1.8)
6161
rainbow (3.1.1)
@@ -149,6 +149,7 @@ DEPENDENCIES
149149
ci-helper
150150
dry-initializer
151151
pry
152+
qonfig (= 0.28.0)
152153
rake
153154
resol!
154155
rspec

Steepfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
D = Steep::Diagnostic
4+
5+
target :lib do
6+
signature "sig"
7+
check "lib/resol/result.rb"
8+
9+
configure_code_diagnostics(D::Ruby.default)
10+
end

lib/resol.rb

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

33
require_relative "resol/version"
4-
require_relative "resol/return_engine"
54
require_relative "resol/configuration"
5+
require_relative "resol/initializers"
66
require_relative "resol/service"
7+
require_relative "resol/plugins"
78

89
module Resol
910
extend self
@@ -15,4 +16,14 @@ def config
1516
def configure
1617
yield config
1718
end
19+
20+
# rubocop:disable Naming/MethodName
21+
def Success(...)
22+
Success.new(...)
23+
end
24+
25+
def Failure(...)
26+
Failure.new(...)
27+
end
28+
# rubocop:enable Naming/MethodName
1829
end

lib/resol/configuration.rb

-13
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,16 @@ module Resol
44
module Configuration
55
extend self
66

7-
DEFAULTS = { return_engine: Resol::ReturnEngine::Catch }.freeze
8-
9-
DEFAULTS.each_key do |attr_name|
10-
define_method(attr_name) { values[attr_name] }
11-
define_method(:"#{attr_name}=") { |value| values[attr_name] = value }
12-
end
13-
147
def smart_config
158
return nil if smart_not_loaded?
169

1710
SmartCore::Initializer::Configuration.config
1811
end
1912

20-
def to_h = values.dup
21-
2213
private
2314

2415
def smart_not_loaded?
2516
!defined?(SmartCore::Initializer::Configuration)
2617
end
27-
28-
def values
29-
@values ||= DEFAULTS.dup
30-
end
3118
end
3219
end

lib/resol/plugins.rb

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,44 @@
44

55
module Resol
66
module Plugins
7+
PLUGINS_PATH = Pathname("resol/plugins")
78
class Manager
8-
include Singleton
9-
109
def initialize
11-
self.patched_class = Resol::Service
1210
self.plugins = []
13-
self.plugin_lib_path = PathName("resol/plugins")
1411
end
1512

16-
def plugin(plugin_name, ...)
17-
plugin_module = find_plugin_module(plugin_name)
18-
plugin_module.apply(target_class, ...) if plugin_module.respond_to?(:apply)
13+
def plugin(plugin_name)
14+
return if plugins.include?(plugin_name)
1915

16+
plugin_module = find_plugin_module(plugin_name)
2017
if defined?(plugin_module::InstanceMethods)
21-
target_class.include(plugin_module::InstanceMethods)
18+
target_class.prepend(plugin_module::InstanceMethods)
2219
end
2320

2421
if defined?(plugin_module::ClassMethods)
25-
target_class.extend(plugin_module::ClassMethods)
22+
target_class.singleton_class.prepend(plugin_module::ClassMethods)
2623
end
2724

2825
plugins << plugin_name
2926
end
3027

3128
private
3229

33-
attr_accessor :target_class, :plugins, :plugin_lib_path
30+
attr_accessor :plugins
3431

3532
def find_plugin_module(plugin_name)
36-
require plugin_lib_path.join(plugin_name)
33+
require PLUGINS_PATH.join(plugin_name)
34+
Plugins.const_get(classify_plugin_name(plugin_name))
3735
rescue LoadError, NameError => e
3836
raise "Failed to load plugin '#{plugin_name}': #{e.message}"
3937
end
4038

41-
def camel_case(string)
42-
string.split("_").map(&:capitalize).join
39+
def classify_plugin_name(string)
40+
string.split(/_|-/).map!(&:capitalize).join
41+
end
42+
43+
def target_class
44+
Resol::Service
4345
end
4446
end
4547
end
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Resol
4+
module Plugins
5+
module ReturnInService
6+
module ClassMethods
7+
private
8+
9+
def handle_catch(_service)
10+
yield
11+
end
12+
13+
def call_service(service)
14+
service.call.tap { |res| return unless res.is_a?(Service::Result) }
15+
end
16+
end
17+
18+
module InstanceMethods
19+
private
20+
21+
def proceed_return(_service, data) = data
22+
end
23+
end
24+
end
25+
end

lib/resol/result.rb

+29-32
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,11 @@
33
module Resol
44
class UnwrapError < StandardError; end
55

6-
class Result
7-
# @!method success?
8-
# @!method failure?
9-
# @!method value_or
10-
# @!method value!
11-
12-
def initialize(*); end
13-
14-
def or
15-
yield(@value) if failure?
16-
end
17-
18-
def either(success_proc, failure_proc)
19-
success? ? success_proc.call(@value) : failure_proc.call(@value)
20-
end
21-
end
6+
class Result; end
227

238
class Success < Result
249
def initialize(value)
25-
super
10+
super()
2611
@value = value
2712
end
2813

@@ -34,22 +19,34 @@ def failure?
3419
false
3520
end
3621

37-
def value_or(*)
22+
def value_or(_other_value = nil)
3823
@value
3924
end
4025

4126
def value!
4227
@value
4328
end
4429

45-
def error
46-
nil
30+
def error = nil
31+
32+
def or = nil
33+
34+
def either(success_proc, _failure_proc)
35+
success_proc.call(@value)
36+
end
37+
38+
def bind
39+
yield @value
40+
end
41+
42+
def fmap(&)
43+
Resol.Success(bind(&))
4744
end
4845
end
4946

5047
class Failure < Result
5148
def initialize(error)
52-
super
49+
super()
5350
@value = error
5451
end
5552

@@ -62,11 +59,7 @@ def failure?
6259
end
6360

6461
def value_or(other_value = nil)
65-
if block_given?
66-
yield(@value)
67-
else
68-
other_value
69-
end
62+
block_given? ? yield(@value) : other_value
7063
end
7164

7265
def value!
@@ -76,13 +69,17 @@ def value!
7669
def error
7770
@value
7871
end
79-
end
8072

81-
def self.Success(...)
82-
Success.new(...)
83-
end
73+
def or
74+
yield @value
75+
end
76+
77+
def either(_success_proc, failure_proc)
78+
failure_proc.call(@value)
79+
end
80+
81+
def bind = self
8482

85-
def self.Failure(...)
86-
Failure.new(...)
83+
alias fmap bind
8784
end
8885
end

lib/resol/return_engine/catch.rb

-24
This file was deleted.

lib/resol/return_engine/return.rb

-21
This file was deleted.

0 commit comments

Comments
 (0)