Sha256: e1ee4ff21733aced38159f819de1fc686cfa2eaa5467e88df7efaf6d80520380

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/LineLength
module Rung
  module Runner
    module CallHelper
      class << self
        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

          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
            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(action, operation_instance)
          case action
          when Symbol, String
            operation_instance.method(action)
          when Proc
            action
          else
            action.method(:call)
          end
        rescue NameError
          action
        end
      end
    end
  end
end
# rubocop:enable Metrics/ParameterLists, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/LineLength

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rung-0.1 lib/rung/runner/call_helper.rb