resol 0.2.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ace6271ca99729b99ebf4828dd8915078834d010d883ee14e71f98ac28534602
4
- data.tar.gz: c3c864c2622e681e0dba5bae64020083a7754604ed0f67623779cc73fc324a9a
3
+ metadata.gz: 3e85fb79be6315df1a0073d5ee5b5c78dd18c2bc4b7f208c4bd72754f923ff13
4
+ data.tar.gz: ee06e3e8d41e9800ddb12a9ba0392ab3d7e171ea66332f702a9c46126c6042c0
5
5
  SHA512:
6
- metadata.gz: dcbbc7fc8aed320aae8dbc8134ba1bf04b9226bb0555d49889777ba72f113b215a31986168a6248ab1d3001619ac3ba0f5c82f63aaed8db143f7f9f4aced4930
7
- data.tar.gz: 01ff693499b76bad144bdd215e1709db905b0ceedb6d805ebc4796538dcc96c0e1213623e0dec0b337da649d4b409a703a2dde01d2df5b6c8a877d402dfd0968
6
+ metadata.gz: fef4615bf36c1bde74815e9e5db5e3c5ff3ae7bada67142207042096c150bba680227755568298893f1024d5c8aaf5a6e14fc3e164a4f07b9d0176d70a4271d2
7
+ data.tar.gz: deeb6d29fdd34f3116df29f143134f44efd7d8f112ae07b9e564cf62e830b97171e9abae8edd03541b9f8e3aac6f1e43850d15c2d95d8d37374fbb5324bc1914
data/Gemfile.lock CHANGED
@@ -1,14 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resol (0.2.1)
4
+ resol (0.5.0)
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.4)
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)
@@ -57,19 +56,19 @@ GEM
57
56
  diff-lcs (>= 1.2.0, < 2.0)
58
57
  rspec-support (~> 3.10.0)
59
58
  rspec-support (3.10.2)
60
- rubocop (1.11.0)
59
+ rubocop (1.17.0)
61
60
  parallel (~> 1.10)
62
61
  parser (>= 3.0.0.0)
63
62
  rainbow (>= 2.2.2, < 4.0)
64
63
  regexp_parser (>= 1.8, < 3.0)
65
64
  rexml
66
- rubocop-ast (>= 1.2.0, < 2.0)
65
+ rubocop-ast (>= 1.7.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)
72
- rubocop (= 1.11.0)
68
+ rubocop-ast (1.7.0)
69
+ parser (>= 3.0.1.1)
70
+ rubocop-config-umbrellio (1.17.0.53)
71
+ rubocop (= 1.17.0)
73
72
  rubocop-performance (= 1.10.0)
74
73
  rubocop-rails (= 2.9.1)
75
74
  rubocop-rake (= 0.5.1)
@@ -90,34 +89,34 @@ 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
- smart_initializer (0.5.0)
101
+ smart_initializer (0.7.0)
103
102
  qonfig (~> 0.24)
104
103
  smart_engine (~> 0.11)
105
104
  smart_types (~> 0.4)
106
105
  smart_types (0.4.0)
107
106
  smart_engine (~> 0.11)
108
- symbiont-ruby (0.6.0)
107
+ symbiont-ruby (0.7.0)
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
11
- def build_klass(*args)
27
+ def builders
28
+ @builders ||= Builders.new
29
+ end
30
+
31
+ def builds(proc = nil, &block)
32
+ builders << (proc || block)
33
+ end
34
+
35
+ def build_klass(*args, **kwargs)
12
36
  klass = self
13
37
 
14
38
  loop do
15
- new_klass = klass.build!(klass, *args)
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, &block)
53
- command = build(*args)
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.1"
4
+ VERSION = "0.5.0"
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.1
4
+ version: 0.5.0
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-19 00:00:00.000000000 Z
11
+ date: 2021-06-28 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: []