Skip to content

Commit 714c6b6

Browse files
committed
Update README (WIP)
1 parent 723b99c commit 714c6b6

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ class Multiply < Micro::Case
108108
# 2. Define the method `call!` with its business logic
109109
def call!
110110

111-
# 3. Wrap the use case result/output using the `Success()` or `Failure()` methods
111+
# 3. Wrap the use case result/output using the `Success(result: *)` or `Failure(result: *)` methods
112112
if a.is_a?(Numeric) && b.is_a?(Numeric)
113-
Success(a * b)
113+
Success result: { number: a * b }
114114
else
115-
Failure { '`a` and `b` attributes must be numeric' }
115+
Failure result: { message: '`a` and `b` attributes must be numeric' }
116116
end
117117
end
118118
end
@@ -126,22 +126,22 @@ end
126126
result = Multiply.call(a: 2, b: 2)
127127

128128
result.success? # true
129-
result.value # 4
129+
result.data # { number: 4 }
130130

131131
# Failure result
132132

133133
bad_result = Multiply.call(a: 2, b: '2')
134134

135135
bad_result.failure? # true
136-
bad_result.value # "`a` and `b` attributes must be numeric"
136+
bad_result.data # { message: "`a` and `b` attributes must be numeric" }
137137

138138
#-----------------------------#
139139
# Calling a use case instance #
140140
#-----------------------------#
141141

142142
result = Multiply.new(a: 2, b: 3).call
143143

144-
result.value # 6
144+
result.value # { number: 6 }
145145

146146
# Note:
147147
# ----
@@ -156,11 +156,13 @@ result.value # 6
156156
A `Micro::Case::Result` stores the use cases output data. These are their main methods:
157157
- `#success?` returns true if is a successful result.
158158
- `#failure?` returns true if is an unsuccessful result.
159-
- `#value` the result value itself.
159+
- `#data` the result data itself.
160160
- `#type` a Symbol which gives meaning for the result, this is useful to declare different types of failures or success.
161-
- `#on_success` or `#on_failure` are hook methods that help you define the application flow.
161+
- `#on_success` or `#on_failure` are hook methods that help you to define the application flow.
162162
- `#use_case` if is a failure result, the use case responsible for it will be accessible through this method. This feature is handy to handle a flow failure (this topic will be covered ahead).
163-
- `#then` allows if the current result is a success, the `then` method will allow to applying a new use case for its value.
163+
- `#then` this method will allow applying a new use case if the current result was a success. The idea of this feature is to allow the creation of dynamic flows.
164+
165+
> **Note:** for backward compatibility, you could use the `#value` method as an alias of `#data` method.
164166
165167
[⬆️ Back to Top](#table-of-contents-)
166168

@@ -175,9 +177,13 @@ class Divide < Micro::Case
175177
attributes :a, :b
176178

177179
def call!
178-
invalid_attributes.empty? ? Success(a / b) : Failure(invalid_attributes)
179-
rescue => e
180-
Failure(e)
180+
if invalid_attributes.empty?
181+
Success result: { number: a / b }
182+
else
183+
Failure result: { invalid_attributes: invalid_attributes }
184+
end
185+
rescue => exception
186+
Failure result: exception
181187
end
182188

183189
private def invalid_attributes
@@ -190,7 +196,7 @@ end
190196
result = Divide.call(a: 2, b: 2)
191197

192198
result.type # :ok
193-
result.value # 1
199+
result.data # { number: 1 }
194200
result.success? # true
195201
result.use_case # raises `Micro::Case::Error::InvalidAccessToTheUseCaseObject: only a failure result can access its own use case`
196202

@@ -199,7 +205,7 @@ result.use_case # raises `Micro::Case::Error::InvalidAccessToTheUseCaseObject: o
199205
bad_result = Divide.call(a: 2, b: '2')
200206

201207
bad_result.type # :error
202-
bad_result.value # {"b"=>"2"}
208+
bad_result.data # { invalid_attributes: { "b"=>"2" } }
203209
bad_result.failure? # true
204210
bad_result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>"2"}, @a=2, @b="2", @__result=#<Micro::Case::Result:0x0000 @use_case=#<Divide:0x0000 ...>, @type=:error, @value={"b"=>"2"}, @success=false>
205211

