Module: OrderedTree::InstanceMethods::Tree
- Included in:
- OrderedTree::InstanceMethods
- Defined in:
- lib/ordered_tree/instance_methods/tree.rb
Instance Method Summary collapse
-
#ancestors(reload = false) ⇒ Object
returns an array of ancestors, starting from parent until root.
-
#children(reload = false) ⇒ Object
returns an array of the object’s immediate children auto-loads itself on first access instead of returning “<child_nodes not loaded yet>”.
-
#descendants(reload = false) ⇒ Object
returns an array of the object’s descendants.
-
#orphan ⇒ Object
orphans the node (sends it to the roots list) (descendants follow).
-
#orphan_children ⇒ Object
orphans the node’s children sends all immediate children to the ‘roots’ list.
-
#orphan_self_and_children ⇒ Object
sends self and immediate children to the roots list.
-
#orphan_self_and_parent_adopts_children ⇒ Object
hands children off to parent (if possible), then orphans itself.
-
#parent(reload = false) ⇒ Object
returns object’s parent in the tree auto-loads itself on first access instead of returning “<parent_node not loaded yet>”.
-
#parent_adopts_children ⇒ Object
hands children off to parent if no parent, children will be orphaned.
-
#root(reload = false) ⇒ Object
returns the top node in the object’s tree.
-
#shift_to(new_parent = nil, new_sibling = nil) ⇒ Object
shifts a node to another parent, optionally specifying it’s position (descendants will follow along).
Instance Method Details
#ancestors(reload = false) ⇒ Object
returns an array of ancestors, starting from parent until root.
return is cached
use ancestors(true) to force a reload
18 19 20 21 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 18 def ancestors(reload = false) reload = true if !@ancestors reload ? find_ancestors : @ancestors end |
#children(reload = false) ⇒ Object
returns an array of the object’s immediate children
auto-loads itself on first access
instead of returning "<child_nodes not loaded yet>"
return is cached
use children(true) to force a reload
40 41 42 43 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 40 def children(reload=false) reload = true if !@children reload ? child_nodes(true) : @children end |
#descendants(reload = false) ⇒ Object
returns an array of the object’s descendants
return is cached
use descendants(true) to force a reload
49 50 51 52 53 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 49 def descendants(reload = false) @descendants = nil if reload reload = true if !@descendants reload ? find_descendants(self) : @descendants end |
#orphan ⇒ Object
orphans the node (sends it to the roots list)
(descendants follow)
89 90 91 92 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 89 def orphan self[foreign_key_column] = 0 self.save end |
#orphan_children ⇒ Object
orphans the node’s children
sends all immediate children to the 'roots' list
96 97 98 99 100 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 96 def orphan_children self.class.transaction do children(true).each{|child| child.orphan} end end |
#orphan_self_and_children ⇒ Object
sends self and immediate children to the roots list
115 116 117 118 119 120 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 115 def orphan_self_and_children self.class.transaction do orphan_children orphan end end |
#orphan_self_and_parent_adopts_children ⇒ Object
hands children off to parent (if possible), then orphans itself
123 124 125 126 127 128 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 123 def orphan_self_and_parent_adopts_children self.class.transaction do parent_adopts_children orphan end end |
#parent(reload = false) ⇒ Object
returns object’s parent in the tree
auto-loads itself on first access
instead of returning "<parent_node not loaded yet>"
return is cached, unless nil
use parent(true) to force a reload
29 30 31 32 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 29 def parent(reload=false) reload = true if !@parent reload ? parent_node(true) : @parent end |
#parent_adopts_children ⇒ Object
hands children off to parent
if no parent, children will be orphaned
104 105 106 107 108 109 110 111 112 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 104 def parent_adopts_children if parent(true) self.class.transaction do children(true).each{|child| parent.children << child} end else orphan_children end end |
#root(reload = false) ⇒ Object
returns the top node in the object’s tree
return is cached, unless nil
use root(true) to force a reload
10 11 12 13 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 10 def root(reload = false) reload = true if !@root reload ? find_root : @root end |
#shift_to(new_parent = nil, new_sibling = nil) ⇒ Object
shifts a node to another parent, optionally specifying it’s position
(descendants will follow along)
shift_to()
defaults to the bottom of the "roots" list
shift_to(nil, new_sibling)
will move the item to "roots",
and position the item above new_sibling
shift_to(new_parent)
will move the item to the new parent,
and position at the bottom of the parent's list
shift_to(new_parent, new_sibling)
will move the item to the new parent,
and position the item above new_sibling
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ordered_tree/instance_methods/tree.rb', line 75 def shift_to(new_parent = nil, new_sibling = nil) if new_parent ok = new_parent.children(true) << self else ok = orphan end if ok && new_sibling ok = move_above(new_sibling) if self_and_siblings(true).include?(new_sibling) end return ok end |