Module: Filigree::Visitor::ClassMethods

Defined in:
lib/filigree/visitor.rb

Overview

Class Methods #

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Used to generate wildcard and binding patterns.



126
127
128
129
130
131
132
# File 'lib/filigree/visitor.rb', line 126

def method_missing(name, *args)
  if args.empty?
    if name == :_ then WildcardPattern.instance else BindingPattern.new(name) end
  else
    super(name, *args)
  end
end

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



71
72
73
# File 'lib/filigree/visitor.rb', line 71

def patterns
  @patterns
end

Class Method Details

.extended(klass) ⇒ Object



134
135
136
# File 'lib/filigree/visitor.rb', line 134

def self.extended(klass)
  klass.install_icvars
end

Instance Method Details

#Bind(name) ⇒ BindingPattern

Force a name binding.

Parameters:

  • name (Symbol)

    Name to bind to

Returns:



78
79
80
# File 'lib/filigree/visitor.rb', line 78

def Bind(name)
  BindingPattern.new(name)
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



94
95
96
97
# File 'lib/filigree/visitor.rb', line 94

def install_icvars
  @patterns = Array.new
  @deferred = Array.new
end

#Literal(obj) ⇒ LiteralPattern

Force a literal comparison.

Parameters:

  • obj (Object)

    Object to be comapred against

Returns:



87
88
89
# File 'lib/filigree/visitor.rb', line 87

def Literal(obj)
  LiteralPattern.new(obj)
end

#on(*pattern, &block) ⇒ void

This method returns an undefined value.

Define a pattern for this visitor.

Parameters:

  • pattern (Object)

    List of pattern elements

  • block (Proc)

    Block to be executed when the pattern is matched

See Also:

  • Pattern matching description


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/filigree/visitor.rb', line 107

def on(*pattern, &block)
  guard = if pattern.last.is_a?(Proc) then pattern.pop end 
    
  @patterns << (mp = OuterPattern.new(pattern, guard, block))
    
  if block
    @deferred.each { |pattern| pattern.block = block }
    @deferred.clear

  else
    @deferred << mp
  end
end