module Rattler::Util::GraphViz::NodeBuilder

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

Public Instance Methods

array_like?(o) click to toggle source

@param o an object @return whether o should be represented as an array

# 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) { |mapping| ... } click to toggle source

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

@param o an object @yield [child] each child of o that should be represented as a separate

node in the graph
# 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) click to toggle source

@param o an object @return the label option for a node representing o.

# 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) click to toggle source

@param o an object @return the options for a node representing o.

# 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) click to toggle source

@param o an object @return the shape option for a node representing o.

# 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) click to toggle source

@param o an object @return whether o should be represented as a record

# 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

Private Instance Methods

hash_content_labels(h) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 84
def hash_content_labels(h)
  h.map {|k, v| "{#{k.inspect}|#{record_value v}}" }
end
parse_node_fields(attrs) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 105
def parse_node_fields(attrs)
  attrs.reject {|k,| k == :name || k == :labeled }
end
record_label(o, fields) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 80
def record_label(o, fields)
  '{' + ([type_label(o)] + hash_content_labels(fields)).join('|') + '}'
end
record_value(v) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 88
def record_value(v)
  if v.respond_to? :to_str
    string_label(v)
  else
    v.inspect
  end.gsub(/([|<>\\])/) { "\\#{$1}" }
end
string_label(s) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 76
def string_label(s)
  "\"#{s}\""
end
type_label(o) click to toggle source
# File lib/rattler/util/graphviz/node_builder.rb, line 96
def type_label(o)
  case o
  when Hash then '\\{\\}'
  when Array then '\\[\\]'
  when Mapping then '-&gt;'
  else o.respond_to?(:name) ? o.name : o.class.name
  end
end