Class: Imagen::Node::Base

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

Overview

Abstract base class

Direct Known Subclasses

Block, CMethod, Class, IMethod, Module, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  @children = []
end

Instance Attribute Details

#ast_nodeObject (readonly)

Returns the value of attribute ast_node.



10
11
12
# File 'lib/imagen/node.rb', line 10

def ast_node
  @ast_node
end

#childrenObject (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/imagen/node.rb', line 10

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/imagen/node.rb', line 10

def name
  @name
end

Instance Method Details

#build_from_ast(ast_node) ⇒ Object



22
23
24
# File 'lib/imagen/node.rb', line 22

def build_from_ast(ast_node)
  tap { @ast_node = ast_node }
end

#file_pathObject



26
27
28
# File 'lib/imagen/node.rb', line 26

def file_path
  ast_node.location.name.source_buffer.name
end

#find_all(matcher, ret = []) ⇒ Object



57
58
59
60
61
62
# File 'lib/imagen/node.rb', line 57

def find_all(matcher, ret = [])
  ret.tap do
    ret << self if matcher.call(self)
    children.each { |child| child.find_all(matcher, ret) }
  end
end

#first_lineObject



30
31
32
# File 'lib/imagen/node.rb', line 30

def first_line
  ast_node.location.first_line
end

#human_nameObject

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/imagen/node.rb', line 18

def human_name
  raise NotImplementedError
end

#last_lineObject



38
39
40
# File 'lib/imagen/node.rb', line 38

def last_line
  ast_node.location.last_line
end

#line_numbersObject



34
35
36
# File 'lib/imagen/node.rb', line 34

def line_numbers
  (first_line..last_line).to_a
end

#sourceObject



42
43
44
# File 'lib/imagen/node.rb', line 42

def source
  source_lines.join("\n")
end

#source_linesObject



50
51
52
53
54
55
# File 'lib/imagen/node.rb', line 50

def source_lines
  ast_node.location.expression.source_buffer.source_lines[
    first_line - 1,
    last_line
  ]
end

#source_lines_with_numbersObject



46
47
48
# File 'lib/imagen/node.rb', line 46

def source_lines_with_numbers
  (first_line..last_line).zip(source_lines)
end