Sha256: c36c913b9f32cb5e064b32cb364a90b96c5c8ed9290a183469dfaccb2f4f55d1

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Solid
  module Output::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(:memo).then { _1.merge!(value.merge(_1)) } } if result.is_a?(Output)

        raise Result::Error::UnexpectedOutcome.build(outcome: result, origin: source,
                                                     expected: Output::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
solid-result-2.0.0 lib/solid/output/callable_and_then.rb