Class: Rattler::Util::GraphViz::NodeBuilder

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

Author:

  • Jason Arhart

Defined Under Namespace

Classes: Mapping

Instance Method Summary collapse

Instance Method Details

#array_like?(o) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/rattler/util/graphviz/node_builder.rb', line 74

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

#each_child_of(o) ⇒ Object

Yield any children of o that should be represented as separate nodes in the graph.



20
21
22
23
24
25
26
27
28
# File 'lib/rattler/util/graphviz/node_builder.rb', line 20

def each_child_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

#hash_content_labels(h) ⇒ Object



88
89
90
# File 'lib/rattler/util/graphviz/node_builder.rb', line 88

def (h)
  h.map {|pair| '{' + pair.map {|_| _.inspect }.join('|') + '}' }
end

#node_label(o) ⇒ Object

Return the label option for a node representing o.

Returns:

  • the label option for a node representing o.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rattler/util/graphviz/node_builder.rb', line 51

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
    "\"#{o}\""
  else
    o.inspect
  end
end

#node_options(o) ⇒ Object

Return the options for a node representing o.

Returns:

  • the options for a node representing o.



32
33
34
# File 'lib/rattler/util/graphviz/node_builder.rb', line 32

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

#node_shape(o) ⇒ Object

Return the shape option for a node representing o.

Returns:

  • the shape option for a node representing o.



38
39
40
41
42
43
44
45
46
47
# File 'lib/rattler/util/graphviz/node_builder.rb', line 38

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

#parse_node_fields(attrs) ⇒ Object



92
93
94
# File 'lib/rattler/util/graphviz/node_builder.rb', line 92

def parse_node_fields(attrs)
  attrs.reject {|k,| k == :name || k == :labeled }
end

#record_label(o, fields) ⇒ Object



84
85
86
# File 'lib/rattler/util/graphviz/node_builder.rb', line 84

def record_label(o, fields)
  '{' + ([type_label(o)] + (fields)).join('|') + '}'
end

#record_like?(o) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/rattler/util/graphviz/node_builder.rb', line 79

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

#type_label(o) ⇒ Object



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

def type_label(o)
  case o
  when Hash then '\\{\\}'
  when Array then '\\[\\]'
  when Mapping then '->'
  else o.respond_to?(:name) ? o.name : o.class.name
  end
end