You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-28Lines changed: 38 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -108,11 +108,11 @@ class Multiply < Micro::Case
108
108
# 2. Define the method `call!` with its business logic
109
109
defcall!
110
110
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
112
112
if a.is_a?(Numeric) && b.is_a?(Numeric)
113
-
Success(a * b)
113
+
Successresult: { number:a * b }
114
114
else
115
-
Failure{'`a` and `b` attributes must be numeric' }
115
+
Failureresult: { message:'`a` and `b` attributes must be numeric' }
116
116
end
117
117
end
118
118
end
@@ -126,22 +126,22 @@ end
126
126
result =Multiply.call(a:2, b:2)
127
127
128
128
result.success? # true
129
-
result.value# 4
129
+
result.data# { number: 4 }
130
130
131
131
# Failure result
132
132
133
133
bad_result =Multiply.call(a:2, b:'2')
134
134
135
135
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" }
137
137
138
138
#-----------------------------#
139
139
# Calling a use case instance #
140
140
#-----------------------------#
141
141
142
142
result =Multiply.new(a:2, b:3).call
143
143
144
-
result.value #6
144
+
result.value #{ number: 6 }
145
145
146
146
# Note:
147
147
# ----
@@ -156,11 +156,13 @@ result.value # 6
156
156
A `Micro::Case::Result` stores the use cases output data. These are their main methods:
157
157
-`#success?` returns true if is a successful result.
158
158
-`#failure?` returns true if is an unsuccessful result.
159
-
-`#value` the result value itself.
159
+
-`#data` the result data itself.
160
160
-`#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.
162
162
-`#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.
164
166
165
167
[⬆️ Back to Top](#table-of-contents-)
166
168
@@ -175,9 +177,13 @@ class Divide < Micro::Case
175
177
attributes :a, :b
176
178
177
179
defcall!
178
-
invalid_attributes.empty? ?Success(a / b) : Failure(invalid_attributes)
#### Is it possible to define a custom result type without a block?
260
268
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.
0 commit comments