Class: Algebrick::Matchers::Array

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

Instance Attribute Summary (collapse)

Attributes inherited from Abstract

#value

Class Method Summary (collapse)

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

- (Array) initialize(*matchers)

Returns a new instance of Array

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/algebrick/matchers/array.rb', line 24

def initialize(*matchers)
  super()
  @matchers = matchers
  raise ArgumentError, 'many can be only last' if @matchers[0..-2].any? { |v| v.is_a?(Many) }
end

Instance Attribute Details

- (Object) matchers (readonly)

Returns the value of attribute matchers



22
23
24
# File 'lib/algebrick/matchers/array.rb', line 22

def matchers
  @matchers
end

Class Method Details

+ (Object) call(*matchers)



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

def self.call(*matchers)
  new *matchers
end

Instance Method Details

- (Object) ==(other)



38
39
40
41
# File 'lib/algebrick/matchers/array.rb', line 38

def ==(other)
  other.kind_of? self.class and
      self.matchers == other.matchers
end

- (Object) children



30
31
32
# File 'lib/algebrick/matchers/array.rb', line 30

def children
  find_children @matchers
end

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

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'lib/algebrick/matchers/array.rb', line 49

def matching?(other)
  return false unless other.kind_of? ::Array
  if rest?
    matchers[0..-2].zip(other).all? { |m, v| m === v } and
        matchers.last === other[(matchers.size-1)..-1]
  else
    matchers.size == other.size and
        matchers.zip(other).all? { |m, v| m === v }
  end
end

- (Boolean) rest?

Returns:



43
44
45
# File 'lib/algebrick/matchers/array.rb', line 43

def rest?
  matchers.last.is_a?(Many)
end

- (Object) to_s



34
35
36
# File 'lib/algebrick/matchers/array.rb', line 34

def to_s
  "#{assign_to_s}#{"Array.(#{matchers.join(',')})" if matchers}"
end