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 = {}) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
# File 'lib/mongo3/node.rb', line 7

def initialize( oid, name, data={} )
  @oid      = oid
  @name     = name
  @children = []
  @data     = data || {}
  @parent   = nil
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#oidObject

Returns the value of attribute oid.



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

def oid
  @oid
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.dump(node, level = 0) ⇒ Object

Dump nodes to stdout



71
72
73
74
# File 'lib/mongo3/node.rb', line 71

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



77
78
79
80
81
# File 'lib/mongo3/node.rb', line 77

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



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

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



20
21
22
23
24
# File 'lib/mongo3/node.rb', line 20

def <<( new_one )
  new_one.parent = self
  @children << new_one
  update_paths( new_one )
end

#path(accessor = :oid) ⇒ Object



45
46
47
48
49
# File 'lib/mongo3/node.rb', line 45

def path( accessor=:oid )
  path = []
  traverse( path, self, accessor )
  path.reverse.join( "|" )
end

#to_adjacenciesObject

convert a tree node to a set of adjacencies



35
36
37
38
39
40
41
42
43
# File 'lib/mongo3/node.rb', line 35

def to_adjacencies
  root_level = { :id => self.oid, :name => self.name, :data => self.data, :adjacencies => [] } 
  cltn = [ root_level ]
  self.children.each do |child|
    root_level[:adjacencies] << child.oid
    cltn << { :id => child.oid, :name => child.name, :data => child.data, :adjacencies => [] } 
  end
  cltn
end

#to_json(*a) ⇒ Object

converts to json



59
60
61
62
63
64
65
66
# File 'lib/mongo3/node.rb', line 59

def to_json(*a)
  {
    'id'         => oid,
    'name'       => self.name,
    'children'   => self.children,
    'data'       => self.data
  }.to_json(*a)
end

#traverse(path, node, accessor) ⇒ Object



51
52
53
54
55
56
# File 'lib/mongo3/node.rb', line 51

def traverse( path, node, accessor )
  path << node.send( accessor ).gsub( /\(\d+\)/, "" )
  if node.parent
    traverse( path, node.parent, accessor )
  end
end

#update_paths(node) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/mongo3/node.rb', line 26

def update_paths( node )
  node.data[:path_names] = node.path( :name )
  node.data[:path_ids]   = node.path( :oid )
  node.children.each do |child|
    child.update_paths( child )
  end
end