Sha256: 33707a37e23d4a36ae750d72de15dc88a8906c45a9bc5fe8f49e9c02271ee129

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module BCDD
  module Context::CallableAndThen
    class Caller < Result::CallableAndThen::Caller
      module KeyArgs
        def self.parameters?(source)
          parameters = source.parameters.map(&:first)

          !parameters.empty? && parameters.all?(/\Akey/)
        end

        def self.invalid_arity(source, method)
          Result::CallableAndThen::Error::InvalidArity.build(source: source, method: method, arity: 'only keyword args')
        end
      end

      def self.call_proc!(source, value, _injected_value)
        return source.call(**value) if KeyArgs.parameters?(source)

        raise KeyArgs.invalid_arity(source, :call)
      end

      def self.call_method!(source, method, value, _injected_value)
        return source.send(method.name, **value) if KeyArgs.parameters?(method)

        raise KeyArgs.invalid_arity(source, method.name)
      end

      def self.ensure_result_object(source, value, result)
        return result.tap { result.send(:acc).then { _1.merge!(value.merge(_1)) } } if result.is_a?(Context)

        raise Result::Error::UnexpectedOutcome.build(outcome: result, origin: source,
                                                     expected: Context::EXPECTED_OUTCOME)
      end

      private_class_method :call_proc!, :call_method!
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-1.0.0 lib/bcdd/context/callable_and_then.rb