strum 0.0.21 → 0.0.22

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: 8564c1b4cee7cb9226c7beaba0dbc32a9f39b731d177a0510ef3e00a68eacb0c
4
- data.tar.gz: d0237c3b5aff99261d61bfabb044f01323b537b40ae0d786aa735bf903a17de4
3
+ metadata.gz: 5e8f1a992f7753d1e95d3b3a4595c4da54c9e425bd8cd973993070660597bb43
4
+ data.tar.gz: 1eac3f924db75025c620418c616d32777b8a82ad7a6fc2a91e185807f9bc7aeb
5
5
  SHA512:
6
- metadata.gz: 68de1c7b0db5542040617f8eda848e4de15c446a9ce740225938038e9eeb58debddf88e564059ce9185201dac46f88863aae61e6dbd0d6193f0e7d991e6e0e78
7
- data.tar.gz: fb54166c475d63f0f99e277afe9f553eced31387bcc9b895262a8d6ad4c1f232fbd301f35eed903a0d5271dff3ccf670db4a49fe6c14f1297f3da71a2575686f
6
+ metadata.gz: 0eeb529bf7ce9a93b44855844d180ed1ac9256daf200a32b556a3cc58221b01ac497234226f8063922f06db9e75f0749f40fc752d29cf6c393128999e90d4d88
7
+ data.tar.gz: 9218ffe1bc3e47184b3b170e9802dd4cd54ec6f1204d06d971cf2fe52d126ad7c462c3b4136f1862afc33a4ea8f711be762fb9ebc29e42b2eae165cd68875a1e
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "strum/version"
4
4
  require "strum/service"
5
+ require "strum/chain"
5
6
 
6
7
  module Strum
7
8
  class Error < StandardError; end
@@ -0,0 +1,21 @@
1
+ module Strum
2
+ class Chain
3
+ include Strum::Service
4
+
5
+ def self.call(input = {}, *chain, &block)
6
+ new(input).execute(*chain, &block)
7
+ end
8
+
9
+ def execute(*chain, &block)
10
+ audit
11
+ @output = input
12
+ while (service = chain.shift) && valid?
13
+ service.public_send(:call, output) do |m|
14
+ m.success { |result| @output = result }
15
+ m.failure { |errors| add_errors(errors) }
16
+ end
17
+ end
18
+ valid? ? valid_result(&block) : invalid_result(&block)
19
+ end
20
+ end
21
+ end
@@ -31,30 +31,10 @@ module Strum
31
31
  # Internal: Interactor class methods.
32
32
  module ClassMethods
33
33
  def call(input = {}, &block)
34
- strum = new(input)
35
- strum.audit
36
- strum.call if strum.valid?
37
- strum.valid? ? valid_result(strum, &block) : invalid_result(strum, &block)
34
+ new(input).execute(&block)
38
35
  end
39
-
40
- private
41
-
42
- def valid_result(strum, &block)
43
- return strum.output || strum.outputs unless block_given?
44
- return StrumMatcher.call([true, strum.output], &block) if strum.output
45
-
46
- StrumMatcher.call([true, *strum.outputs.keys, strum.outputs], &block)
47
- end
48
-
49
- def invalid_result(strum, &block)
50
- return nil unless block_given?
51
-
52
- StrumMatcher.call([false, *strum.errors.values, strum.errors], &block)
53
- end
54
36
  end
55
37
 
56
- attr_reader :output, :outputs
57
-
58
38
  # Instance methods
59
39
  def initialize(input)
60
40
  self.strum_errors = {}
@@ -66,12 +46,12 @@ module Strum
66
46
  end
67
47
  end
68
48
 
69
- def call
70
- raise Failure, "call method must be implemented"
49
+ def execute(&block)
50
+ audit
51
+ call if valid?
52
+ valid? ? valid_result(&block) : invalid_result(&block)
71
53
  end
72
54
 
73
- def audit; end
74
-
75
55
  def valid?
76
56
  strum_errors.empty?
77
57
  end
@@ -82,8 +62,13 @@ module Strum
82
62
 
83
63
  protected
84
64
 
85
- attr_accessor :input, :strum_errors
86
- attr_writer :output, :outputs
65
+ attr_accessor :input, :strum_errors, :output, :outputs
66
+
67
+ def call
68
+ raise Failure, "call method must be implemented"
69
+ end
70
+
71
+ def audit; end
87
72
 
88
73
  def add_error(field, value)
89
74
  if field.is_a?(Array)
@@ -112,5 +97,18 @@ module Strum
112
97
  rescue StandardError
113
98
  key
114
99
  end
100
+
101
+ def valid_result(&block)
102
+ return output || outputs unless block_given?
103
+ return StrumMatcher.call([true, output], &block) if output
104
+
105
+ StrumMatcher.call([true, *outputs.keys, outputs], &block)
106
+ end
107
+
108
+ def invalid_result(&block)
109
+ return nil unless block_given?
110
+
111
+ StrumMatcher.call([false, *errors.values, errors], &block)
112
+ end
115
113
  end
116
114
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strum
4
- VERSION = "0.0.21"
4
+ VERSION = "0.0.22"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-09 00:00:00.000000000 Z
11
+ date: 2019-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-inflector
@@ -142,6 +142,7 @@ files:
142
142
  - bin/strum
143
143
  - lib/strum.rb
144
144
  - lib/strum/Rakefile
145
+ - lib/strum/chain.rb
145
146
  - lib/strum/cli.rb
146
147
  - lib/strum/command.rb
147
148
  - lib/strum/commands/generate.rb