Class: Dry::Transaction::Extra::Steps::Merge
- Inherits:
-
Object
- Object
- Dry::Transaction::Extra::Steps::Merge
- Defined in:
- lib/dry/transaction/extra/steps/merge.rb
Overview
If you’re using keyword args as the arguments to your steps, you often want a step to add its output to those args, while keeping the original kwargs intact.
* If the output of the step is a Hash, then that hash is merged into the input.
* If the output of the step is not a Hash, then a key is inferred
from the step name. The name of the key can be overridden with the
`as:` option.
Instance Method Summary collapse
Instance Method Details
#call(operation, options, args) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dry/transaction/extra/steps/merge.rb', line 57 def call(operation, , args) if args.size > 1 || (!args[0].is_a?(Hash) && !args[0].nil?) raise ArgumentError, "the merge step only works with keyword arguments" end result = operation.call(*args) return result if result.is_a?(Failure) value = result.is_a?(Success) ? result.value! : result unless value.is_a?(Hash) key = [:as] || [:step_name] value = { key.to_sym => value } end Success((args[0] || {}).merge(value)) end |