Closed MatheusRich closed 3 years ago
@MatheusRich Thak you again for your contribution! 👏
As we talked on Telegram, follow the final "spec" for this feature
Check(result: true) # Success(:ok, result: { check: true })
Check(result: 1) # Success(:ok, result: { check: true })
Check(result: nil) # Failure(:error, result: { check: false })
Check(result: false) # Failure(:error, result: { check: false })
Check(result: true, on: { success: {a: 1}, failure: {})
# Success(:ok, result: { a: 1 })
Check(:valid, result: true) # Success(:valid, result: { valid: true })
Check(:valid, result: false) # Failure(:valid, result: { valid: false })
This PR adds the method
Check
on Micro::Case.Micro::Case#Check
Check is a method that make simple to evaluate boolean conditions and turn them into
Result
objects.Let's take this simple use case as an example. It should check if a given number is even and fails on odd numbers:
We can simplify this conditional by using
Check
. Take a look:Changing the result type
It's possible to add a type a custom result type:
Changing the result value
By default
Check
adds the return of the given block as the Result value. In the example above, the value would be the result ofnumber.even?
.You can define a custom return value with the
result:
keyword argument:In the example above, the result value will be
number
, rather thannumber.even?
.Check with private methods
Check
really shines when using private methods to define steps in a use case: