Module: SimpleNestedSet::InstanceMethods

Defined in:
lib/simple_nested_set/instance_methods.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


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_treeObject 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

Returns:

  • (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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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_countObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


36
37
38
# File 'lib/simple_nested_set/instance_methods.rb', line 36

def leaf?
  rgt.to_i - lft.to_i == 1
end

#leavesObject

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_leftObject

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_rightObject

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_rootObject

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_setObject



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_siblingObject 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

#parentObject

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_siblingObject 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

#rootObject

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.

Returns:

  • (Boolean)


26
27
28
# File 'lib/simple_nested_set/instance_methods.rb', line 26

def root?
  parent_id.blank?
end

#self_and_ancestorsObject

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_childrenObject

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_descendantsObject

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_siblingsObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#siblingsObject

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