Class: AST2Dot

Inherits:
Object
  • Object
show all
Defined in:
lib/ast2dot.rb,
lib/ast2dot/version.rb,
lib/ast2dot/node_label.rb

Defined Under Namespace

Classes: NodeLabel

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ AST2Dot

Returns a new instance of AST2Dot.



12
13
14
15
# File 'lib/ast2dot.rb', line 12

def initialize(code)
  @ast = RubyVM::AbstractSyntaxTree.parse(code)
  @template = File.read(File.expand_path('templates/default.dot.erb', __dir__))
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

#graph_edgesObject (readonly)

Returns the value of attribute graph_edges.



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

def graph_edges
  @graph_edges
end

#node_labelsObject (readonly)

Returns the value of attribute node_labels.



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

def node_labels
  @node_labels
end

Class Method Details

.render(code) ⇒ Object



6
7
8
# File 'lib/ast2dot.rb', line 6

def self.render(code)
  new(code).render
end

Instance Method Details

#exportObject



17
18
# File 'lib/ast2dot.rb', line 17

def export
end

#renderObject



20
21
22
23
24
25
26
27
28
# File 'lib/ast2dot.rb', line 20

def render
  @graph_edges = []
  @node_labels = Hash.new

  node_labelling(ast)

  graph_data = ERB.new(@template, nil, '-').result(binding)
  graph_data.lines.map(&:rstrip).join("\n")
end