Module: MongoTree::Base::InstanceMethods

Defined in:
lib/mongo_tree/base.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



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

#childrenObject



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

#depthObject



25
26
27
# File 'lib/mongo_tree/base.rb', line 25

def depth
  ancestors.length
end

#descendantsObject



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

#rootObject



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