Class: Dynamised::Node
- Inherits:
-
Object
- Object
- Dynamised::Node
- Includes:
- Enumerable
- Defined in:
- lib/dynamised/node.rb
Instance Attribute Summary collapse
-
#childs ⇒ Object
Returns the value of attribute childs.
-
#data ⇒ Object
Returns the value of attribute data.
-
#ident ⇒ Object
Returns the value of attribute ident.
-
#init ⇒ Object
Returns the value of attribute init.
-
#siblings ⇒ Object
Returns the value of attribute siblings.
Instance Method Summary collapse
- #<=>(other_node) ⇒ Object
- #[](*keys) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(init = {}, ident = nil) ⇒ Node
constructor
A new instance of Node.
- #new_child(ident, &block) ⇒ Object
- #pretty_print(pp) ⇒ Object
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
#childs ⇒ Object
Returns the value of attribute childs.
5 6 7 |
# File 'lib/dynamised/node.rb', line 5 def childs @childs end |
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/dynamised/node.rb', line 5 def data @data end |
#ident ⇒ Object
Returns the value of attribute ident.
5 6 7 |
# File 'lib/dynamised/node.rb', line 5 def ident @ident end |
#init ⇒ Object
Returns the value of attribute init.
5 6 7 |
# File 'lib/dynamised/node.rb', line 5 def init @init end |
#siblings ⇒ Object
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 |