lib/rung/runner/call_helper.rb in rung-0.0.1.pre.alpha vs lib/rung/runner/call_helper.rb in rung-0.1
- old
+ new
@@ -1,33 +1,51 @@
+# rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/LineLength
module Rung
module Runner
module CallHelper
class << self
- def call(operation, state, operation_instance, from_block = false, &block)
- raise "Can't pass block when from_block is enabled" if block && from_block
- runnable = to_runnable(operation, operation_instance)
+ def call(action, state, operation_instance, action_from_block = false, second_argument = nil, &block)
+ raise Error, "Can't pass block when action_from_block is enabled" if block && action_from_block
- if from_block
- runnable.arity.zero? ? operation_instance.instance_exec(&runnable) : operation_instance.instance_exec(state, &runnable)
+ runnable = to_runnable(action, operation_instance)
+ arity = runnable.arity
+
+ if action_from_block
+ case arity
+ when 0
+ operation_instance.instance_exec(&runnable)
+ when 1
+ operation_instance.instance_exec(state, &runnable)
+ else
+ operation_instance.instance_exec(state, second_argument, &runnable)
+ end
else
- runnable.arity.zero? ? runnable.call(&block) : runnable.call(state, &block)
+ case arity
+ when 0
+ runnable.call(&block)
+ when 1
+ runnable.call(state, &block)
+ else
+ runnable.call(state, second_argument, &block)
+ end
end
end
private
- def to_runnable(operation, operation_instance)
- case operation
+ def to_runnable(action, operation_instance)
+ case action
when Symbol, String
- operation_instance.method(operation)
+ operation_instance.method(action)
when Proc
- operation
+ action
else
- operation.method(:call)
+ action.method(:call)
end
rescue NameError
- operation
+ action
end
end
end
end
end
+# rubocop:enable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/LineLength