Class: BasicTree
- Inherits:
-
Object
- Object
- BasicTree
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/basic_tree.rb,
lib/basic_tree/version.rb
Defined Under Namespace
Classes: Kids
Constant Summary collapse
- VERSION =
"1.0.3"
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
- #<=>(other) ⇒ Object
-
#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: test.
- #root ⇒ Object
- #root? ⇒ Boolean
- #siblings ⇒ Object
- #siblings_and_self ⇒ Object
- #subtree ⇒ Object
Constructor Details
#initialize(object, parent = nil, &block) ⇒ BasicTree
TODO: test
17 18 19 20 21 22 23 |
# File 'lib/basic_tree.rb', line 17 def initialize(object, parent = nil, &block) self.object = object parent.try(:insert!, self) if block_given? block.arity == 1 ? yield(self) : instance_eval(&block) end end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
96 97 98 |
# File 'lib/basic_tree.rb', line 96 def object @object end |
#parent ⇒ Object
Returns the value of attribute parent.
95 96 97 |
# File 'lib/basic_tree.rb', line 95 def parent @parent end |
Instance Method Details
#<=>(other) ⇒ Object
130 131 132 |
# File 'lib/basic_tree.rb', line 130 def <=>(other) subtree.map(&:object) <=> other.subtree.map(&:object) end |
#add(object, &block) ⇒ Object
TODO: test
26 27 28 29 30 31 32 |
# File 'lib/basic_tree.rb', line 26 def add(object, &block) if object.is_a?(self.class) insert!(object) else self.class.new(object, self, &block) end end |
#ancestors ⇒ Object
71 72 73 |
# File 'lib/basic_tree.rb', line 71 def ancestors root? ? [] : (parent.ancestors << parent) end |
#children ⇒ Object
63 64 65 |
# File 'lib/basic_tree.rb', line 63 def children kids.dup end |
#descendants ⇒ Object
75 76 77 78 79 |
# File 'lib/basic_tree.rb', line 75 def descendants d = [] kids.each { |k| d += k.descendants.unshift(k) } d end |
#first? ⇒ Boolean
120 121 122 |
# File 'lib/basic_tree.rb', line 120 def first? root? || siblings_and_self[0] == self end |
#insert!(basic_tree) ⇒ Object
TODO: test
35 36 37 38 39 |
# File 'lib/basic_tree.rb', line 35 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
124 125 126 |
# File 'lib/basic_tree.rb', line 124 def last? root? || siblings_and_self.last == self end |
#leaf? ⇒ Boolean
116 117 118 |
# File 'lib/basic_tree.rb', line 116 def leaf? kids.empty? end |
#level ⇒ Object
102 103 104 |
# File 'lib/basic_tree.rb', line 102 def level path.size end |
#move_down! ⇒ Object
TODO: test
56 57 58 59 |
# File 'lib/basic_tree.rb', line 56 def move_down! raise "Already last" if last? parent.send(:kids).swap!(position, position + 1) end |
#move_up! ⇒ Object
TODO: test
50 51 52 53 |
# File 'lib/basic_tree.rb', line 50 def move_up! raise "Already first" if first? parent.send(:kids).swap!(position, position - 1) end |
#path ⇒ Object
67 68 69 |
# File 'lib/basic_tree.rb', line 67 def path ancestors << self end |
#position ⇒ Object
106 107 108 |
# File 'lib/basic_tree.rb', line 106 def position siblings_and_self.index(self) end |
#remove!(basic_tree) ⇒ Object
TODO: test
42 43 44 45 46 47 |
# File 'lib/basic_tree.rb', line 42 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
98 99 100 |
# File 'lib/basic_tree.rb', line 98 def root path.first end |
#root? ⇒ Boolean
112 113 114 |
# File 'lib/basic_tree.rb', line 112 def root? !parent end |
#siblings ⇒ Object
89 90 91 |
# File 'lib/basic_tree.rb', line 89 def siblings root? ? [] : siblings_and_self.delete_if { |s| s.equal?(self) } end |
#siblings_and_self ⇒ Object
85 86 87 |
# File 'lib/basic_tree.rb', line 85 def siblings_and_self root? ? [self] : parent.children end |
#subtree ⇒ Object
81 82 83 |
# File 'lib/basic_tree.rb', line 81 def subtree descendants.unshift(self) end |