Class: Synvert::Rewriter::Scope
- Inherits:
-
Object
- Object
- Synvert::Rewriter::Scope
- Defined in:
- lib/synvert/rewriter/scope.rb
Overview
Scope finds the child nodes which match rules.
Instance Method Summary collapse
-
#initialize(instance, rules, &block) ⇒ Scope
constructor
Initialize a scope.
-
#process ⇒ Object
Find the matching nodes.
Constructor Details
#initialize(instance, rules, &block) ⇒ Scope
Initialize a scope
11 12 13 14 15 |
# File 'lib/synvert/rewriter/scope.rb', line 11 def initialize(instance, rules, &block) @instance = instance @rules = rules @block = block end |
Instance Method Details
#process ⇒ Object
Find the matching nodes. It checks the current node and iterates all child nodes, then run the block code for each matching node.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/synvert/rewriter/scope.rb', line 19 def process current_node = @instance.current_node return unless current_node process_with_node current_node do matching_nodes = [] matching_nodes << current_node if current_node.match? @instance, @rules current_node.recursive_children do |child_node| matching_nodes << child_node if child_node.match? @instance, @rules end matching_nodes.each do |matching_node| process_with_node matching_node do @instance.instance_eval &@block end end end end |