module Filigree::Visitor
An implementation of the Visitor
pattern.
Public Instance Methods
method_missing(name, *args)
click to toggle source
This is used to get and set binding names
Calls superclass method
# File lib/filigree/visitor.rb, line 70 def method_missing(name, *args) if args.empty? and @match_bindings.last.respond_to?(name) @match_bindings.last.send(name) elsif name.to_s[-1] == '=' and args.length == 1 @match_bindings.last.send(name, *args) else super(name, *args) end end
visit(*objects)
click to toggle source
Find the correct pattern and execute its block on the provided objects.
@param [Object] objects Objects to pattern match.
@return [Object, MatchError] Result of calling the matched pattern's block or MatchError
if nothing was found
@raise [MatchError] Raised when no matching pattern is found and
strict matching is enabled.
# 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