Class: CodeNode::GraphBuilder

Inherits:
Object
  • Object
show all
Includes:
Cog::Generator
Defined in:
lib/code_node/graph_builder.rb

Overview

Helps to build an IR::Graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parser) ⇒ GraphBuilder

Returns a new instance of GraphBuilder.

Parameters:

  • name (String)

    the name of the graph

  • parser (Ruby18Parser, Ruby19Parser)

    a ruby parser instance



16
17
18
19
20
21
# File 'lib/code_node/graph_builder.rb', line 16

def initialize(name, parser)
  @name = name
  @graph = IR::Graph.new
  @parser = parser
  @sexp = [] # array of file sexp, one per file
end

Instance Attribute Details

#graphIR::Graph (readonly)

Returns the graph being built.

Returns:



12
13
14
# File 'lib/code_node/graph_builder.rb', line 12

def graph
  @graph
end

Instance Method Details

#define(&block) ⇒ self

Define the custom graph generation rules

Returns:

  • (self)


30
31
32
33
# File 'lib/code_node/graph_builder.rb', line 30

def define(&block)
  block.call DSL::GraphDefiner.new(@graph)
  self
end

#finalizenil

Apply styles and prune the graph

Returns:

  • (nil)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/code_node/graph_builder.rb', line 53

def finalize
  # Apply styles before pruning because some relations may be destroyed while pruning
  puts "Applying styles"
  @graph.apply_styles

  # Prune the graph according to ignore rules.
  # We keep pruning until there are no more changes because some rules don't apply the first time (for example: &:island?)
  puts "Pruning nodes"
  i = 1
  while (x = @graph.prune) > 0
    puts "  #{x} nodes pruned on #{i.ordinalize} pass"
    i += 1
  end
  self
end

#find_nodesself

Search the sources for nodes

Returns:

  • (self)


37
38
39
40
41
# File 'lib/code_node/graph_builder.rb', line 37

def find_nodes
  puts '1st pass: find nodes'
  find :nodes
  self
end

#find_relationsnil

Search the sources for relations

Returns:

  • (nil)


45
46
47
48
49
# File 'lib/code_node/graph_builder.rb', line 45

def find_relations
  puts '2nd pass: find relations'
  find :relations
  self
end

#rendernil

Render the graph

Returns:

  • (nil)


71
72
73
74
# File 'lib/code_node/graph_builder.rb', line 71

def render
  stamp 'code_node/graph.dot', "#{@name}.dot"
  nil
end

#sourcesArray<String>

Returns paths to ruby source files.

Returns:

  • (Array<String>)

    paths to ruby source files



24
25
26
# File 'lib/code_node/graph_builder.rb', line 24

def sources
  Dir.glob("#{Cog.project_path}/**/*.rb")
end