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.



154
155
156
157
158
159
160
# File 'lib/filigree/visitor.rb', line 154

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



162
163
164
# File 'lib/filigree/visitor.rb', line 162

def self.extended(klass)
  klass.install_icvars
end

Instance Method Details

#add_pattern(new_pat) ⇒ void

This method returns an undefined value.

Inserts a new pattern in the appropriate place in the patterns list.

Parameters:



97
98
99
100
101
102
103
104
105
106
# File 'lib/filigree/visitor.rb', line 97

def add_pattern(new_pat)
  @patterns.each_with_index do |old_pat, index|
    if new_pat > old_pat
      @patterns.insert(index, new_pat)
      return
    end
  end
  
  @patterns << new_pat
end

#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

#inherited(klass) ⇒ void

This method returns an undefined value.

A callback used to pass patterns declared in a parent class to a subclass.

Parameters:

  • klass (Class)

    Subclass



114
115
116
# File 'lib/filigree/visitor.rb', line 114

def inherited(klass)
  klass.install_icvars(@patterns.clone)
end

#install_icvars(inherited_patterns = Array.new) ⇒ void

This method returns an undefined value.

Install the instance class variables in the including class.



121
122
123
124
# File 'lib/filigree/visitor.rb', line 121

def install_icvars(inherited_patterns = Array.new)
  @patterns = inherited_patterns
  @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


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/filigree/visitor.rb', line 134

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

  else
    @deferred << mp
  end
end