Class: CodeNode::GraphBuilder

Inherits:
Object
  • Object
show all
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



14
15
16
17
18
19
# File 'lib/code_node/graph_builder.rb', line 14

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:



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

def graph
  @graph
end

Instance Method Details

#define(&block) ⇒ self

Define the custom graph generation rules

Returns:

  • (self)


28
29
30
31
# File 'lib/code_node/graph_builder.rb', line 28

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

#finalizenil

Apply styles and prune the graph

Returns:

  • (nil)


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

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)


35
36
37
38
39
# File 'lib/code_node/graph_builder.rb', line 35

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

#find_relationsnil

Search the sources for relations

Returns:

  • (nil)


43
44
45
46
47
# File 'lib/code_node/graph_builder.rb', line 43

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

#rendernil

Render the graph

Returns:

  • (nil)


69
70
71
72
73
74
75
76
# File 'lib/code_node/graph_builder.rb', line 69

def render
  # Activate code_node while rendering templates
  # so that cog will be able to find code_node templates
  Cog.activate_tool 'code_node' do
    stamp 'code_node/graph.dot', "#{@name}.dot"
  end
  nil
end

#sourcesArray<String>

Returns paths to ruby source files.

Returns:

  • (Array<String>)

    paths to ruby source files



22
23
24
# File 'lib/code_node/graph_builder.rb', line 22

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