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(opts = {}) ⇒ Object
Returns an array of all parents.
-
#as_tree ⇒ Object
(also: #load_tree)
Returns self with the children association recursively pre-populated.
- #attributes=(attributes, *args) ⇒ Object
- #changed? ⇒ Boolean
-
#child? ⇒ Boolean
Returns true if this is a child node.
-
#children? ⇒ Boolean
(also: #has_children?)
Returns true if the node has any children.
-
#descendants(opts = {}) ⇒ 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.
-
#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_path(path) ⇒ Object
Makes this node to the given path.
-
#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
41 42 43 |
# File 'lib/simple_nested_set/instance_methods.rb', line 41 def <=>(other) lft <=> other.lft end |
#ancestor_of?(other) ⇒ Boolean
Returns true if this is an ancestor of the given node
56 57 58 |
# File 'lib/simple_nested_set/instance_methods.rb', line 56 def ancestor_of?(other) lft < other.lft && rgt > other.rgt end |
#ancestors(opts = {}) ⇒ Object
Returns an array of all parents
66 67 68 |
# File 'lib/simple_nested_set/instance_methods.rb', line 66 def ancestors(opts = {}) nested_set.with_ancestors(lft, rgt, opts) end |
#as_tree ⇒ Object Also known as: load_tree
Returns self with the children association recursively pre-populated
19 20 21 22 |
# File 'lib/simple_nested_set/instance_methods.rb', line 19 def as_tree nested_set.populate_associations(descendants) self end |
#attributes=(attributes, *args) ⇒ Object
13 14 15 16 |
# File 'lib/simple_nested_set/instance_methods.rb', line 13 def attributes=(attributes, *args) @_nested_set_attributes = nested_set_class.extract_attributes!(attributes) super(attributes, *args) end |
#changed? ⇒ Boolean
9 10 11 |
# File 'lib/simple_nested_set/instance_methods.rb', line 9 def changed? super || @_nested_set_attributes end |
#child? ⇒ Boolean
Returns true if this is a child node
31 32 33 |
# File 'lib/simple_nested_set/instance_methods.rb', line 31 def child? !root? end |
#children? ⇒ Boolean Also known as: has_children?
Returns true if the node has any children
106 107 108 |
# File 'lib/simple_nested_set/instance_methods.rb', line 106 def children? descendants_count > 0 end |
#descendants(opts = {}) ⇒ Object
Returns a set of all of its children and nested children.
86 87 88 |
# File 'lib/simple_nested_set/instance_methods.rb', line 86 def descendants(opts = {}) nested_set.with_descendants(lft, rgt, opts) end |
#descendants_count ⇒ Object
Returns the number of descendants
96 97 98 |
# File 'lib/simple_nested_set/instance_methods.rb', line 96 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
76 77 78 |
# File 'lib/simple_nested_set/instance_methods.rb', line 76 def descendent_of?(other) lft > other.lft && rgt < other.rgt end |
#leaf? ⇒ Boolean
Returns true if this is a leaf node
36 37 38 |
# File 'lib/simple_nested_set/instance_methods.rb', line 36 def leaf? rgt.to_i - lft.to_i == 1 end |
#leaves ⇒ Object
Returns all descendants that are leaves
134 135 136 |
# File 'lib/simple_nested_set/instance_methods.rb', line 134 def leaves rgt - lft == 1 ? [] : nested_set.with_descendants(lft, rgt).with_leaves end |
#move_left ⇒ Object
Moves the node to the left of its left sibling if any
149 150 151 |
# File 'lib/simple_nested_set/instance_methods.rb', line 149 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
154 155 156 |
# File 'lib/simple_nested_set/instance_methods.rb', line 154 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
139 140 141 |
# File 'lib/simple_nested_set/instance_methods.rb', line 139 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. If this other node is nil then this means the node is to be made the rightmost sibling.
160 161 162 163 164 165 166 |
# File 'lib/simple_nested_set/instance_methods.rb', line 160 def move_to_left_of(node) if node nested_set.move_to(node, :left) elsif right_most = siblings.last move_to_right_of(right_most) end end |
#move_to_path(path) ⇒ Object
Makes this node to the given path
179 180 181 |
# File 'lib/simple_nested_set/instance_methods.rb', line 179 def move_to_path(path) nested_set.move_to_path(path) end |
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node. If this other node is nil then this means the node is to be made the leftmost sibling.
170 171 172 173 174 175 176 |
# File 'lib/simple_nested_set/instance_methods.rb', line 170 def move_to_right_of(node) if node nested_set.move_to(node, :right) elsif left_most = siblings.first move_to_left_of(left_most) end end |
#move_to_root ⇒ Object
Makes this node a root node
144 145 146 |
# File 'lib/simple_nested_set/instance_methods.rb', line 144 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
128 129 130 |
# File 'lib/simple_nested_set/instance_methods.rb', line 128 def next_sibling nested_set.with_right_sibling(rgt).first end |
#parent ⇒ Object
Returns the parent
51 52 53 |
# File 'lib/simple_nested_set/instance_methods.rb', line 51 def parent self.class.find(parent_id) unless root? end |
#previous_sibling ⇒ Object Also known as: left_sibling
Returns the lefthand sibling
122 123 124 |
# File 'lib/simple_nested_set/instance_methods.rb', line 122 def previous_sibling nested_set.with_left_sibling(lft).first end |
#root ⇒ Object
Returns the root
46 47 48 |
# File 'lib/simple_nested_set/instance_methods.rb', line 46 def root root? ? self : ancestors.first end |
#root? ⇒ Boolean
Returns true if this is a root node.
26 27 28 |
# File 'lib/simple_nested_set/instance_methods.rb', line 26 def root? parent_id.blank? end |
#self_and_ancestors ⇒ Object
Returns the array of all parents and self
71 72 73 |
# File 'lib/simple_nested_set/instance_methods.rb', line 71 def self_and_ancestors ancestors(:include_self => true) end |
#self_and_children ⇒ Object
Returns a set of only this entry’s immediate children including self
101 102 103 |
# File 'lib/simple_nested_set/instance_methods.rb', line 101 def self_and_children [self] + children end |
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children.
91 92 93 |
# File 'lib/simple_nested_set/instance_methods.rb', line 91 def self_and_descendants descendants(:include_self => true) end |
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, included self
117 118 119 |
# File 'lib/simple_nested_set/instance_methods.rb', line 117 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
61 62 63 |
# File 'lib/simple_nested_set/instance_methods.rb', line 61 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
81 82 83 |
# File 'lib/simple_nested_set/instance_methods.rb', line 81 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
112 113 114 |
# File 'lib/simple_nested_set/instance_methods.rb', line 112 def siblings self_and_siblings.without_node(id) end |