Module: MongoTree::Base::InstanceMethods
- Defined in:
- lib/mongo_tree/base.rb
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #children=(nodes) ⇒ Object
- #depth ⇒ Object
- #descendants ⇒ Object
- #root ⇒ Object
Instance Method Details
#ancestors ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongo_tree/base.rb', line 37 def ancestors collected = [] current = self.parent until current.nil? collected << current current = current.parent end collected end |
#children ⇒ Object
29 30 31 |
# File 'lib/mongo_tree/base.rb', line 29 def children MongoTree::Children.new(self.class.all(:parent_id => self.id), self) end |
#children=(nodes) ⇒ Object
33 34 35 |
# File 'lib/mongo_tree/base.rb', line 33 def children=(nodes) nodes.each{ |node| children << node } end |
#depth ⇒ Object
25 26 27 |
# File 'lib/mongo_tree/base.rb', line 25 def depth ancestors.length end |
#descendants ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongo_tree/base.rb', line 49 def descendants collected = [] nodes = self.children until nodes.empty? current = nodes.shift collected << current nodes += current.children end collected end |
#root ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/mongo_tree/base.rb', line 15 def root current = self until current.parent.nil? current = current.parent end current end |