class Algebrick::Matchers::Product

Attributes

algebraic_type[R]
field_matchers[R]

Public Class Methods

new(algebraic_type, *field_matchers) click to toggle source
Calls superclass method Algebrick::Matchers::Abstract.new
# File lib/algebrick/matchers/product.rb, line 20
def initialize(algebraic_type, *field_matchers)
  super()
  @algebraic_type = Type! algebraic_type, Algebrick::ProductVariant, Algebrick::ParametrizedType
  raise ArgumentError unless algebraic_type.fields
  @field_matchers = case

                      # AProduct.()
                    when field_matchers.empty?
                      ::Array.new(algebraic_type.fields.size) { Algebrick.any }

                      # AProduct.(field_name: a_matcher)
                    when field_matchers.size == 1 && field_matchers.first.is_a?(Hash)
                      field_matchers = field_matchers.first
                      unless (dif = field_matchers.keys - algebraic_type.field_names).empty?
                        raise ArgumentError, "no #{dif} fields in #{algebraic_type}"
                      end
                      algebraic_type.field_names.map do |field|
                        field_matchers.key?(field) ? field_matchers[field] : Algebrick.any
                      end

                      # normal
                    else
                      field_matchers
                    end
  unless algebraic_type.fields.size == @field_matchers.size
    raise ArgumentError
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/algebrick/matchers/product.rb, line 57
def ==(other)
  other.kind_of? self.class and
      self.algebraic_type == other.algebraic_type and
      self.field_matchers == other.field_matchers
end
children() click to toggle source
# File lib/algebrick/matchers/product.rb, line 49
def children
  find_children @field_matchers
end
to_s() click to toggle source
# File lib/algebrick/matchers/product.rb, line 53
def to_s
  assign_to_s + "#{@algebraic_type.name}.(#{@field_matchers.join(', ')})"
end

Protected Instance Methods

matching?(other) click to toggle source
# File lib/algebrick/matchers/product.rb, line 65
def matching?(other)
  other.kind_of?(@algebraic_type) and other.kind_of?(ProductConstructors::Abstract) and
      @field_matchers.zip(other.fields).all? do |matcher, field|
        matcher === field
      end
end