Class: Synvert::Core::Rewriter::GotoScope

Inherits:
Scope
  • Object
show all
Defined in:
lib/synvert/core/rewriter/scope/goto_scope.rb

Overview

Go to and change its scope to a child node.

Instance Method Summary collapse

Constructor Details

#initialize(instance, child_node_name) { ... } ⇒ GotoScope

Initialize a GotoScope.

Parameters:

Yields:

  • run on the child node



11
12
13
14
# File 'lib/synvert/core/rewriter/scope/goto_scope.rb', line 11

def initialize(instance, child_node_name, &block)
  super(instance, &block)
  @child_node_name = child_node_name
end

Instance Method Details

#processObject

Go to a child now, then run the block code on the the child node.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/synvert/core/rewriter/scope/goto_scope.rb', line 17

def process
  current_node = @instance.current_node
  return unless current_node

  child_node = current_node
  @child_node_name.to_s.split('.').each do |child_node_name|
    child_node = child_node.is_a?(Array) && child_node_name =~ /-?\d+/ ? child_node[child_node_name.to_i] : child_node.send(child_node_name)
  end
  if child_node.is_a?(Array)
    child_node.each do |child_child_node|
      @instance.process_with_other_node child_child_node do
        @instance.instance_eval(&@block)
      end
    end
  else
    @instance.process_with_other_node child_node do
      @instance.instance_eval(&@block)
    end
  end
end