Module: Rattler::Util::GraphViz::NodeBuilder

Extended by:
NodeBuilder
Included in:
DigraphBuilder, NodeBuilder
Defined in:
lib/rattler/util/graphviz/node_builder.rb

Overview

NodeBuilder is used by DigraphBuilder to build nodes for a GraphViz digraph object representing a tree of nodes.

Defined Under Namespace

Classes: Mapping

Instance Method Summary collapse

Instance Method Details

#array_like?(o) ⇒ Boolean

Returns whether o should be represented as an array.

Parameters:

  • o

    an object

Returns:

  • (Boolean)

    whether o should be represented as an array



69
70
71
72
# File 'lib/rattler/util/graphviz/node_builder.rb', line 69

def array_like?(o)
  o.respond_to? :each and
  not o.respond_to? :to_str
end

#each_child_node_of(o) {|child| ... } ⇒ Object

Run the block with any children of o that should be represented as separate nodes in the graph.

Parameters:

  • o

    an object

Yields:

  • (child)

    each child of o that should be represented as a separate node in the graph



15
16
17
18
19
20
21
22
23
# File 'lib/rattler/util/graphviz/node_builder.rb', line 15

def each_child_node_of(o)
  if array_like? o and not record_like? o
    if o.respond_to? :to_hash
      o.each {|k, v| yield Mapping.new(k, v) }
    else
      o.each {|_| yield _ }
    end
  end
end

#node_label(o) ⇒ Object

Returns the label option for a node representing o.

Parameters:

  • o

    an object

Returns:

  • the label option for a node representing o.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rattler/util/graphviz/node_builder.rb', line 46

def node_label(o)
  if o.is_a? ::Rattler::Util::Node
    record_label(o, parse_node_fields(o.attrs))
  elsif record_like? o
    record_label(o, o)
  elsif array_like? o
    type_label(o)
  elsif o.respond_to? :to_str
    string_label(o)
  else
    o.inspect
  end
end

#node_options(o) ⇒ Object

Returns the options for a node representing o.

Parameters:

  • o

    an object

Returns:

  • the options for a node representing o.



27
28
29
# File 'lib/rattler/util/graphviz/node_builder.rb', line 27

def node_options(o)
  { :shape => node_shape(o), :label => node_label(o) }
end

#node_shape(o) ⇒ Object

Returns the shape option for a node representing o.

Parameters:

  • o

    an object

Returns:

  • the shape option for a node representing o.



33
34
35
36
37
38
39
40
41
42
# File 'lib/rattler/util/graphviz/node_builder.rb', line 33

def node_shape(o)
  case o
  when Array, Hash, Mapping
    'circle'
  when String, Numeric, Symbol
    'plaintext'
  else
    'Mrecord'
  end
end

#record_like?(o) ⇒ Boolean

Returns whether o should be represented as a record.

Parameters:

  • o

    an object

Returns:

  • (Boolean)

    whether o should be represented as a record



62
63
64
65
# File 'lib/rattler/util/graphviz/node_builder.rb', line 62

def record_like?(o)
  o.respond_to? :each_pair and
  o.none? {|k, v| array_like? v or record_like? v }
end