Sha256: 4ebb5ccbc3b543d4e04844ec2815411aba9372eb05fc5b4d7e38b197108dae35

Contents?: true

Size: 954 Bytes

Versions: 2

Compression:

Stored size: 954 Bytes

Contents

# frozen_string_literal: true

module Fear
  module Extractor
    # Recursive array matcher. Match against its head and tail
    #
    class ArrayMatcher < Matcher
      # @!attribute head
      #   @return [ArrayHeadMatcher]
      # @!attribute tail
      #   @return [ArrayMatcher | EmptyListMatcher]

      def defined_at?(other)
        if other.is_a?(Array)
          head.defined_at?(other) && tail.defined_at?(other.slice(1..-1))
        end
      end

      def bindings(other)
        if head.is_a?(ArraySplatMatcher)
          head.bindings(other)
        else
          head.bindings(other).merge(tail.bindings(other.slice(1..-1)))
        end
      end

      def failure_reason(other)
        if other.is_a?(Array)
          if head.defined_at?(other)
            tail.failure_reason(other.slice(1..-1))
          else
            head.failure_reason(other)
          end
        else
          super
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fear-1.2.0 lib/fear/extractor/array_matcher.rb
fear-1.1.0 lib/fear/extractor/array_matcher.rb