Class: GraphViz::Node

Inherits:
Object show all
Includes:
Constants
Defined in:
lib/graphviz/node.rb

Constant Summary

Constants included from Constants

Constants::EDGESATTRS, Constants::FORMATS, Constants::GENCS_ATTRS, Constants::GRAPHSATTRS, Constants::GRAPHTYPE, Constants::NODESATTRS, Constants::PROGRAMS, Constants::RGV_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

getAttrsFor

Constructor Details

#initialize(node_id, parent_graph) ⇒ Node

Create a new node

  • node_id : ID of the node

  • parent_graph : Graph



33
34
35
36
37
38
39
40
# File 'lib/graphviz/node.rb', line 33

def initialize( node_id, parent_graph )
   @neighbors = []
   @incidents = []
   @node_id = node_id
   @parent_graph = parent_graph
   @node_attributes = GraphViz::Attrs::new( nil, "node", NODESATTRS )
   @index = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(idName, *args, &block) ⇒ Object

Add node options use node.<option>=<value> or node.<option>( <value> )



126
127
128
129
130
# File 'lib/graphviz/node.rb', line 126

def method_missing( idName, *args, &block ) #:nodoc:
   xName = idName.id2name

   self[xName.gsub( /=$/, "" )]=args[0]
end

Instance Attribute Details

#incidentsObject (readonly)

List of nodes that are incident to the given node (in a directed graph neighbors == incidents)



27
28
29
# File 'lib/graphviz/node.rb', line 27

def incidents
  @incidents
end

#neighborsObject (readonly)

List of nodes that are directly accessible from given node (in a directed graph neighbors == incidents)



25
26
27
# File 'lib/graphviz/node.rb', line 25

def neighbors
  @neighbors
end

Instance Method Details

#<<(node) ⇒ Object Also known as: >, -, >>

Create an edge between the current node and the node node



97
98
99
100
101
102
103
104
105
# File 'lib/graphviz/node.rb', line 97

def <<( node )
   if( node.class == Array )
      node.each do |no|
         self << no
      end
   else
      return GraphViz::commonGraph( node, self ).add_edges( self, node )
   end
end

#[](attribute_name) ⇒ Object

Get the value of the node attribute attribute_name



67
68
69
70
71
72
73
74
75
76
# File 'lib/graphviz/node.rb', line 67

def []( attribute_name )
   if Hash === attribute_name
      attribute_name.each do |key, value|
         self[key] = value
      end
      return self
   else
      (@node_attributes[attribute_name.to_s].nil?)?nil:@node_attributes[attribute_name.to_s].clone
   end
end

#[]=(attribute_name, attribute_value) ⇒ Object

Set value attribute_value to the node attribute attribute_name



61
62
63
64
# File 'lib/graphviz/node.rb', line 61

def []=( attribute_name, attribute_value )
   attribute_value = attribute_value.to_s if attribute_value.class == Symbol
   @node_attributes[attribute_name.to_s] = attribute_value
end

#each_attribut(global = true, &b) ⇒ Object



91
92
93
94
# File 'lib/graphviz/node.rb', line 91

def each_attribut(global = true, &b)
   warn "`GraphViz::Node#each_attribut` is deprecated, please use `GraphViz::Node#each_attribute`"
   each_attribute(global, &b)
end

#each_attribute(global = true, &b) ⇒ Object

Calls block once for each attribute of the node, passing the name and value to the block as a two-element array.

If global is set to false, the block does not receive the attributes set globally



82
83
84
85
86
87
88
89
90
# File 'lib/graphviz/node.rb', line 82

def each_attribute(global = true, &b)
   attrs = @node_attributes.to_h
   if global
      attrs = pg.node.to_h.merge attrs
   end
   attrs.each do |k,v|
      yield(k,v)
   end
end

#idObject

Get the node ID



43
44
45
# File 'lib/graphviz/node.rb', line 43

def id
   @node_id.clone
end

#indexObject

Return the node index



48
49
50
# File 'lib/graphviz/node.rb', line 48

def index
   @index
end

#index=(i) ⇒ Object

:nodoc:



51
52
53
# File 'lib/graphviz/node.rb', line 51

def index=(i) #:nodoc:
   @index = i if @index == nil
end

#outputObject

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/graphviz/node.rb', line 136

def output #:nodoc:
   # reserved words, they aren't accepted in dot as node name
   reserved_names = ["node", "edge","graph", "digraph", "subgraph", "strict"]
   #node_id = @node_id.clone
   #node_id = '"' << node_id << '"' if node_id.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
   node_id = GraphViz.escape(@node_id)

   # add a check to see if the node names are valid
   # if they aren't is added an _ before
   # and the print staies the same, because of the label
   xOut = reserved_names.include?(node_id) ? "" << "_" + node_id : "" << node_id
   xAttr = ""
   xSeparator = ""

   if @node_attributes.data.has_key?("label") and @node_attributes.data.has_key?("html")
      @node_attributes.data.delete("label")
   end
   @node_attributes.data.each do |k, v|
      xAttr << xSeparator + k + " = " + v.to_gv
      xSeparator = ", "
   end
   if xAttr.length > 0
      xOut << " [" + xAttr + "]"
   end
   xOut << ";"

   return( xOut )
end

#pgObject

:nodoc:



132
133
134
# File 'lib/graphviz/node.rb', line 132

def pg #:nodoc:
   @parent_graph
end

#root_graphObject

Return the root graph



56
57
58
# File 'lib/graphviz/node.rb', line 56

def root_graph
   return( (self.pg.nil?) ? nil : self.pg.root_graph )
end

#set {|_self| ... } ⇒ Object

Set node attributes

Example :

n = graph.add_nodes( ... )
...
n.set { |_n|
  _n.color = "blue"
  _n.fontcolor = "red"
}

Yields:

  • (_self)

Yield Parameters:



120
121
122
# File 'lib/graphviz/node.rb', line 120

def set( &b )
   yield( self )
end