Sha256: 0f7b6228b6eeb17b5ce7f195eb33ea84f0769682d555a37891c436585c54d2f9

Contents?: true

Size: 997 Bytes

Versions: 3

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

require 'dry/logic/operations/binary'
require 'dry/logic/result'

module Dry
  module Logic
    module Operations
      class And < Binary
        attr_reader :hints

        def initialize(*, **)
          super
          @hints = options.fetch(:hints, true)
        end

        def type
          :and
        end
        alias operator type

        def call(input)
          left_result = left.(input)

          if left_result.success?
            right_result = right.(input)

            if right_result.success?
              Result::SUCCESS
            else
              Result.new(false, id) { right_result.ast(input) }
            end
          else
            Result.new(false, id) do
              left_ast = left_result.to_ast
              hints ? [type, [left_ast, [:hint, right.ast(input)]]] : left_ast
            end
          end
        end

        def [](input)
          left[input] && right[input]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-logic-1.0.6 lib/dry/logic/operations/and.rb
dry-logic-1.0.5 lib/dry/logic/operations/and.rb
dry-logic-1.0.4 lib/dry/logic/operations/and.rb