strum 0.0.25 → 0.0.26

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: 1fe0b1d1b61058e17af6b6e7b9cf775cd5cdfa0c8ace295f1424f274ffbaaadf
4
- data.tar.gz: 668ed074e8870bcdaa8badf23bed56b18a9f1ba98c4c91ba9f9aba9965145d2a
3
+ metadata.gz: af3921774101423a2f06bb8cad080738bbdd6fe2bc35da3d9b3e0e8158c8a222
4
+ data.tar.gz: 873c5a9d6306077dae49b3ef3342ae666f13ff0aac3c77147fc54d0a76dc94db
5
5
  SHA512:
6
- metadata.gz: 9521ad19dcd7ec87e2870128bb7b4cb32a4de5a8bb3647a9a7a615fa13ae462160e98611113373fff0a2189f25bcc323e4c7b83da0808c6479c38d027fa250ac
7
- data.tar.gz: '08a0b846e11138e6a0a38f9aadad666871a1d2bb6064a4270531ee33f8a901e80a13502605e2ec8d2f4429ccf464ac598c6d1dfb94b0060a17fce51971c74f9b'
6
+ metadata.gz: 46520355b2158efba77519750d6759fc13eb706f0d043f7a4b2ce18bdee02684f749890b0ad7d4c11ee8d012c6c68e4afcf919310e5843a3edd6c5497a1cb531
7
+ data.tar.gz: dccf7f3e66814ea3181430dd812fbd5fb20d44a30a0a2f2aeda85caf1ae7024a51a7d826f39aaf8b8a17e3fdd901053f4b0a53634ac655e523219072374a4c59
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum (0.0.25)
4
+ strum (0.0.26)
5
5
  dry-inflector (~> 0.2.0)
6
6
  dry-matcher (~> 0.8.2)
7
7
  thor (~> 0.20)
@@ -10,16 +10,10 @@ module Strum
10
10
 
11
11
  def execute(*chain, &block)
12
12
  audit
13
- @output = input
13
+ output(payload)
14
14
  while (service = chain.shift) && valid?
15
- service.public_send(:call, output) do |m|
16
- m.success do |result|
17
- @output = if @output.is_a?(Hash) && result.is_a?(Hash)
18
- @output.merge(result)
19
- else
20
- result
21
- end
22
- end
15
+ service.public_send(:call, outputs[:default]) do |m|
16
+ m.success { |result| output(result) }
23
17
  m.failure { |errors| add_errors(errors) }
24
18
  end
25
19
  end
@@ -9,7 +9,7 @@ module Strum
9
9
  include Strum::Service
10
10
  define_method :call do
11
11
  serializer_class = Strum::SerializerName.call(name: name)
12
- @output = serializer_class.new(input).serializable_hash
12
+ output(serializer_class.new(input).serializable_hash)
13
13
  end
14
14
  end
15
15
  end
@@ -7,7 +7,7 @@ module Strum
7
7
  include Strum::Service
8
8
 
9
9
  def call
10
- @output = Kernel.const_get(input[:name].to_s + "Serializer")
10
+ output(Kernel.const_get(input[:name].to_s + "Serializer"))
11
11
  rescue StandardError => e
12
12
  add_error(:serializer, e)
13
13
  end
@@ -30,16 +30,19 @@ module Strum
30
30
 
31
31
  # Internal: Interactor class methods.
32
32
  module ClassMethods
33
- def call(input = nil, &block)
34
- new(input).execute(&block)
33
+ def call(payload = nil, &block)
34
+ new(payload).execute(&block)
35
35
  end
36
36
  end
37
37
 
38
38
  # Instance methods
39
- def initialize(input)
39
+ def initialize(payload)
40
40
  self.strum_errors = {}
41
41
  self.outputs = {}
42
- self.input = input
42
+ self.clear_output = false
43
+ self.payload = payload
44
+ self.payload.freeze
45
+ self.input = self.payload.dup
43
46
  end
44
47
 
45
48
  def execute(&block)
@@ -56,9 +59,13 @@ module Strum
56
59
  strum_errors
57
60
  end
58
61
 
62
+ def output_clear?
63
+ clear_output
64
+ end
65
+
59
66
  protected
60
67
 
