Module: SimpleNestedSet::InstanceMethods
- Defined in:
- lib/simple_nested_set/instance_methods.rb
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
compare by left column.
-
#ancestor_of?(other) ⇒ Boolean
Returns true if this is an ancestor of the given node.
-
#ancestors ⇒ Object
Returns an array of all parents.
- #attributes=(attributes) ⇒ Object
-
#child? ⇒ Boolean
Returns true if this is a child node.
-
#children? ⇒ Boolean
(also: #has_children?)
Returns true if the node has any children.
-
#descendants ⇒ Object
Returns a set of all of its children and nested children.
-
#descendants_count ⇒ Object
Returns the number of descendants.
-
#descendent_of?(other) ⇒ Boolean
Returns true if this is a descendent of the given node.
-
#leaf? ⇒ Boolean
Returns true if this is a leaf node.
-
#leaves ⇒ Object
Returns all descendants that are leaves.
-
#load_tree ⇒ Object
recursively populates the parent and children associations of self and all descendants using one query.
-
#move_left ⇒ Object
Moves the node to the left of its left sibling if any.
-
#move_right ⇒ Object
Moves the node to the right of its right sibling if any.
-
#move_to_child_of(node) ⇒ Object
Moves the node to the child of another node.
-
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node.
-
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node.
-
#move_to_root ⇒ Object
Makes this node a root node.
- #nested_set ⇒ Object
-
#next_sibling ⇒ Object
(also: #right_sibling)
Returns the righthand sibling.
-
#parent ⇒ Object
Returns the parent.
-
#previous_sibling ⇒ Object
(also: #left_sibling)
Returns the lefthand sibling.
-
#root ⇒ Object
Returns the root.
-
#root? ⇒ Boolean
Returns true if this is a root node.
-
#self_and_ancestors ⇒ Object
Returns the array of all parents and self.
-
#self_and_children ⇒ Object
Returns a set of only this entry’s immediate children including 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, included self.
-
#self_or_ancestor_of?(other) ⇒ Boolean
Returns true if this is equal to or an ancestor of the given node.
-
#self_or_descendent_of?(other) ⇒ Boolean
Returns true if this is equal to or a descendent of the given node.
-
#siblings ⇒ Object
Returns the array of all children of the parent, except self.
Instance Method Details
#<=>(other) ⇒ Object
compare by left column
37 38 39 |
# File 'lib/simple_nested_set/instance_methods.rb', line 37 def <=>(other) lft <=> other.lft end |
#ancestor_of?(other) ⇒ Boolean
Returns true if this is an ancestor of the given node
52 53 54 |
# File 'lib/simple_nested_set/instance_methods.rb', line 52 def ancestor_of?(other) lft < other.lft && rgt > other.rgt end |
#ancestors ⇒ Object
Returns an array of all parents
62 63 64 |
# File 'lib/simple_nested_set/instance_methods.rb', line 62 def ancestors nested_set.with_ancestors(lft, rgt) end |
#attributes=(attributes) ⇒ Object
9 10 11 12 |
# File 'lib/simple_nested_set/instance_methods.rb', line 9 def attributes=(attributes) @_nested_set_attributes = attributes.extract_nested_set_attributes! super end |
#child? ⇒ Boolean
Returns true if this is a child node
27 28 29 |
# File 'lib/simple_nested_set/instance_methods.rb', line 27 def child? !root? end |
#children? ⇒ Boolean Also known as: has_children?
Returns true if the node has any children
102 103 104 |
# File 'lib/simple_nested_set/instance_methods.rb', line 102 def children? descendants_count > 0 end |
#descendants ⇒ Object
Returns a set of all of its children and nested children.
82 83 84 |
# File 'lib/simple_nested_set/instance_methods.rb', line 82 def descendants rgt - lft == 1 ? [] : nested_set.with_descendants(lft, rgt) end |
#descendants_count ⇒ Object
Returns the number of descendants
92 93 94 |
# File 'lib/simple_nested_set/instance_methods.rb', line 92 def descendants_count rgt > lft ? (rgt - lft - 1) / 2 : 0 end |
#descendent_of?(other) ⇒ Boolean
Returns true if this is a descendent of the given node
72 73 74 |
# File 'lib/simple_nested_set/instance_methods.rb', line 72 def descendent_of?(other) lft > other.lft && rgt < other.rgt end |
#leaf? ⇒ Boolean
Returns true if this is a leaf node
32 33 34 |
# File 'lib/simple_nested_set/instance_methods.rb', line 32 def leaf? rgt.to_i - lft.to_i == 1 end |
#leaves ⇒ Object
Returns all descendants that are leaves
130 131 132 |
# File 'lib/simple_nested_set/instance_methods.rb', line 130 def leaves rgt - lft == 1 ? [] : nested_set.with_descendants(lft, rgt).with_leaves end |
#load_tree ⇒ Object
recursively populates the parent and children associations of self and all descendants using one query
16 17 18 19 |
# File 'lib/simple_nested_set/instance_methods.rb', line 16 def load_tree nested_set.populate_associations(descendants) self end |
#move_left ⇒ Object
Moves the node to the left of its left sibling if any
145 146 147 |
# File 'lib/simple_nested_set/instance_methods.rb', line 145 def move_left move_to_left_of(left_sibling) if left_sibling end |
#move_right ⇒ Object
Moves the node to the right of its right sibling if any
150 151 152 |
# File 'lib/simple_nested_set/instance_methods.rb', line 150 def move_right move_to_right_of(right_sibling) if right_sibling end |
#move_to_child_of(node) ⇒ Object
Moves the node to the child of another node
135 136 137 |
# File 'lib/simple_nested_set/instance_methods.rb', line 135 def move_to_child_of(node) node ? nested_set.move_to(node, :child) : move_to_root end |
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node
155 156 157 |
# File 'lib/simple_nested_set/instance_methods.rb', line 155 def move_to_left_of(node) nested_set.move_to(node, :left) end |
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node
160 161 162 |
# File 'lib/simple_nested_set/instance_methods.rb', line 160 def move_to_right_of(node) nested_set.move_to(node, :right) end |
#move_to_root ⇒ Object
Makes this node a root node
140 141 142 |
# File 'lib/simple_nested_set/instance_methods.rb', line 140 def move_to_root nested_set.move_to(nil, :root) end |
#nested_set ⇒ Object
5 6 7 |
# File 'lib/simple_nested_set/instance_methods.rb', line 5 def nested_set @_nested_set ||= nested_set_class.new(self) end |
#next_sibling ⇒ Object Also known as: right_sibling
Returns the righthand sibling
124 125 126 |
# File 'lib/simple_nested_set/instance_methods.rb', line 124 def next_sibling nested_set.with_right_sibling(rgt).first end |
#parent ⇒ Object
Returns the parent
47 48 49 |
# File 'lib/simple_nested_set/instance_methods.rb', line 47 def parent self.class.find(parent_id) unless root? end |
#previous_sibling ⇒ Object Also known as: left_sibling
Returns the lefthand sibling
118 119 120 |
# File 'lib/simple_nested_set/instance_methods.rb', line 118 def previous_sibling nested_set.with_left_sibling(lft).first end |
#root ⇒ Object
Returns the root
42 43 44 |
# File 'lib/simple_nested_set/instance_methods.rb', line 42 def root root? ? self : ancestors.first end |
#root? ⇒ Boolean
Returns true if this is a root node.
22 23 24 |
# File 'lib/simple_nested_set/instance_methods.rb', line 22 def root? parent_id.blank? end |
#self_and_ancestors ⇒ Object
Returns the array of all parents and self
67 68 69 |
# File 'lib/simple_nested_set/instance_methods.rb', line 67 def self_and_ancestors ancestors + [self] end |
#self_and_children ⇒ Object
Returns a set of only this entry’s immediate children including self
97 98 99 |
# File 'lib/simple_nested_set/instance_methods.rb', line 97 def self_and_children [self] + children end |
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children.
87 88 89 |
# File 'lib/simple_nested_set/instance_methods.rb', line 87 def self_and_descendants [self] + descendants end |
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, included self
113 114 115 |
# File 'lib/simple_nested_set/instance_methods.rb', line 113 def self_and_siblings nested_set.with_parent(parent_id) end |
#self_or_ancestor_of?(other) ⇒ Boolean
Returns true if this is equal to or an ancestor of the given node
57 58 59 |
# File 'lib/simple_nested_set/instance_methods.rb', line 57 def self_or_ancestor_of?(other) self == other || ancestor_of?(other) end |
#self_or_descendent_of?(other) ⇒ Boolean
Returns true if this is equal to or a descendent of the given node
77 78 79 |
# File 'lib/simple_nested_set/instance_methods.rb', line 77 def self_or_descendent_of?(other) self == other || descendent_of?(other) end |
#siblings ⇒ Object
Returns the array of all children of the parent, except self
108 109 110 |
# File 'lib/simple_nested_set/instance_methods.rb', line 108 def siblings self_and_siblings.without_node(id) end |