Skip to content

Commit

Permalink
Add Step#call_operation to support calling operations with no args
Browse files Browse the repository at this point in the history
Some steps in a transaction may not accept any input. Make it easy for step adapters to support calling these operations by adding a `Step#call_operation` method. This only calls the operation with args if it accepts any.
  • Loading branch information
timriley committed Jul 22, 2017
1 parent d11792e commit 9a19ed5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/dry/transaction/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def call(input)
}
end

def call_operation(*input)
if arity >= 1
operation.call(*input)
else
operation.call
end
end

def arity
operation.is_a?(Proc) ? operation.arity : operation.method(:call).arity
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/transaction/step_adapters/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Map
include Dry::Monads::Either::Mixin

def call(step, input, *args)
Right(step.operation.call(input, *args))
Right(step.call_operation(input, *args))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dry/transaction/step_adapters/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Raw
include Dry::Monads::Either::Mixin

def call(step, input, *args)
result = step.operation.call(input, *args)
result = step.call_operation(input, *args)

unless result.is_a?(Dry::Monads::Either)
raise ArgumentError, "step +#{step.step_name}+ must return an Either object"
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/transaction/step_adapters/tee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Tee
include Dry::Monads::Either::Mixin

def call(step, input, *args)
step.operation.call(input, *args)
step.call_operation(input, *args)
Right(input)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/dry/transaction/step_adapters/try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def call(step, input, *args)
raise ArgumentError, "+try+ steps require one or more exception classes provided via +catch:+"
end

Right(step.operation.call(input, *args))
result = step.call_operation(input, *args)
Right(result)
rescue *Array(step.options[:catch]) => e
e = step.options[:raise].new(e.message) if step.options[:raise]
Left(e)
Expand Down

0 comments on commit 9a19ed5

Please sign in to comment.