Class: Dynamised::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dynamised/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}, ident = nil) ⇒ Node

Returns a new instance of Node.



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

def initialize(init={},ident=nil)
  @ident  = ident
  @childs = {}
  @sibilngs =  {}
  @init   = init.clone
  @data   = init.clone
end

Instance Attribute Details

#childsObject

Returns the value of attribute childs.



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

def childs
  @childs
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#identObject

Returns the value of attribute ident.



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

def ident
  @ident
end

#initObject

Returns the value of attribute init.



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

def init
  @init
end

#siblingsObject

Returns the value of attribute siblings.



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

def siblings
  @siblings
end

Instance Method Details

#<=>(other_node) ⇒ Object



23
24
25
# File 'lib/dynamised/node.rb', line 23

def <=>(other_node)
  @data <=> other_node.data
end

#[](*keys) ⇒ Object



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

def [](*keys)
  return self if @childs.empty?
  [*keys.flatten].inject(self) do |node,ident|
      node.find {|n| n.ident == ident}
  end
end

#each(&block) ⇒ Object



16
17
18
19
20
21
# File 'lib/dynamised/node.rb', line 16

def each(&block)
  block.call(self)
  @childs.map do |key,child|
    child.each(&block)
  end
end

#new_child(ident, &block) ⇒ Object



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

def new_child(ident,&block)
  child = self.class.new(@init,ident)
  child.siblings = self.childs
  child.tap(&block) if block_given?
  @childs[ident] = child
end

#pretty_print(pp) ⇒ Object



41
42
43
# File 'lib/dynamised/node.rb', line 41

def pretty_print(pp)
  self.each {|node| pp.text(node.ident || "" );puts "\n";pp.pp_hash node.data}
end