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
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2100
+
2101
+
## BCDD::Result#and_then!
2102
+
2103
+
In the Ruby ecosystem, several gems facilitate operation composition using classes and modules. Two notable examples are the `interactor` gem and the `u-case` gem.
To facilitate migration for users accustomed to the above approaches, `bcdd-result` includes the `BCDD::Result#and_then!`/`BCDD::Result::Context#and_then!` methods, which will invoke the method `call` of the given operation and expect it to return a `BCDD::Result`/`BCDD::Result::Context` object.
2137
+
2138
+
```ruby
2139
+
BCDD::Result.configure do |config|
2140
+
config.feature.enable!(:and_then!)
2141
+
end
2142
+
2143
+
classPlaceOrder
2144
+
includeBCDD::Result::Context.mixin
2145
+
2146
+
defcall(**input)
2147
+
Given(input)
2148
+
.and_then!(CreateOrder.new)
2149
+
.and_then!(PayOrder.new)
2150
+
.and_then!(SendOrderConfirmation.new)
2151
+
.and_then!(NotifyAdmins.new)
2152
+
end
2153
+
end
2154
+
```
2155
+
2156
+
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2157
+
2158
+
#### Dependency Injection
2159
+
2160
+
Like `#and_then`, `#and_then!` also supports an additional argument for dependency injection.
2161
+
2162
+
**In BCDD::Result**
2163
+
2164
+
```ruby
2165
+
classPlaceOrder
2166
+
includeBCDD::Result.mixin
2167
+
2168
+
defcall(input, logger:)
2169
+
Given(input)
2170
+
.and_then!(CreateOrder.new, logger)
2171
+
# Further method chaining...
2172
+
end
2173
+
end
2174
+
```
2175
+
2176
+
**In BCDD::Result::Context**
2177
+
2178
+
```ruby
2179
+
classPlaceOrder
2180
+
includeBCDD::Result::Context.mixin
2181
+
2182
+
defcall(logger:, **input)
2183
+
Given(input)
2184
+
.and_then!(CreateOrder.new, logger:)
2185
+
# Further method chaining...
2186
+
end
2187
+
end
2188
+
```
2189
+
2190
+
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
-`enable!(:and_then!)`: Activates the `and_then!` feature.
2205
+
2206
+
-`default_method_name_to_call`: Sets a default method other than `:call` for `and_then!`.
2207
+
2208
+
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2209
+
2210
+
#### Analysis: Why is `and_then!` an Anti-pattern?
2211
+
2212
+
The `and_then!` approach, despite its brevity, introduces several issues:
2213
+
2214
+
-**Lack of Clarity:** The input/output relationship between the steps is not apparent.
2215
+
2216
+
-**Steps Coupling:** Each operation becomes interdependent (high coupling), complicating implementation and compromising the reusability of these operations.
2217
+
2218
+
We recommend cautious use of `#and_then!`. Due to these issues, it is turned off by default and considered an antipattern.
2219
+
2220
+
It should be a temporary solution, primarily for assisting in migration from another to gem to `bcdd-result`.
2221
+
2222
+
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2223
+
2224
+
#### `#and_then` versus `#and_then!`
2225
+
2226
+
The main difference between the `#and_then` and `#and_then!` is that the latter does not check the result source. However, as a drawback, the result source will change.
2227
+
2228
+
Attention: to ensure the correct behavior, do not mix `#and_then` and `#and_then!` in the same result chain.
2229
+
2230
+
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2231
+
2232
+
#### Analysis: Why is `#and_then` the antidote/standard?
2233
+
2234
+
The `BCDD::Result#and_then`/`BCDD::Result::Context#and_then` methods diverge from the above approach by requiring explicit invocation and mapping of the outcomes at each process step. This approach has the following advantages:
2235
+
2236
+
-**Clarity:** The input/output relationship between the steps is apparent and highly understandable.
2237
+
2238
+
-**Steps uncoupling:** Each operation becomes independent (low coupling). You can even map a failure result to a success (and vice versa).
2239
+
2240
+
See this example to understand what your code should look like:
<palign="right"><ahref="#-bcddresult">⬆️ back to top</a></p>
2287
+
2086
2288
## About
2087
2289
2088
2290
[Rodrigo Serradura](https://github.com/serradura) created this project. He is the B/CDD process/method creator and has already made similar gems like the [u-case](https://github.com/serradura/u-case) and [kind](https://github.com/serradura/kind/blob/main/lib/kind/result.rb). This gem is a general-purpose abstraction/monad, but it also contains key features that serve as facilitators for adopting B/CDD in the code.
0 commit comments