Class: NdcTree::Node

Inherits:
Tree::TreeNode
  • Object
show all
Defined in:
lib/ndc_tree/node.rb

Defined Under Namespace

Classes: InputNdcError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "root", content = {}) ⇒ Node

Returns

a instance object of NdcTree::Node



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

def initialize(name="root",content={})
  @weight=1
  super(name, content)
end

Instance Attribute Details

#weightObject

Returns the value of attribute weight.



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

def weight
  @weight
end

Instance Method Details

#<<(value) ⇒ Object

This method inserts a node has name same as given value as a child node. If given value is instance of Array, this method repeats inserting each element of value.

Returns

self instance



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

def <<(value)
  case value.class.name.to_sym
    when :String
	add_ndc(value)
    when :Array
	value.flatten.each{|v| add_ndc(v) }
    when :Node
	super
    else
	raise TypeError,"#{value} is not valid"
  end 
  self
end

#[](key) ⇒ Object

Returns

a node has name same as a given key



24
25
26
# File 'lib/ndc_tree/node.rb', line 24

def [](key)
  nodes.find {|node| node.name==key }
end

#marshal_load(dumped_tree_array) ⇒ Object

This method loads dump of a tree formatted marshal object.

Return

self instance



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ndc_tree/node.rb', line 83

def marshal_load(dumped_tree_array)
  nodes = { }
  dumped_tree_array.each do |node_hash|
    name        = node_hash[:name]
    parent_name = node_hash[:parent]
    content     = Marshal.load(node_hash[:content])

    if parent_name then
	nodes[name] = current_node = self.class.new(name, content)
	nodes[parent_name].add current_node
    else
	# This is the root node, hence initialize self.
	initialize(name, content)

	nodes[name] = self    # Add self to the list of nodes
    end
  end
end

This method output a image file with GraphViz.

Returns

true

Raise

when given file path is wrong

Yields:

  • (g)


106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ndc_tree/node.rb', line 106

def print_image(opts={:gif=>"ndc_tree.gif"},&block)
  sort!

  g = GraphViz::new("G")
  set_default_style
  to_graphviz(g,opts)

  yield g if block_given?

  result = g.output(opts)
  result.nil? ? true : result
end

#search(exp) ⇒ Object

this method searches nodes have name matching a given expression

Returns

a list of NdcTree::Node objects.



33
34
35
# File 'lib/ndc_tree/node.rb', line 33

def search(exp)
  nodes.find_all {|node| node.name =~ exp }
end

#sortObject

sorts nodes at each layers by ndc codes ( it is not bang method )

Returns

a instance object of NdcTree::Node



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

def sort
  node = self.dup
  node.sort!
end

#sort!Object

this method sorts nodes at each layers by ndc codes ( it’s bang method )

Returns

a instance object of NdcTree::Node



41
42
43
44
45
46
# File 'lib/ndc_tree/node.rb', line 41

def sort!
  unless self.is_leaf? then
    @children.sort!.map{|node| node.sort!}
  end
  self
end