Class: Wood::TreePattern::PatternBuilder

Inherits:
BasicObject
Defined in:
lib/wood/tree_pattern/pattern_builder.rb

Overview

Helper class for building node patterns & replacement actions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PatternBuilder

Returns a new instance of PatternBuilder.



6
7
8
9
10
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 6

def initialize(&block)
  @vars      = []
  @pattern   = instance_eval(&block) if block
  @callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(var_name, var_value = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 32

def method_missing(var_name, var_value = nil)
  if var_value
    add_var PatternVariable.new(var_name, var_value)
    return var_value
  else
    return VariableMatcher.new(self, var_name)
  end
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 4

def pattern
  @pattern
end

#replacement_builderObject (readonly)

Returns the value of attribute replacement_builder.



4
5
6
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 4

def replacement_builder
  @replacement_builder
end

#varsObject (readonly)

Returns the value of attribute vars.



4
5
6
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 4

def vars
  @vars
end

Instance Method Details

#===(node) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 47

def === node
  if val = (pattern === node)
    @callbacks.each do |c|
      c.call(node)
    end
    return val
  end
  return false
end

#_(var_name = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 12

def _(var_name = nil)
  if var_name
    VariableMatcher.new(self, var_name)
  else
    AnyMatcher.new
  end
end

#add_var(var) ⇒ Object



20
21
22
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 20

def add_var(var)
  @vars << var
end

#perform(&block) ⇒ Object



28
29
30
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 28

def perform(&block)
  @callbacks << PatternCallback.new(self, &block)
end

#replacement_for(node) ⇒ Object



41
42
43
44
45
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 41

def replacement_for(node)
  if rb = replacement_builder
    return rb.replacement_for(node)
  end
end

#rewrite(&block) ⇒ Object



24
25
26
# File 'lib/wood/tree_pattern/pattern_builder.rb', line 24

def rewrite(&block)
  @replacement_builder = ReplacementBuilder.new(self, &block)
end