strum 0.0.25 → 0.0.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/strum/chain.rb +3 -9
- data/lib/strum/json_serializer.rb +1 -1
- data/lib/strum/serializer_name.rb +1 -1
- data/lib/strum/service.rb +43 -9
- data/lib/strum/templates/service_crud/%resource_name%/create.rb.tt +1 -1
- data/lib/strum/templates/service_crud/%resource_name%/find.rb.tt +1 -1
- data/lib/strum/templates/service_crud/%resource_name%/search.rb.tt +1 -1
- data/lib/strum/templates/service_crud/%resource_name%/update.rb.tt +3 -3
- data/lib/strum/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af3921774101423a2f06bb8cad080738bbdd6fe2bc35da3d9b3e0e8158c8a222
|
4
|
+
data.tar.gz: 873c5a9d6306077dae49b3ef3342ae666f13ff0aac3c77147fc54d0a76dc94db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46520355b2158efba77519750d6759fc13eb706f0d043f7a4b2ce18bdee02684f749890b0ad7d4c11ee8d012c6c68e4afcf919310e5843a3edd6c5497a1cb531
|
7
|
+
data.tar.gz: dccf7f3e66814ea3181430dd812fbd5fb20d44a30a0a2f2aeda85caf1ae7024a51a7d826f39aaf8b8a17e3fdd901053f4b0a53634ac655e523219072374a4c59
|
data/Gemfile.lock
CHANGED
data/lib/strum/chain.rb
CHANGED
@@ -10,16 +10,10 @@ module Strum
|
|
10
10
|
|
11
11
|
def execute(*chain, &block)
|
12
12
|
audit
|
13
|
-
|
13
|
+
output(payload)
|
14
14
|
while (service = chain.shift) && valid?
|
15
|
-
service.public_send(:call,
|
16
|
-
m.success
|
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
|
data/lib/strum/service.rb
CHANGED
@@ -30,16 +30,19 @@ module Strum
|
|
30
30
|
|
31
31
|
# Internal: Interactor class methods.
|
32
32
|
module ClassMethods
|
33
|
-
def call(
|
34
|
-
new(
|
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(
|
39
|
+
def initialize(payload)
|
40
40
|
self.strum_errors = {}
|
41
41
|
self.outputs = {}
|
42
|
-
self.
|
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, :
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
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 %>
|
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 %>
|
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
|
|
@@ -9,10 +9,10 @@
|
|
9
9
|
<%= ident %> class Update
|
10
10
|
<%= ident %> include Strum::Service
|
11
11
|
<%= ident %> def call
|
12
|
-
<%= ident %>
|
13
|
-
<%= ident %> if
|
12
|
+
<%= ident %> output(<%= resource_class_name %>.find(id: input[:id]))
|
13
|
+
<%= ident %> if output
|
14
14
|
<%= ident %> input.delete(:id)
|
15
|
-
<%= ident %>
|
15
|
+
<%= ident %> output.update(input)
|
16
16
|
<%= ident %> else
|
17
17
|
<%= ident %> add_error(:<%= resource_name %>, :not_found)
|
18
18
|
<%= ident %> end
|
data/lib/strum/version.rb
CHANGED