Class: Mongo3::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo3/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 || BSON::OrderedHash.new
  @parent   = nil
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/mongo3/node.rb', line 6

def children
  @children
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/mongo3/node.rb', line 6

def data
  @data
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/mongo3/node.rb', line 6

def name
  @name
end

#oidObject

Returns the value of attribute oid.



6
7
8
# File 'lib/mongo3/node.rb', line 6

def oid
  @oid
end

#parentObject

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

.make_node(name) ⇒ Object



16
17
18
# File 'lib/mongo3/node.rb', line 16

def self.make_node( name )
  Node.new( name, name, :path_ids => name, :path_names => name )
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

Returns:

  • (Boolean)


34
35
36
# File 'lib/mongo3/node.rb', line 34

def master?
  data.has_key?( :master )
end

#slave?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mongo3/node.rb', line 38

def slave?
  data.has_key?( :slave )
end

#to_adjacenciesObject

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 = BSON::OrderedHash.new
  hash[:id]       = oid
  hash[:name]     = self.name
  hash[:children] = self.children
  hash[:data]     = self.data 
  hash.to_json(*a)
end