Module: Mongestry::ClassMethods

Defined in:
lib/mongestry.rb

Instance Method Summary collapse

Instance Method Details

#after_depth(depth) ⇒ Object

Return nodes that are deeper than depth (node.depth > depth)



208
209
210
# File 'lib/mongestry.rb', line 208

def after_depth depth
  self.where(persisted_depth: {"$gt" => depth})
end

#ancestors_of(node) ⇒ Object

Ancestors of node, node can be either a record or an id



163
164
165
# File 'lib/mongestry.rb', line 163

def ancestors_of node
  node.ancestors
end

#at_depth(depth) ⇒ Object

Return nodes that are at depth (node.depth == depth)



198
199
200
# File 'lib/mongestry.rb', line 198

def at_depth depth
  self.where(persisted_depth: depth)
end

#before_depth(depth) ⇒ Object

Return nodes that are less deep than depth (node.depth < depth)



188
189
190
# File 'lib/mongestry.rb', line 188

def before_depth depth
  self.where(persisted_depth: {"$lt" => depth})
end

#children_of(node) ⇒ Object

Children of node, node can be either a record or an id



168
169
170
# File 'lib/mongestry.rb', line 168

def children_of node
  node.children
end

#descendants_of(node) ⇒ Object

Descendants of node, node can be either a record or an id



173
174
175
# File 'lib/mongestry.rb', line 173

def descendants_of node
  node.descendants
end

#from_depth(depth) ⇒ Object

Return nodes starting from a certain depth (node.depth >= depth)



203
204
205
# File 'lib/mongestry.rb', line 203

def from_depth depth
  self.where(persisted_depth: {"$gte" => depth})
end

#object_for(identifier) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mongestry.rb', line 145

def object_for identifier
  return nil if identifier == ""
  case identifier
  when BSON::ObjectId
    self.find identifier
  when String
    self.find(BSON::ObjectId.from_string(identifier))
  when self
    identifier
  end
end

#rootsObject

Root nodes



158
159
160
# File 'lib/mongestry.rb', line 158

def roots
  self.where(ancestry: nil)
end

#siblings_of(node) ⇒ Object

Siblings of node, node can be either a record or an id



183
184
185
# File 'lib/mongestry.rb', line 183

def siblings_of node
  node.siblings
end

#subtree_of(node) ⇒ Object

Subtree of node, node can be either a record or an id



178
179
180
# File 'lib/mongestry.rb', line 178

def subtree_of node
  node.subtree
end

#to_depth(depth) ⇒ Object

Return nodes up to a certain depth (node.depth <= depth)



193
194
195
# File 'lib/mongestry.rb', line 193

def to_depth depth
  self.where(persisted_depth: {"$lte" => depth})
end