@@ -208,31 +214,33 @@ bad_result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>"2"}, @a=2, @b
208214
err_result = Divide.call(a: 2, b: 0)
209215

210216
err_result.type # :exception
211-
err_result.value # <ZeroDivisionError: divided by 0>
217+
err_result.data # { exception: <ZeroDivisionError: divided by 0> }
212218
err_result.failure? # true
213219
err_result.use_case # #<Divide:0x0000 @__attributes={"a"=>2, "b"=>0}, @a=2, @b=0, @__result=#<Micro::Case::Result:0x0000 @use_case=#<Divide:0x0000 ...>, @type=:exception, @value=#<ZeroDivisionError: divided by 0>, @success=false>
214220

215221
# Note:
216222
# ----
217223
# Any Exception instance which is wrapped by
218-
# the Failure() method will receive `:exception` instead of the `:error` type.
224+
# the Failure(result: *) method will receive `:exception` instead of the `:error` type.
219225
```
220226

221227
[⬆️ Back to Top](#table-of-contents-)
222228

223229
#### How to define custom result types?
224230

225-
Answer: Use a symbol as the argument of `Success()`, `Failure()` methods and declare a block to set their values.
231+
Answer: Use a symbol as the argument of `Success()`, `Failure()` methods and declare the `result:` keyword to set the result data.
226232

227233
```ruby
228234
class Multiply < Micro::Case
229235
attributes :a, :b
230236

231237
def call!
232-
return Success(a * b) if a.is_a?(Numeric) && b.is_a?(Numeric)
233-
234-
Failure(:invalid_data) do
235-
attributes.reject { |_, input| input.is_a?(Numeric) }
238+
if a.is_a?(Numeric) && b.is_a?(Numeric)
239+
Success result: { number: a * b }
240+
else
241+
Failure :invalid_data, result: {
242+
attributes: attributes.reject { |_, input| input.is_a?(Numeric) }
243+
}
236244
end
237245
end
238246
end
@@ -242,39 +250,41 @@ end
242250
result = Multiply.call(a: 3, b: 2)
243251

244252
result.type # :ok
245-
result.value # 6
253+
result.data # { number: 6 }
246254
result.success? # true
247255

248256
# Failure result
249257

250258
bad_result = Multiply.call(a: 3, b: '2')
251259

252260
bad_result.type # :invalid_data
253-
bad_result.value # {"b"=>"2"}
261+
bad_result.data # { attributes: {"b"=>"2"} }
254262
bad_result.failure? # true
255263
```
256264

257265
[⬆️ Back to Top](#table-of-contents-)
258266

259267
#### Is it possible to define a custom result type without a block?
260268

261-
Answer: Yes, it is. But only for failure results!
269+
Answer: Yes, it is possible. But this will have special behavior because the result data will be a hash with the given type as the key and true as its value.
262270

263271
```ruby
264272
class Multiply < Micro::Case
265273
attributes :a, :b
266274

267275
def call!
268-
return Failure(:invalid_data) unless a.is_a?(Numeric) && b.is_a?(Numeric)
269-
270-
Success(a * b)
276+
if a.is_a?(Numeric) && b.is_a?(Numeric)
277+
Success result: { number: a * b }
278+
else
279+
Failure(:invalid_data)
280+
end
271281
end
272282
end
273283

274284
result = Multiply.call(a: 2, b: '2')
275285

276286
result.failure? # true
277-
result.value # :invalid_data
287+
result.data # { :invalid_data => true }
278288
result.type # :invalid_data
279289
result.use_case.attributes # {"a"=>2, "b"=>"2"}
280290

0 commit comments

Comments
 (0)