61
- attr_accessor :input, :strum_errors, :output, :outputs
68
+ attr_accessor :payload, :input, :strum_errors, :outputs, :clear_output
62
69
 
63
70
  def call
64
71
  raise Failure, "call method must be implemented"
@@ -66,6 +73,18 @@ module Strum
66
73
 
67
74
  def audit; end
68
75
 
76
+ def self.output=(value)
77
+ @outputs[:default] = value
78
+ end
79
+
80
+ def output(key = :default)
81
+ @outputs[key]
82
+ end
83
+
84
+ def output(key = :default, value)
85
+ @outputs[key] = value
86
+ end
87
+
69
88
  def add_error(field, value)
70
89
  if field.is_a?(Array)
71
90
  field.each do |key|
@@ -94,6 +113,10 @@ module Strum
94
113
  self.input = [*input]
95
114
  end
96
115
 
116
+ def clear_output!
117
+ self.clear_output = true
118
+ end
119
+
97
120
  private
98
121
 
99
122
  def key_to_sym(key)
@@ -103,10 +126,21 @@ module Strum
103
126
  end
104
127
 
105
128
  def valid_result(&block)
106
- return output || outputs unless block_given?
107
- return StrumMatcher.call([true, output], &block) if output
108
-
109
- StrumMatcher.call([true, *outputs.keys, outputs], &block)
129
+ result_outputs = if !output_clear? && payload.is_a?(Hash)
130
+ outputs.each_with_object({}) do |(k, v), memo|
131
+ memo[k] = if v.is_a?(Hash)
132
+ payload.merge(v)
133
+ else
134
+ v
135
+ end
136
+ end
137
+ else
138
+ outputs
139
+ end
140
+ return result_outputs[:default] || result_outputs unless block_given?
141
+ return StrumMatcher.call([true, result_outputs[:default]], &block) if result_outputs[:default]
142
+
143
+ StrumMatcher.call([true, *result_outputs.keys, result_outputs], &block)
110
144
  end
111
145
 
112
146
  def invalid_result(&block)
@@ -12,7 +12,7 @@
12
12
  <%= ident %> def call
13
13
  <%= ident %> if (<%= resource_name %> = <%= resource_class_name %>.new(input)).valid?
14
14
  <%= ident %> <%= resource_name %>.save
15
- <%= ident %> @output = <%= resource_name %>
15
+ <%= ident %> output(%= resource_name %)
16
16
  <%= ident %> else
17
17
  <%= ident %> add_errors(<%= resource_name %>.errors)
18
18
  <%= ident %> end
@@ -10,7 +10,7 @@
10
10
  <%= ident %> include Strum::Service
11
11
 
12
12
  <%= ident %> def call
13
- <%= ident %> @output = <%= resource_class_name %>.find(input)
13
+ <%= ident %> output(<%= resource_class_name %>.find(input))
14
14
  <%= ident %> add_error(:<%= resource_name %>, :not_found) unless @output
15
15
  <%= ident %> end
16
16
 
@@ -10,7 +10,7 @@
10
10
  <%= ident %> include Strum::Service
11
11
 
12
12
  <%= ident %> def call
13
- <%= ident %> @output = <%= resource_class_name %>.all
13
+ <%= ident %> output(<%= resource_class_name %>.all)
14
14
  <%= ident %> end
15
15
 
16
16
  <%= ident %> def audit
@@ -9,10 +9,10 @@
9
9
  <%= ident %> class Update
10
10
  <%= ident %> include Strum::Service
11
11
  <%= ident %> def call
12
- <%= ident %> @output = <%= resource_class_name %>.find(id: input[:id])
13
- <%= ident %> if @output
12
+ <%= ident %> output(<%= resource_class_name %>.find(id: input[:id]))
13
+ <%= ident %> if output
14
14
  <%= ident %> input.delete(:id)
15
- <%= ident %> @output.update(input)
15
+ <%= ident %> output.update(input)
16
16
  <%= ident %> else
17
17
  <%= ident %> add_error(:<%= resource_name %>, :not_found)
18
18
  <%= ident %> end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strum
4
- VERSION = "0.0.25"
4
+ VERSION = "0.0.26"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.25
4
+ version: 0.0.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov