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



55
56
57
58
59
60
61
62
63
# File 'lib/filigree/visitor.rb', line 55

def method_missing(name, *args)
	if args.empty? and @match_bindings.respond_to?(name)
		@match_bindings.send(name)
	elsif name.to_s[-1] == '=' and args.length == 1
		@match_bindings.send(name, *args)
	else
		super(name, *args)
	end
end

Instance Method Details

#call(*objects) ⇒ Object Also known as: visit

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/filigree/visitor.rb', line 38

def call(*objects)
	self.class.patterns.each do |pattern|
		@match_bindings = OpenStruct.new

		return pattern.(self, objects) if pattern.match?(objects, self)
	end
	
	# If we didn't find anything we raise a MatchError.
	raise MatchError
end