Module: Mongoid::Acts::Tree::InstanceMethods
- Defined in:
- lib/mongoid_tree.rb
Instance Method Summary collapse
-
#<=>(another_node) ⇒ Object
Comparable.
-
#breadth_first ⇒ Object
(also: #bfs)
Returns the whole subtree including itself as array.
- #depth ⇒ Object
-
#depth_first ⇒ Object
(also: #dfs)
Returns the whole subtree including itself as array.
- #insert_after(new_child) ⇒ Object
- #insert_before(new_child) ⇒ Object
- #move_to(target_node) ⇒ Object
- #parent ⇒ Object
- #rebuild_paths ⇒ Object
- #update_path ⇒ Object
Instance Method Details
#<=>(another_node) ⇒ Object
Comparable
75 76 77 |
# File 'lib/mongoid_tree.rb', line 75 def <=> (another_node) self.position <=> another_node.position end |
#breadth_first ⇒ Object Also known as: bfs
Returns the whole subtree including itself as array
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongoid_tree.rb', line 94 def breadth_first result = [] queue = [self] while !queue.empty? node = queue.shift result << node node.children.sort.each do |child| queue << child end end return result end |
#depth ⇒ Object
70 71 72 |
# File 'lib/mongoid_tree.rb', line 70 def depth self.parents.count end |
#depth_first ⇒ Object Also known as: dfs
Returns the whole subtree including itself as array
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mongoid_tree.rb', line 80 def depth_first result = [self] if self.child_ids.empty? return result else self.children.sort.each do |child| result += child.depth_first end end return result end |
#insert_after(new_child) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid_tree.rb', line 118 def insert_after ( new_child ) new_child.position = self.position + 1 self.parent.children.each do |child| if child.position >= new_child.position child.update_attributes(:position => child.position + 1) end end self.parent.children << new_child end |
#insert_before(new_child) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/mongoid_tree.rb', line 108 def insert_before( new_child ) new_child.position = self.position self.parent.children.each do |child| if child.position >= new_child.position child.update_attributes(:position => child.position + 1) end end self.parent.reload.children << new_child end |
#move_to(target_node) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongoid_tree.rb', line 128 def move_to(target_node) # unhinge - I was getting a nil on another implementation, so this is a bit longer but works child_ids_array = self.parent.child_ids.clone child_ids_array.delete(self.id) parent.update_attributes(:child_ids => child_ids_array ) self.update_attributes(:parent_ids => []) # and append target_node.children << self # recurse through subtree self.rebuild_paths end |
#parent ⇒ Object
66 67 68 |
# File 'lib/mongoid_tree.rb', line 66 def parent self.parents.last end |
#rebuild_paths ⇒ Object
140 141 142 143 144 145 |
# File 'lib/mongoid_tree.rb', line 140 def rebuild_paths self.update_path self.children.each do |child| child.rebuild_paths end end |
#update_path ⇒ Object
147 148 149 |
# File 'lib/mongoid_tree.rb', line 147 def update_path self.update_attributes(:parent_ids => self.parent.parent_ids + [self.parent.id]) end |