Class: Algebrick::Matchers::Product

Inherits:
Abstract show all
Defined in:
lib/algebrick/matchers/product.rb

Instance Attribute Summary (collapse)

Attributes inherited from Abstract

#value

Instance Method Summary (collapse)

Methods inherited from Abstract

#!, #&, #===, #>, #assign!, #assign?, #assign_to_s, #assigned?, #assigns, #case, #children_including_self, #collect_assigns, #find_children, #inspect, #matchable!, #matched?, #to_a, #|

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Constructor Details

- (Product) initialize(algebraic_type, *field_matchers)

Returns a new instance of Product

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

- (Object) algebraic_type (readonly)

Returns the value of attribute algebraic_type



18
19
20
# File 'lib/algebrick/matchers/product.rb', line 18

def algebraic_type
  @algebraic_type
end

- (Object) field_matchers (readonly)

Returns the value of attribute field_matchers



18
19
20
# File 'lib/algebrick/matchers/product.rb', line 18

def field_matchers
  @field_matchers
end

Instance Method Details

- (Object) ==(other)



57
58
59
60
61
# 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

- (Object) children



49
50
51
# File 'lib/algebrick/matchers/product.rb', line 49

def children
  find_children @field_matchers
end

- (Boolean) matching?(other) (protected)

Returns:



65
66
67
68
69
70
# 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

- (Object) to_s



53
54
55
# File 'lib/algebrick/matchers/product.rb', line 53

def to_s
  assign_to_s + "#{@algebraic_type.name}.(#{@field_matchers.join(', ')})"
end