Class: Mongo3::Node
- Inherits:
-
Object
- Object
- Mongo3::Node
- Defined in:
- lib/mongo3/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#data ⇒ Object
Returns the value of attribute data.
-
#name ⇒ Object
Returns the value of attribute name.
-
#oid ⇒ Object
Returns the value of attribute oid.
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
-
.dump(node, level = 0) ⇒ Object
Dump nodes to stdout.
-
.dump_adj(adjs, level = 0) ⇒ Object
Dump adjancencies to stdout.
- .make_node(name) ⇒ Object
Instance Method Summary collapse
-
#<<(new_one) ⇒ Object
Add a child node.
- #_find(node, node_id) ⇒ Object
- #_to_adjacencies(node, cltn) ⇒ Object
- #find(node_id) ⇒ Object
-
#initialize(oid, name, data = nil) ⇒ Node
constructor
A new instance of Node.
- #mark_master! ⇒ Object
- #mark_slave! ⇒ Object
- #master? ⇒ Boolean
- #slave? ⇒ Boolean
-
#to_adjacencies ⇒ Object
convert a tree node to a set of adjacencies.
-
#to_json(*a) ⇒ Object
converts to json.
Constructor Details
#initialize(oid, name, data = nil) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 14 |
# File 'lib/mongo3/node.rb', line 8 def initialize( oid, name, data=nil ) @oid = oid @name = name @children = [] @data = data || OrderedHash.new @parent = nil end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
6 7 8 |
# File 'lib/mongo3/node.rb', line 6 def children @children end |
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/mongo3/node.rb', line 6 def data @data end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/mongo3/node.rb', line 6 def name @name end |
#oid ⇒ Object
Returns the value of attribute oid.
6 7 8 |
# File 'lib/mongo3/node.rb', line 6 def oid @oid end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/mongo3/node.rb', line 6 def parent @parent end |
Class Method Details
.dump(node, level = 0) ⇒ Object
Dump nodes to stdout
92 93 94 95 |
# File 'lib/mongo3/node.rb', line 92 def self.dump( node, level=0 ) puts ' '*level + "%-#{20-level}s (%d) [%s] -- %s" % [node.oid, node.children.size, node.name, (node.data ? node.data.inspect : 'N/A' )] node.children.each { |c| dump( c, level+1 ) } end |
.dump_adj(adjs, level = 0) ⇒ Object
Dump adjancencies to stdout
98 99 100 101 102 |
# File 'lib/mongo3/node.rb', line 98 def self.dump_adj( adjs, level = 0 ) adjs.each do |adj| puts ' '*level + "%-#{20-level}s (%d) [%s] -- %s" % [adj[:id], adj[:adjacencies].size, adj[:name], (adj[:data] ? adj[:data].inspect : 'N/A' )] end end |
Instance Method Details
#<<(new_one) ⇒ Object
Add a child node
43 44 45 46 47 |
# File 'lib/mongo3/node.rb', line 43 def <<( new_one ) new_one.parent = self @children << new_one update_paths( new_one ) end |
#_find(node, node_id) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/mongo3/node.rb', line 53 def _find( node, node_id ) return node if node.oid == node_id unless node.children.empty? node.children.each { |c| found = _find( c, node_id ); return found if found } end nil end |
#_to_adjacencies(node, cltn) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mongo3/node.rb', line 68 def _to_adjacencies( node, cltn ) node_level = { :id => node.oid, :name => node.name, :data => node.data, :adjacencies => [] } cltn << node_level node.children.each do |child| node_level[:adjacencies] << child.oid _to_adjacencies( child, cltn ) # cltn << { :id => child.oid, :name => child.name, :data => child.data, :adjacencies => [] } end # cltn end |
#find(node_id) ⇒ Object
49 50 51 |
# File 'lib/mongo3/node.rb', line 49 def find( node_id ) _find( self, node_id ) end |
#mark_master! ⇒ Object
27 28 29 30 31 32 |
# File 'lib/mongo3/node.rb', line 27 def mark_master! data[:master] = true data['$color'] = '#92b948' data['$dim'] = 15 data['$lineWidth'] = 3 end |
#mark_slave! ⇒ Object
20 21 22 23 24 25 |
# File 'lib/mongo3/node.rb', line 20 def mark_slave! data[:slave] = true data['$lineWidth'] = 3 data['$color'] = "#434343" data['$dim'] = 15 end |
#master? ⇒ Boolean
34 35 36 |
# File 'lib/mongo3/node.rb', line 34 def master? data.has_key?( :master ) end |
#slave? ⇒ Boolean
38 39 40 |
# File 'lib/mongo3/node.rb', line 38 def slave? data.has_key?( :slave ) end |
#to_adjacencies ⇒ Object
convert a tree node to a set of adjacencies
62 63 64 65 66 |
# File 'lib/mongo3/node.rb', line 62 def to_adjacencies cltn = [] _to_adjacencies( self, cltn ) cltn end |
#to_json(*a) ⇒ Object
converts to json
80 81 82 83 84 85 86 87 |
# File 'lib/mongo3/node.rb', line 80 def to_json(*a) hash = OrderedHash.new hash[:id] = oid hash[:name] = self.name hash[:children] = self.children hash[:data] = self.data hash.to_json(*a) end |