resol 0.2.0 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 243b5c0d3e3a14a705dd6c8237603d161e55e10120df472d84287f6096e35b0c
4
- data.tar.gz: 45f4d69cb14396375d667d1ec9842fefe546c0c518c8beb233fb5feeeacd4376
3
+ metadata.gz: 0dc5047622a240e207abad9020c3835060ae039eb93fa5f3741896d57871f681
4
+ data.tar.gz: e608a7e82d8c8d54169d305f7b1dbc01a7e1f6088877a05815d7b95979df4da8
5
5
  SHA512:
6
- metadata.gz: f5901ddb1f9f04dce2129944e1a83fac1477dd883b1ffa56aa1c083afba425e73976439ac5da81e9dc3f323cc3180283b449d94381ea9595cb5e33ab2460bf9c
7
- data.tar.gz: 423785b253f1d54678aa45cff5b99eb2c627f080f74b437c75468c5914dbe87529af4bd564caeab90b9beea240fb9678c596c8c0716ad6c6bbee969ef54c6f2b
6
+ metadata.gz: 9cdd9fe171f117b346c4ebc787451a9def9fc28fbc23f8023852bb2388eb030e3b898dc883864eb0c4b180683c8f4058c74ee4661e856f0d9ce8e55a33cc46e4
7
+ data.tar.gz: 143cd088406f35993f522d3e2549741277eb28fee2b9f99248b86ba9dbd5302b3eee3ec4db9cefbefc8ad8992af64348272e7e09bb068c58b0407df365ce2ce8
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,14 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resol (0.2.0)
4
+ resol (0.4.2)
5
5
  smart_initializer (~> 0.5)
6
- uber (~> 0.1)
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
10
9
  specs:
11
- activesupport (6.1.3.1)
10
+ activesupport (6.1.3.2)
12
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
13
12
  i18n (>= 1.6, < 2)
14
13
  minitest (>= 5.1)
@@ -24,18 +23,18 @@ GEM
24
23
  umbrellio-sequel-plugins (~> 0.4)
25
24
  coderay (1.1.3)
26
25
  colorize (0.8.1)
27
- concurrent-ruby (1.1.8)
26
+ concurrent-ruby (1.1.9)
28
27
  diff-lcs (1.4.4)
29
- docile (1.3.5)
28
+ docile (1.4.0)
30
29
  dry-inflector (0.2.0)
31
30
  i18n (1.8.10)
32
31
  concurrent-ruby (~> 1.0)
33
32
  method_source (1.0.0)
34
33
  minitest (5.14.4)
35
34
  parallel (1.20.1)
36
- parser (3.0.1.0)
35
+ parser (3.0.1.1)
37
36
  ast (~> 2.4.1)
38
- pry (0.14.0)
37
+ pry (0.14.1)
39
38
  coderay (~> 1.1)
40
39
  method_source (~> 1.0)
41
40
  qonfig (0.25.0)
@@ -66,9 +65,9 @@ GEM
66
65
  rubocop-ast (>= 1.2.0, < 2.0)
67
66
  ruby-progressbar (~> 1.7)
68
67
  unicode-display_width (>= 1.4.0, < 3.0)
69
- rubocop-ast (1.4.1)
70
- parser (>= 2.7.1.5)
71
- rubocop-config-umbrellio (1.11.0.40)
68
+ rubocop-ast (1.7.0)
69
+ parser (>= 3.0.1.1)
70
+ rubocop-config-umbrellio (1.11.0.51)
72
71
  rubocop (= 1.11.0)
73
72
  rubocop-performance (= 1.10.0)
74
73
  rubocop-rails (= 2.9.1)
@@ -90,14 +89,14 @@ GEM
90
89
  rubocop-sequel (0.2.0)
91
90
  rubocop (~> 1.0)
92
91
  ruby-progressbar (1.11.0)
93
- sequel (5.43.0)
92
+ sequel (5.45.0)
94
93
  simplecov (0.21.2)
95
94
  docile (~> 1.1)
96
95
  simplecov-html (~> 0.11)
97
96
  simplecov_json_formatter (~> 0.1)
98
97
  simplecov-html (0.12.3)
