decouplio 1.0.0alpha3 → 1.0.0alpha4

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.
data/docs/error_store.rb DELETED
@@ -1,202 +0,0 @@
1
- require_relative '../lib/decouplio'
2
-
3
- # How to use
4
- class SomeAction < Decouplio::Action
5
- logic do
6
- step :step_one
7
- fail :fail_one
8
- end
9
-
10
- def step_one(**)
11
- false
12
- end
13
-
14
- def fail_one(**)
15
- add_error(:something_went_wrong, 'Something went wrong')
16
- end
17
- end
18
-
19
- action = SomeAction.call
20
-
21
- puts action # =>
22
- # Result: failure
23
-
24
- # Railway Flow:
25
- # step_one -> fail_one
26
-
27
- # Context:
28
- # {}
29
-
30
- # Errors:
31
- # {:something_went_wrong=>["Something went wrong"]}
32
-
33
- puts action.errors # =>
34
- # {:something_went_wrong=>["Something went wrong"]}
35
-
36
-
37
-
38
-
39
- # Custom error store
40
- class CustomErrorStore
41
- attr_reader :errors
42
-
43
- def initialize
44
- @errors = {}
45
- end
46
-
47
- def add_error(key:, message:, namespace: :root)
48
- @errors[namespace] ||= {}
49
- @errors[namespace].store(
50
- key,
51
- (@errors[namespace][key] || []) + [message].flatten
52
- )
53
- end
54
-
55
- def merge(error_store)
56
- @errors = deep_merge(@errors, error_store.errors)
57
- end
58
-
59
- private
60
-
61
- def deep_merge(this_hash, other_hash)
62
- this_hash.merge(other_hash) do |_key, this_val, other_val|
63
- if this_val.is_a?(Hash) && other_val.is_a?(Hash)
64
- deep_merge(this_val, other_val)
65
- else
66
- this_val + other_val
67
- end
68
- end
69
- end
70
- end
71
-
72
- class SomeActionWithCustomErrorStore < Decouplio::Action
73
- error_store_class CustomErrorStore
74
-
75
- logic do
76
- step :step_one
77
- step :step_two
78
- end
79
-
80
- def step_one(**)
81
- add_error(
82
- key: :under_root,
83
- message: 'Error Message One'
84
- )
85
- end
86
-
87
- def step_two(**)
88
- add_error(
89
- namespace: :step_two,
90
- key: :error_happened,
91
- message: 'Error Message Two'
92
- )
93
- end
94
- end
95
-
96
- action = SomeActionWithCustomErrorStore.call
97
-
98
- puts action # =>
99
- # Result: success
100
-
101
- # Railway Flow:
102
- # step_one -> step_two
103
-
104
- # Context:
105
- # {}
106
-
107
- # Errors:
108
- # {:root=>{:under_root=>["Error Message One"]}, :step_two=>{:error_happened=>["Error Message Two"]}}
109
-
110
- puts action.errors # =>
111
- # {:root=>{:under_root=>["Error Message One"]}, :step_two=>{:error_happened=>["Error Message Two"]}}
112
-
113
-
114
-
115
-
116
- # When error store is the same for parent and inner action
117
-
118
- class InnerActionWithCustomErrorStore < Decouplio::Action
119
- error_store_class CustomErrorStore
120
-
121
- logic do
122
- step :inner_step
123
- end
124
-
125
- def inner_step(**)
126
- add_error(
127
- namespace: :inner,
128
- key: :inner_key,
129
- message: 'Somebody was told me...'
130
- )
131
- end
132
- end
133
-
134
- class ParentActionWithCustomErrorStore < Decouplio::Action
135
- error_store_class CustomErrorStore
136
-
137
- logic do
138
- step :step_one, action: InnerActionWithCustomErrorStore
139
- step :step_two
140
- end
141
-
142
- def step_two(**)
143
- add_error(
144
- namespace: :parent,
145
- key: :error_happened,
146
- message: 'Message'
147
- )
148
- end
149
- end
150
-
151
- action = ParentActionWithCustomErrorStore.call
152
-
153
- puts action # =>
154
- # Result: success
155
-
156
- # Railway Flow:
157
- # step_one -> inner_step -> step_two
158
-
159
- # Context:
160
- # {}
161
-
162
- # Errors:
163
- # {:inner=>{:inner_key=>["Somebody was told me..."]}, :parent=>{:error_happened=>["Message"]}}
164
-
165
- puts action.errors # =>
166
- # {:inner=>{:inner_key=>["Somebody was told me..."]}, :parent=>{:error_happened=>["Message"]}}
167
-
168
-
169
-
170
-
171
- # When error store is different for parent and inner action
172
-
173
- class InnerActionWithDefaultErrorStore < Decouplio::Action
174
- logic do
175
- step :inner_step
176
- end
177
-
178
- def inner_step(**)
179
- add_error(
180
- key: :inner_key,
181
- message: 'Somebody was told me...'
182
- )
183
- end
184
- end
185
-
186
- class ParentActionForInnerActionDefaultErrorStore < Decouplio::Action
187
- error_store_class CustomErrorStore
188
-
189
- logic do
190
- step :step_one, action: InnerActionWithDefaultErrorStore
191
- step :step_two
192
- end
193
-
194
- def step_two(**)
195
- add_error(
196
- namespace: :parent,
197
- key: :error_happened,
198
- message: 'Message'
199
- )
200
- end
201
- end # =>
202
- # Error store for action and inner action should be the same. (Decouplio::Errors::ErrorStoreError)