Class: GraphViz::Node

Inherits:
Object
  • 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 Method Summary collapse

Methods included from Constants

getAttrsFor

Constructor Details

#initialize(xNodeName, oGParrent) ⇒ Node

Create a new node

In:

  • xNodeName : ID of the node

  • oGParrent : Graph



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

def initialize( xNodeName, oGParrent )
  @xNodeName = xNodeName
  @oGParrent = oGParrent
  @oAttrNode = 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> )



135
136
137
138
139
# File 'lib/graphviz/node.rb', line 135

def method_missing( idName, *args, &block ) #:nodoc:
  xName = idName.id2name
  
  self[xName.gsub( /=$/, "" )]=args[0]
end

Instance Method Details

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

Create an edge between the current node and the node oNode



105
106
107
108
109
110
111
112
113
# File 'lib/graphviz/node.rb', line 105

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

#[](xAttrName) ⇒ Object

Get the value of the node attribut xAttrName



76
77
78
79
80
81
82
83
84
# File 'lib/graphviz/node.rb', line 76

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

#[]=(xAttrName, xAttrValue) ⇒ Object

Set value xAttrValue to the node attribut xAttrName



68
69
70
71
# File 'lib/graphviz/node.rb', line 68

def []=( xAttrName, xAttrValue )
  xAttrValue = xAttrValue.to_s if xAttrValue.class == Symbol
  @oAttrNode[xAttrName.to_s] = xAttrValue
end

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

Calls block once for each attribut 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 attributs set globally



92
93
94
95
96
97
98
99
100
# File 'lib/graphviz/node.rb', line 92

def each_attribut(global = true, &b)
  attrs = @oAttrNode.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



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

def id
  @xNodeName.clone
end

#indexObject

Return the node index



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

def index
 @index
end

#index=(i) ⇒ Object

:nodoc:



54
55
56
# File 'lib/graphviz/node.rb', line 54

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

#outputObject

:nodoc:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/graphviz/node.rb', line 145

def output #:nodoc:
  #xNodeName = @xNodeName.clone
  #xNodeName = '"' << xNodeName << '"' if xNodeName.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
  xNodeName = GraphViz.escape(@xNodeName)
  
  xOut = "" << xNodeName
  xAttr = ""
  xSeparator = ""
  
  if @oAttrNode.data.has_key?("label") and @oAttrNode.data.has_key?("html")
    @oAttrNode.data.delete("label")
  end
  @oAttrNode.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:



141
142
143
# File 'lib/graphviz/node.rb', line 141

def pg #:nodoc:
  @oGParrent
end

#root_graphObject

Return the root graph



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

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

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

Set node attributs

Example :

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

Yields:

  • (_self)

Yield Parameters:



129
130
131
# File 'lib/graphviz/node.rb', line 129

def set( &b )
  yield( self )
end