Class: CodeNode::GraphBuilder
- Inherits:
-
Object
- Object
- CodeNode::GraphBuilder
- Includes:
- Cog::Generator
- Defined in:
- lib/code_node/graph_builder.rb
Overview
Helps to build an IR::Graph
Instance Attribute Summary collapse
-
#graph ⇒ IR::Graph
readonly
The graph being built.
Instance Method Summary collapse
-
#define(&block) ⇒ self
Define the custom graph generation rules.
-
#finalize ⇒ nil
Apply styles and prune the graph.
-
#find_nodes ⇒ self
Search the sources for nodes.
-
#find_relations ⇒ nil
Search the sources for relations.
-
#initialize(name, parser) ⇒ GraphBuilder
constructor
A new instance of GraphBuilder.
-
#render ⇒ nil
Render the graph.
-
#sources ⇒ Array<String>
Paths to ruby source files.
Constructor Details
#initialize(name, parser) ⇒ GraphBuilder
Returns a new instance of GraphBuilder.
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
#graph ⇒ IR::Graph (readonly)
Returns the graph being built.
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
30 31 32 33 |
# File 'lib/code_node/graph_builder.rb', line 30 def define(&block) block.call DSL::GraphDefiner.new(@graph) self end |
#finalize ⇒ nil
Apply styles and prune the graph
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_nodes ⇒ self
Search the sources for nodes
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_relations ⇒ nil
Search the sources for relations
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 |
#render ⇒ nil
Render the graph
71 72 73 74 |
# File 'lib/code_node/graph_builder.rb', line 71 def render stamp 'code_node/graph.dot', "#{@name}.dot" nil end |
#sources ⇒ Array<String>
Returns 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 |