Class: Imagen::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/imagen/visitor.rb

Overview

AST Traversal that calls respective builder methods from Imagen::Node

Constant Summary collapse

TYPES =
{
  block: Imagen::Node::Block,
  class: Imagen::Node::Class,
  def: Imagen::Node::IMethod,
  defs: Imagen::Node::CMethod,
  module: Imagen::Node::Module
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_rootObject (readonly)

Returns the value of attribute current_root.



14
15
16
# File 'lib/imagen/visitor.rb', line 14

def current_root
  @current_root
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



14
15
16
# File 'lib/imagen/visitor.rb', line 14

def file_path
  @file_path
end

#rootObject (readonly)

Returns the value of attribute root.



14
15
16
# File 'lib/imagen/visitor.rb', line 14

def root
  @root
end

Class Method Details

.traverse(ast, root) ⇒ Object



16
17
18
19
# File 'lib/imagen/visitor.rb', line 16

def self.traverse(ast, root)
  new.traverse(ast, root)
  root
end

Instance Method Details

#traverse(ast_node, parent) ⇒ Object



21
22
23
24
25
# File 'lib/imagen/visitor.rb', line 21

def traverse(ast_node, parent)
  return unless ast_node.is_a?(Parser::AST::Node)
  node = visit(ast_node, parent)
  ast_node.children.each { |child| traverse(child, node || parent) }
end

#visit(ast_node, parent) ⇒ Object

This method reeks of :reek:UtilityFunction



28
29
30
31
32
33
# File 'lib/imagen/visitor.rb', line 28

def visit(ast_node, parent)
  klass = TYPES[ast_node.type] || return
  klass.new.build_from_ast(ast_node).tap do |node|
    parent.children << node
  end
end