Class: RGL::DOT::Node

Inherits:
Element show all
Defined in:
lib/rgl/rdot.rb

Overview

A node representation. Edges are drawn between nodes. The rendering of a node depends upon the options set for it.

Instance Attribute Summary collapse

Attributes inherited from Element

#name, #options

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, option_list = NODE_OPTS) ⇒ Node

Creates a new Node with the params Hash providing settings for all node options. The option_list parameter restricts those options to the list of valid names it contains. The exception to this is the ports option which, if specified, must be an Enumerable containing a list of ports.



233
234
235
236
# File 'lib/rgl/rdot.rb', line 233

def initialize (params = {}, option_list = NODE_OPTS)
  super(params, option_list)
  @ports = params['ports'] ? params['ports'] : []
end

Instance Attribute Details

#portsObject

Returns the value of attribute ports.



226
227
228
# File 'lib/rgl/rdot.rb', line 226

def ports
  @ports
end

Instance Method Details

#to_s(leader = '', indent = ' ') ⇒ Object

Returns a string representation of this node which is consumable by the graphviz tools dot and neato. The leader parameter is used to indent every line of the returned string, and the indent parameter is used to additionally indent nested items.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/rgl/rdot.rb', line 242

def to_s (leader = '', indent = '    ')
  label_option = nil
  if @options['shape'] =~ /^M?record$/ && !@ports.empty? then
    # Ignore the given label option in this case since the ports should each
    # provide their own name/label.
    label_option = leader + indent + "#{quote_ID('label')} = #{quote_ID(@ports.collect { |port| port.to_s }.join(" | "))}"
  elsif @options['label'] then
    # Otherwise, use the label when given one.
    label_option = leader + indent + "#{quote_ID('label')} = #{quote_label(@options['label'])}"
  end

  # Convert all the options except `label' and options with nil values
  # straight into name = value pairs.  Then toss out any resulting nil
  # entries in the final array.
  stringified_options = @options.collect do |name, val|
    unless name == 'label' || val.nil? then
      leader + indent + "#{quote_ID(name)} = #{quote_ID(val)}"
    end
  end.compact
  # Append the specially computed label option.
  stringified_options.push(label_option) unless label_option.nil?
  # Join them all together.
  stringified_options = stringified_options.join(",\n")

  # Put it all together into a single string with indentation and return the
  # result.
  if stringified_options.empty? then
    return leader + quote_ID(@name) unless @name.nil?
    return nil
  else
    return leader + (@name.nil? ? '' : quote_ID(@name) + " ") + "[\n" +
      stringified_options + "\n" +
      leader + "]"
  end
end