Class: CodeNode::DSL::GraphDefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/code_node/dsl/graph_definer.rb

Overview

Specify rules for generating the graph

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ GraphDefiner

Returns a new instance of GraphDefiner.

Parameters:

  • graph (IR::Graph)

    a graph for which rules can be defined



11
12
13
# File 'lib/code_node/dsl/graph_definer.rb', line 11

def initialize(graph)
  @graph = graph
end

Instance Method Details

#ignore(name = nil) {|node| ... } ⇒ nil

Specify an ignore rule for the graph. Nodes matching the ignore rule will not be included in the graph. Ignore rules can be given in one of three ways,

Examples:

CodeNode.graph 'my_graph' do |g|

  # Using a full path
  g.ignore 'Foo::Bar::Car'

  # Using a regular expression
  g.ignore /ClassMethods$/

  # Using a block
  g.ignore do |node|
    node.inherits_from? 'Exception'
  end
end

Parameters:

  • name (String, Regexp, nil) (defaults to: nil)

    fully qualified path or regular expression which will be compared to IR::Node::QueryMethods#path

Yield Parameters:

Returns:

  • (nil)


40
41
42
43
44
45
46
47
# File 'lib/code_node/dsl/graph_definer.rb', line 40

def ignore(name=nil, &block)
  if (name.nil? && block.nil?) || (name && block)
    raise ArgumentError.new('Provide either a name or a block') 
  end
  matcher = IR::NodeMatcher.new name || block
  @graph.instance_eval {@exclude_matchers << matcher}
  nil
end

#style(name = nil, style = {}) {|node| ... } ⇒ nil

Specify a rule for styling nodes. Nodes matching the given rule will have the provided style attributes applied. Rules can be given in one of three ways,

Examples:

CodeNode.graph 'my_graph' do |g|

  # Using a full path
  g.style 'Foo::Bar::Car', :shape => 'box'

  # Using a regular expression
  g.style /ClassMethods$/, :fillcolor => '#336699'

  # Using a block
  g.style :penwidth => 3 do |node|
    node.extends? 'ActiveSupport::Concern'
  end
end

Parameters:

  • name (String, Regexp, nil) (defaults to: nil)

    fully qualified path or regular expression which will be compared to IR::Node::QueryMethods#path

  • style (Hash) (defaults to: {})

    a set of attributes and values to apply to matching nodes. For a full list of applicable to GraphViz nodes: www.graphviz.org/content/attrs

Yield Parameters:

Returns:

  • (nil)


75
76
77
78
79
80
81
82
83
# File 'lib/code_node/dsl/graph_definer.rb', line 75

def style(name=nil, style={}, &block)
  style, name = name, nil if name.is_a?(Hash)
  if (name.nil? && block.nil?) || (name && block)
    raise ArgumentError.new('Provide either a name or a block') 
  end
  matcher = IR::NodeMatcher.new name || block
  @graph.instance_eval {@style_matchers << [matcher, style]}
  nil
end