Sha256: 33dcfe87be3d5ead12c1cb799f479580edd05a7ae68c676311671755705c0d91

Contents?: true

Size: 738 Bytes

Versions: 4

Compression:

Stored size: 738 Bytes

Contents

# frozen_string_literal: true

module Pipeable
  module Steps
    # Delegates to a non-callable operation which automatically wraps the result if necessary.
    class To < Abstract
      def initialize(operation, message, **)
        super(**)
        @operation = operation
        @message = message
      end

      def call result
        result.bind do |arguments|
          splat = marameters.categorize operation.method(message).parameters, arguments
          wrap operation.public_send(message, *splat.positionals, **splat.keywords, &splat.block)
        end
      end

      private

      attr_reader :operation, :message

      def wrap(result) = result.is_a?(Dry::Monads::Result) ? result : Success(result)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pipeable-0.4.0 lib/pipeable/steps/to.rb
pipeable-0.3.0 lib/pipeable/steps/to.rb
pipeable-0.2.0 lib/pipeable/steps/to.rb
pipeable-0.1.0 lib/pipeable/steps/to.rb