99
98
  simplecov-lcov (0.8.0)
100
- simplecov_json_formatter (0.1.2)
99
+ simplecov_json_formatter (0.1.3)
101
100
  smart_engine (0.11.0)
102
101
  smart_initializer (0.5.0)
103
102
  qonfig (~> 0.24)
@@ -109,15 +108,15 @@ GEM
109
108
  thor (1.1.0)
110
109
  tzinfo (2.0.4)
111
110
  concurrent-ruby (~> 1.0)
112
- uber (0.1.0)
113
- umbrellio-sequel-plugins (0.4.0.164)
111
+ umbrellio-sequel-plugins (0.5.1.27)
114
112
  sequel
115
- symbiont-ruby (>= 0.6)
113
+ symbiont-ruby
116
114
  unicode-display_width (2.0.0)
117
115
  zeitwerk (2.4.2)
118
116
 
119
117
  PLATFORMS
120
118
  x86_64-darwin-19
119
+ x86_64-darwin-20
121
120
  x86_64-linux
122
121
 
123
122
  DEPENDENCIES
@@ -132,4 +131,4 @@ DEPENDENCIES
132
131
  simplecov-lcov
133
132
 
134
133
  BUNDLED WITH
135
- 2.2.15
134
+ 2.2.19
data/README.md CHANGED
@@ -63,6 +63,34 @@ All incoming params and options can be defined using a [smart_initializer](https
63
63
  - `success!(value)` – finish with a success value
64
64
  - `fail!(code, data = nil)` – fail with any error code and optional data
65
65
 
66
+ #### Define callbacks
67
+
68
+ You can define callbacks to be executed before calling the `#call` method.
69
+
70
+ ```ruby
71
+ class FindUser < Resol::Service
72
+ param :id
73
+
74
+ before_call :set_user
75
+
76
+ def call
77
+ if user
78
+ success!(user)
79
+ else
80
+ fail!(:not_found)
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ attr_accessor :user
87
+
88
+ def set_user
89
+ self.user = User.find(id)
90
+ end
91
+ end
92
+ ```
93
+
66
94
  ### Result objects
67
95
 
68
96
  Methods:
@@ -71,6 +99,8 @@ Methods:
71
99
  - `failure?` – returns `true` for failure result and `false` for success result
72
100
  - `value!` – unwraps a result object, returns the value for success result, and throws an error for failure result
73
101
  - `value_or(other_value, &block)` – returns a value for success result or `other_value` for failure result (either calls `block` in case it given)
102
+ - `or(&block)` - calls block for failure result, for success result does nothing
103
+ - `either(success_proc, failure_proc)` - for success result calls success_proc with result value in args, for failure result calls failure_proc with error in args.
74
104
 
75
105
 
76
106
  ### Configuration
data/lib/resol.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "uber/builder"
4
3
  require "smart_core/initializer"
5
4
 
6
5
  require_relative "resol/version"
data/lib/resol/builder.rb CHANGED
@@ -1,18 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resol
4
+ # Most of the code here is borrowed from Uber::Builder code
5
+ # See https://github.com/apotonick/uber/blob/master/lib/uber/builder.rb
4
6
  module Builder
5
7
  def self.included(base)
6
- base.include(Uber::Builder)
7
8
  base.extend(ClassMethods)
8
9
  end
9
10
 
11
+ class Builders < Array
12
+ def call(initial_klass, *args, **kwargs)
13
+ each do |block|
14
+ klass = block.call(initial_klass, *args, **kwargs) and return klass
15
+ end
16
+
17
+ initial_klass
18
+ end
19
+
20
+ def <<(proc)
21
+ wrapped = -> (ctx, *args, **kwargs) { ctx.instance_exec(*args, **kwargs, &proc) }
22
+ super(wrapped)
23
+ end
24
+ end
25
+
10
26
  module ClassMethods
27
+ def builders
28
+ @builders ||= Builders.new
29
+ end
30
+
31
+ def builds(proc = nil, &block)
32
+ builders << (proc || block)
33
+ end
34
+
11
35
  def build_klass(*args, **kwargs)
12
36
  klass = self
13
37
 
14
38
  loop do
15
- new_klass = klass.build!(klass, *args, **kwargs)
39
+ new_klass = klass.builders.call(klass, *args, **kwargs)
16
40
  break if new_klass == klass
17
41
 
18
42
  klass = new_klass
@@ -21,8 +45,8 @@ module Resol
21
45
  klass
22
46
  end
23
47
 
24
- def build(*args)
25
- build_klass(*args).new(*args)
48
+ def build(...)
49
+ build_klass(...).new(...)
26
50
  end
27
51
  end
28
52
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resol
4
+ module Callbacks
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ base.instance_variable_set(:@__callback_methods__, [])
8
+ end
9
+
10
+ module ClassMethods
11
+ def inherited(subclass)
12
+ super
13
+ subclass.instance_variable_set(:@__callback_methods__, @__callback_methods__.dup)
14
+ end
15
+
16
+ def before_call(method_name)
17
+ @__callback_methods__ << method_name
18
+ end
19
+
20
+ private
21
+
22
+ def __run_callbacks__(instance)
23
+ @__callback_methods__.each { |method_name| instance.__send__(method_name) }
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/resol/result.rb CHANGED
@@ -10,6 +10,14 @@ module Resol
10
10
  # @!method value!
11
11
 
12
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
13
21
  end
14
22
 
15
23
  class Success < Result
@@ -62,11 +70,11 @@ module Resol
62
70
  end
63
71
  end
64
72
 
65
- def self.Success(*args)
66
- Success.new(*args)
73
+ def self.Success(...)
74
+ Success.new(...)
67
75
  end
68
76
 
69
- def self.Failure(*args)
70
- Failure.new(*args)
77
+ def self.Failure(...)
78
+ Failure.new(...)
71
79
  end
72
80
  end
data/lib/resol/service.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "builder"
4
+ require_relative "callbacks"
4
5
  require_relative "result"
5
6
 
6
7
  module Resol
@@ -41,6 +42,7 @@ module Resol
41
42
 
42
43
  include SmartCore::Initializer
43
44
  include Resol::Builder
45
+ include Resol::Callbacks
44
46
 
45
47
  class << self
46
48
  def inherited(klass)
@@ -49,8 +51,9 @@ module Resol
49
51
  super
50
52
  end
51
53
 
52
- def call(*args, **options, &block)
53
- command = build(*args, **options)
54
+ def call(*args, **kwargs, &block)
55
+ command = build(*args, **kwargs)
56
+ __run_callbacks__(command)
54
57
  command.call(&block)
55
58
 
56
59
  error_message = "No success! or fail! called in the #call method in #{command.class}"
@@ -61,8 +64,8 @@ module Resol
61
64
  Resol::Failure(e)
62
65
  end
63
66
 
64
- def call!(*args)
65
- call(*args).value_or { |error| raise error }
67
+ def call!(...)
68
+ call(...).value_or { |error| raise error }
66
69
  end
67
70
  end
68
71
 
data/lib/resol/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resol
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Bespalov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-12 00:00:00.000000000 Z
11
+ date: 2021-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smart_initializer
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.5'
27
- - !ruby/object:Gem::Dependency
28
- name: uber
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.1'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler-audit
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -171,6 +157,7 @@ files:
171
157
  - bin/setup
172
158
  - lib/resol.rb
173
159
  - lib/resol/builder.rb
160
+ - lib/resol/callbacks.rb
174
161
  - lib/resol/result.rb
175
162
  - lib/resol/service.rb
176
163
  - lib/resol/version.rb
@@ -178,7 +165,7 @@ homepage: https://github.com/umbrellio/resol
178
165
  licenses:
179
166
  - MIT
180
167
  metadata: {}
181
- post_install_message:
168
+ post_install_message:
182
169
  rdoc_options: []
183
170
  require_paths:
184
171
  - lib
@@ -193,8 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
180
  - !ruby/object:Gem::Version
194
181
  version: '0'
195
182
  requirements: []
196
- rubygems_version: 3.1.2
197
- signing_key:
183
+ rubygems_version: 3.2.15
184
+ signing_key:
198
185
  specification_version: 4
199
186
  summary: Gem for creating (any) object patterns
200
187
  test_files: []