class Algebrick::Matchers::Abstract

Attributes

value[R]

Public Class Methods

new() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 21
def initialize
  @assign, @value, @matched = nil
end

Public Instance Methods

!() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 52
def !
  Not.new self
end
&(matcher) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 44
def &(matcher)
  And.new self, matcher
end
==(other) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 102
def ==(other)
  raise NotImplementedError
end
===(other) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 82
def ===(other)
  matching?(other).tap { |matched| @value = other if (@matched = matched) }
end
>(block) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 31
def >(block)
  return self, block
end
Also aliased as: >>
>>(block)
Alias for: >
assign!() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 37
def assign!
  @assign = true
  self
end
Also aliased as: ~
assign?() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 56
def assign?
  @assign
end
assign_to_s() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 86
def assign_to_s
  assign? ? '~' : ''
end
assigned?() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 60
def assigned?
  !!@value
end
assigns() { |*assigns| ... } click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 72
def assigns
  collect_assigns.tap do
    return yield *assigns if block_given?
  end
end
case(&block) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 25
def case(&block)
  return self, block
end
Also aliased as: when
children() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 94
def children
  raise NotImplementedError
end
children_including_self() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 68
def children_including_self
  children.unshift self
end
inspect() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 90
def inspect
  to_s
end
matched?() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 64
def matched?
  @matched
end
to_a() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 78
def to_a
  assigns
end
to_s() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 98
def to_s
  raise NotImplementedError
end
when(&block)
Alias for: case
|(matcher) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 48
def |(matcher)
  Or.new self, matcher
end
~()
Alias for: assign!

Protected Instance Methods

matching?(other) click to toggle source

TODO pretty_print for all matchers

# File lib/algebrick/matchers/abstract.rb, line 110
def matching?(other)
  raise NotImplementedError
end

Private Instance Methods

collect_assigns() click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 116
def collect_assigns
  mine = @assign ? [@value] : []
  children.inject(mine) { |assigns, child| assigns + child.assigns }
end
find_children(collection) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 126
def find_children(collection)
  collection.map do |matcher|
    matcher if matcher.kind_of? Abstract
  end.compact
end
matchable!(obj) click to toggle source
# File lib/algebrick/matchers/abstract.rb, line 121
def matchable!(obj)
  raise ArgumentError, 'object does not respond to :===' unless obj.respond_to? :===
  obj
end