Class: BasicTree
- Inherits:
-
Object
- Object
- BasicTree
- Includes:
- Enumerable
- Defined in:
- lib/basic_tree.rb,
lib/basic_tree/version.rb
Defined Under Namespace
Classes: Kids
Constant Summary collapse
- VERSION =
"1.0.1"
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#add(object, &block) ⇒ Object
TODO: test.
- #ancestors ⇒ Object
- #children ⇒ Object
- #descendants ⇒ Object
- #first? ⇒ Boolean
-
#initialize(object, parent = nil, &block) ⇒ BasicTree
constructor
TODO: test.
-
#insert!(basic_tree) ⇒ Object
TODO: test.
- #last? ⇒ Boolean
- #leaf? ⇒ Boolean
- #level ⇒ Object
-
#move_down! ⇒ Object
TODO: test.
-
#move_up! ⇒ Object
TODO: test.
- #path ⇒ Object
- #position ⇒ Object
-
#remove!(basic_tree) ⇒ Object
TODO: self.
- #root ⇒ Object
- #root? ⇒ Boolean
- #siblings ⇒ Object
- #siblings_and_self ⇒ Object
- #subtree ⇒ Object
Constructor Details
#initialize(object, parent = nil, &block) ⇒ BasicTree
TODO: test
16 17 18 19 20 |
# File 'lib/basic_tree.rb', line 16 def initialize(object, parent = nil, &block) self.object = object parent.try(:insert!, self) instance_eval(&block) if block_given? end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
93 94 95 |
# File 'lib/basic_tree.rb', line 93 def object @object end |
#parent ⇒ Object
Returns the value of attribute parent.
92 93 94 |
# File 'lib/basic_tree.rb', line 92 def parent @parent end |
Instance Method Details
#add(object, &block) ⇒ Object
TODO: test
23 24 25 26 27 28 29 |
# File 'lib/basic_tree.rb', line 23 def add(object, &block) if object.is_a?(self.class) insert!(object) else self.class.new(object, self, &block) end end |
#ancestors ⇒ Object
68 69 70 |
# File 'lib/basic_tree.rb', line 68 def ancestors root? ? [] : (parent.ancestors << parent) end |
#children ⇒ Object
60 61 62 |
# File 'lib/basic_tree.rb', line 60 def children kids.dup end |
#descendants ⇒ Object
72 73 74 75 76 |
# File 'lib/basic_tree.rb', line 72 def descendants d = [] kids.each { |k| d += k.descendants.unshift(k) } d end |
#first? ⇒ Boolean
117 118 119 |
# File 'lib/basic_tree.rb', line 117 def first? root? || siblings_and_self[0] == self end |
#insert!(basic_tree) ⇒ Object
TODO: test
32 33 34 35 36 |
# File 'lib/basic_tree.rb', line 32 def insert!(basic_tree) raise ArgumentError, "Must be a #{self.class}" unless basic_tree.is_a?(self.class) basic_tree.send(:parent=, self) kids << basic_tree end |
#last? ⇒ Boolean
121 122 123 |
# File 'lib/basic_tree.rb', line 121 def last? root? || siblings_and_self.last == self end |
#leaf? ⇒ Boolean
113 114 115 |
# File 'lib/basic_tree.rb', line 113 def leaf? kids.empty? end |
#level ⇒ Object
99 100 101 |
# File 'lib/basic_tree.rb', line 99 def level path.size end |
#move_down! ⇒ Object
TODO: test
53 54 55 56 |
# File 'lib/basic_tree.rb', line 53 def move_down! raise "Already last" if last? parent.send(:kids).swap!(position, position + 1) end |
#move_up! ⇒ Object
TODO: test
47 48 49 50 |
# File 'lib/basic_tree.rb', line 47 def move_up! raise "Already first" if first? parent.send(:kids).swap!(position, position - 1) end |
#path ⇒ Object
64 65 66 |
# File 'lib/basic_tree.rb', line 64 def path ancestors << self end |
#position ⇒ Object
103 104 105 |
# File 'lib/basic_tree.rb', line 103 def position siblings_and_self.index(self) end |
#remove!(basic_tree) ⇒ Object
TODO: self
39 40 41 42 43 44 |
# File 'lib/basic_tree.rb', line 39 def remove!(basic_tree) raise ArgumentError, "Must be a #{self.class}" unless basic_tree.is_a?(self.class) raise StandardError, "Can't remove root" if root? parent.send(:kids).delete(self) basic_tree.send(:parent=, nil) end |
#root ⇒ Object
95 96 97 |
# File 'lib/basic_tree.rb', line 95 def root path.first end |
#root? ⇒ Boolean
109 110 111 |
# File 'lib/basic_tree.rb', line 109 def root? !parent end |
#siblings ⇒ Object
86 87 88 |
# File 'lib/basic_tree.rb', line 86 def siblings root? ? [] : siblings_and_self.delete_if { |s| s == self } end |
#siblings_and_self ⇒ Object
82 83 84 |
# File 'lib/basic_tree.rb', line 82 def siblings_and_self root? ? [self] : parent.children end |
#subtree ⇒ Object
78 79 80 |
# File 'lib/basic_tree.rb', line 78 def subtree descendants.unshift(self) end |