Module: Filigree::Visitor

Includes:
ClassMethodsModule
Defined in:
lib/filigree/visitor.rb

Overview

An implementation of the Visitor pattern.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethodsModule

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

This is used to get and set binding names



70
71
72
73
74
75
76
77
78
# 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

Instance Method Details

#visit(*objects) ⇒ Object

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

Parameters:

  • objects (Object)

    Objects to pattern match.

Returns:

  • (Object)

    Result of calling the matched pattern’s block

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
		nil
	end
end