Method: Filigree::Visitor#visit

Defined in:
lib/filigree/visitor.rb

#visit(*objects) ⇒ Object, MatchError

Find the correct pattern and execute its block on the provided objects.

Parameters:

  • objects (Object)

    Objects to pattern match.

Returns:

  • (Object, MatchError)

    Result of calling the matched pattern’s block or MatchError if nothing was found

Raises:

  • (MatchError)

    Raised when no matching pattern is found and strict matching is enabled.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/filigree/visitor.rb', line 39

def visit(*objects)
  # FIXME: A dirty hack.  Find a better place to initialize this.
  @match_bindings ||= Array.new

  @match_bindings.push OpenStruct.new

  self.class.patterns.each do |pattern|

    # FIXME: Make these take their arguments in the same order
    if pattern.match?(objects, self)
      result = pattern.(self, objects)
      @match_bindings.pop
      return result
    end
  end

  @match_bindings.pop

  if self.class.strict_match?
    # If we didn't find anything we raise a MatchError.
    raise MatchError
  else
    MatchError
  end
end