Module: MongoNestedSet::InstanceMethods
- Defined in:
- lib/mongo_nested_set.rb
Instance Method Summary collapse
-
#<=>(x) ⇒ Object
order by left column.
-
#==(comparison_object) ⇒ Object
Redefine to act like active record.
-
#_parent_id ⇒ Object
Value of the parent column.
-
#ancestors ⇒ Object
Returns an array of all parents.
- #base_class ⇒ Object
-
#child? ⇒ Boolean
Returns true is this is a child node.
-
#descendants ⇒ Object
Returns a set of all of its children and nested children.
- #is_ancestor_of?(other) ⇒ Boolean
- #is_descendant_of?(other) ⇒ Boolean
- #is_or_is_ancestor_of?(other) ⇒ Boolean
- #is_or_is_descendant_of?(other) ⇒ Boolean
- #leaf? ⇒ Boolean
-
#left ⇒ Object
Value of the left column.
-
#left_sibling ⇒ Object
Find the first sibling to the left.
-
#level ⇒ Object
Returns the level of this object in the tree root level is 0.
-
#move_left ⇒ Object
Shorthand method for finding the left sibling and moving to the left of it.
- #move_possible?(target) ⇒ Boolean
-
#move_right ⇒ Object
Shorthand method for finding the right sibling and moving to the right of it.
-
#move_to_child_of(node) ⇒ Object
Move the node to the child of another node (you can pass id only).
-
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only).
-
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only).
-
#move_to_root ⇒ Object
Move the node to root nodes.
-
#right ⇒ Object
Value of the right column.
-
#right_sibling ⇒ Object
Find the first sibling to the right.
-
#root ⇒ Object
Returns root.
-
#root? ⇒ Boolean
Returns true if this is a root node.
-
#same_scope?(other) ⇒ Boolean
Check if other model is in the same scope.
- #scope_hash ⇒ Object
- #scoped(conditions = {}) ⇒ Object
-
#self_and_ancestors ⇒ Object
Returns the array of all parents and self.
-
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children.
-
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self.
-
#siblings ⇒ Object
Returns the array of all children of the parent, except self.
- #to_text ⇒ Object
Instance Method Details
#<=>(x) ⇒ Object
order by left column
259 260 261 |
# File 'lib/mongo_nested_set.rb', line 259 def <=>(x) left <=> x.left end |
#==(comparison_object) ⇒ Object
Redefine to act like active record
264 265 266 267 268 269 |
# File 'lib/mongo_nested_set.rb', line 264 def ==(comparison_object) comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id && !comparison_object.new?) end |
#_parent_id ⇒ Object
Value of the parent column
230 231 232 |
# File 'lib/mongo_nested_set.rb', line 230 def _parent_id send parent_column_name end |
#ancestors ⇒ Object
Returns an array of all parents
290 291 292 |
# File 'lib/mongo_nested_set.rb', line 290 def ancestors without_self self_and_ancestors end |
#base_class ⇒ Object
225 226 227 |
# File 'lib/mongo_nested_set.rb', line 225 def base_class self.class.base_class end |
#child? ⇒ Boolean
Returns true is this is a child node
254 255 256 |
# File 'lib/mongo_nested_set.rb', line 254 def child? !_parent_id.nil? end |
#descendants ⇒ Object
Returns a set of all of its children and nested children
321 322 323 |
# File 'lib/mongo_nested_set.rb', line 321 def descendants without_self self_and_descendants end |
#is_ancestor_of?(other) ⇒ Boolean
333 334 335 |
# File 'lib/mongo_nested_set.rb', line 333 def is_ancestor_of?(other) self.left < other.left && other.left < self.right && same_scope?(other) end |
#is_descendant_of?(other) ⇒ Boolean
325 326 327 |
# File 'lib/mongo_nested_set.rb', line 325 def is_descendant_of?(other) other.left < self.left && self.left < other.right && same_scope?(other) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
337 338 339 |
# File 'lib/mongo_nested_set.rb', line 337 def is_or_is_ancestor_of?(other) self.left <= other.left && other.left < self.right && same_scope?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
329 330 331 |
# File 'lib/mongo_nested_set.rb', line 329 def is_or_is_descendant_of?(other) other.left <= self.left && self.left < other.right && same_scope?(other) end |
#leaf? ⇒ Boolean
249 250 251 |
# File 'lib/mongo_nested_set.rb', line 249 def leaf? !new? && right - left == 1 end |
#left ⇒ Object
Value of the left column
235 236 237 |
# File 'lib/mongo_nested_set.rb', line 235 def left send left_column_name end |
#left_sibling ⇒ Object
Find the first sibling to the left
349 350 351 |
# File 'lib/mongo_nested_set.rb', line 349 def left_sibling base_class.find :first, scoped(parent_column_name => _parent_id, left_column_name => { '$lt' => left }, :order => "#{left_column_name} DESC") end |
#level ⇒ Object
Returns the level of this object in the tree root level is 0
311 312 313 |
# File 'lib/mongo_nested_set.rb', line 311 def level _parent_id.nil? ? 0 : ancestors.count end |
#move_left ⇒ Object
Shorthand method for finding the left sibling and moving to the left of it.
359 360 361 |
# File 'lib/mongo_nested_set.rb', line 359 def move_left move_to_left_of left_sibling end |
#move_possible?(target) ⇒ Boolean
388 389 390 391 392 393 394 |
# File 'lib/mongo_nested_set.rb', line 388 def move_possible?(target) self != target && # Can't target self same_scope?(target) && # can't be in different scopes # !(left..right).include?(target.left..target.right) # this needs tested more # detect impossible move !((left <= target.left && right >= target.left) or (left <= target.right && right >= target.right)) end |
#move_right ⇒ Object
Shorthand method for finding the right sibling and moving to the right of it.
364 365 366 |
# File 'lib/mongo_nested_set.rb', line 364 def move_right move_to_right_of right_sibling end |
#move_to_child_of(node) ⇒ Object
Move the node to the child of another node (you can pass id only)
379 380 381 |
# File 'lib/mongo_nested_set.rb', line 379 def move_to_child_of(node) move_to node, :child end |
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only)
369 370 371 |
# File 'lib/mongo_nested_set.rb', line 369 def move_to_left_of(node) move_to node, :left end |
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only)
374 375 376 |
# File 'lib/mongo_nested_set.rb', line 374 def move_to_right_of(node) move_to node, :right end |
#move_to_root ⇒ Object
Move the node to root nodes
384 385 386 |
# File 'lib/mongo_nested_set.rb', line 384 def move_to_root move_to nil, :root end |
#right ⇒ Object
Value of the right column
240 241 242 |
# File 'lib/mongo_nested_set.rb', line 240 def right send right_column_name end |
#right_sibling ⇒ Object
Find the first sibling to the right
354 355 356 |
# File 'lib/mongo_nested_set.rb', line 354 def right_sibling base_class.find :first, scoped(parent_column_name => _parent_id, left_column_name => { '$gt' => right }, :order => "#{left_column_name}") end |
#root ⇒ Object
Returns root
280 281 282 |
# File 'lib/mongo_nested_set.rb', line 280 def root base_class.find :first, scoped(left_column_name => { '$lte' => left }, right_column_name => { '$gte' => right }) end |
#root? ⇒ Boolean
Returns true if this is a root node.
245 246 247 |
# File 'lib/mongo_nested_set.rb', line 245 def root? _parent_id.nil? end |
#same_scope?(other) ⇒ Boolean
Check if other model is in the same scope
342 343 344 345 346 |
# File 'lib/mongo_nested_set.rb', line 342 def same_scope?(other) Array([:scope]).all? do |attr| self.send(attr) == other.send(attr) end end |
#scope_hash ⇒ Object
271 272 273 |
# File 'lib/mongo_nested_set.rb', line 271 def scope_hash Hash[*Array([:scope]).collect { |s| [s, send(s)] }.flatten] end |
#scoped(conditions = {}) ⇒ Object
275 276 277 |
# File 'lib/mongo_nested_set.rb', line 275 def scoped(conditions = {}) conditions.reverse_merge(scope_hash) end |
#self_and_ancestors ⇒ Object
Returns the array of all parents and self
285 286 287 |
# File 'lib/mongo_nested_set.rb', line 285 def self_and_ancestors base_class.find :all, scoped(left_column_name => { '$lte' => left }, right_column_name => { '$gte' => right }) end |
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children
316 317 318 |
# File 'lib/mongo_nested_set.rb', line 316 def self_and_descendants base_class.find :all, scoped(left_column_name => { '$gte' => left }, right_column_name => { '$lte' => right }) end |
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self
295 296 297 |
# File 'lib/mongo_nested_set.rb', line 295 def self_and_siblings base_class.find :all, scoped(parent_column_name => _parent_id) end |
#siblings ⇒ Object
Returns the array of all children of the parent, except self
300 301 302 |
# File 'lib/mongo_nested_set.rb', line 300 def siblings without_self self_and_siblings end |
#to_text ⇒ Object
396 397 398 399 400 |
# File 'lib/mongo_nested_set.rb', line 396 def to_text self_and_descendants.map do |node| "#{'*'*(node.level+1)} #{node.id} #{node.to_s} (#{node._parent_id}, #{node.left}, #{node.right})" end.join("\n") end |