Module: MongoTree::Strategies::MaterializedPath::InstanceMethods

Defined in:
lib/mongo_tree/strategies/materialized_path.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



68
69
70
71
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 68

def ancestors
  return [] if self.path.nil?
  path_array[0..-2].map{ |n| find_by_path_attribute(n) }
end

#childrenObject



56
57
58
59
60
61
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 56

def children
  docs = self.class.all(:path => /^#{ self.path },/, :depth => self.depth + 1)
  MongoTree::Children.new(docs, self) do |child, parent|
    child.parent = parent
  end
end

#descendantsObject



73
74
75
76
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 73

def descendants
  return [] if self.path.nil?
  self.class.all(:path => /^#{ self.path },/)
end

#parentObject



47
48
49
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 47

def parent
  @parent ||= find_parent_from_path
end

#parent=(node) ⇒ Object



51
52
53
54
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 51

def parent=(node)
  @parent = node
  update_path_and_depth
end

#pathObject



35
36
37
38
39
40
41
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 35

def path
  if self[:path].nil?
    self.send(path_attribute).to_s
  else
    self[:path]
  end
end

#rootObject



43
44
45
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 43

def root
  find_by_path_attribute(path_array.first)
end

#siblingsObject



63
64
65
66
# File 'lib/mongo_tree/strategies/materialized_path.rb', line 63

def siblings
  return [] if @parent.nil?
  self.class.all(:path => /^#{ @parent.path },/, :depth => self.depth, path_attribute.ne => self.send(path_attribute).to_s)
end