Module: DataMapper::Is::AwesomeSet::InstanceMethods
- Defined in:
- lib/dm-is-awesome_set.rb
Overview
mod ClassMethods
Instance Method Summary collapse
- #ancestor?(descendant) ⇒ Boolean
-
#ancestors ⇒ Object
Gets all ancestors of this node.
-
#attributes_set(hash) ⇒ Object
:nodoc:.
- #descendant?(ancestor) ⇒ Boolean
-
#descendants ⇒ Object
Fixed spelling for when English majors are peering over your shoulder.
-
#descendents ⇒ Object
Gets all descendents of this node.
-
#destroy ⇒ Object
Destroys the current node and all children nodes, running their before and after hooks Returns the destroyed objects.
-
#destroy! ⇒ Object
Same as @destroy, but does not run the hooks.
-
#full_set ⇒ Object
Returns the full set within this scope.
-
#leaves ⇒ Object
Retrieves the nodes without any children.
- #level ⇒ Object
-
#move(vector) ⇒ Object
move self / node to a position in the set.
-
#next_sibling ⇒ Object
Returns next node with same parent, or nil.
-
#previous_sibling ⇒ Object
Returns previous node with same parent, or nil.
-
#root ⇒ Object
Gets the root of this node.
-
#roots ⇒ Object
Gets all the roots of this node’s tree.
-
#self_and_ancestors ⇒ Object
Same as ancestors, but also including this node.
- #self_and_descendants ⇒ Object
-
#self_and_descendents ⇒ Object
Same as descendents, but returns self as well.
-
#self_and_siblings ⇒ Object
Same as siblings, but returns this node as well.
- #self_or_ancestor?(descendant) ⇒ Boolean
- #self_or_descendant?(ancestor) ⇒ Boolean
-
#siblings ⇒ Object
Gets all nodes that share the same parent node, except for this node.
Instance Method Details
#ancestor?(descendant) ⇒ Boolean
241 242 243 |
# File 'lib/dm-is-awesome_set.rb', line 241 def ancestor?(descendant) descendant.lft > lft && descendant.rgt < rgt end |
#ancestors ⇒ Object
Gets all ancestors of this node
189 190 191 |
# File 'lib/dm-is-awesome_set.rb', line 189 def ancestors get_class.all(scope_hash.merge(:lft.lt => lft, :rgt.gt => rgt, :order => [:lft.asc])) end |
#attributes_set(hash) ⇒ Object
:nodoc:
258 259 260 261 |
# File 'lib/dm-is-awesome_set.rb', line 258 def attributes_set(hash) #:nodoc: hash = hash || {} self.attributes = hash end |
#descendant?(ancestor) ⇒ Boolean
233 234 235 |
# File 'lib/dm-is-awesome_set.rb', line 233 def descendant?(ancestor) ancestor.lft < lft && ancestor.rgt > rgt end |
#descendants ⇒ Object
Fixed spelling for when English majors are peering over your shoulder
250 |
# File 'lib/dm-is-awesome_set.rb', line 250 def descendants; descendents; end |
#descendents ⇒ Object
Gets all descendents of this node
224 225 226 |
# File 'lib/dm-is-awesome_set.rb', line 224 def descendents get_class.all(scope_hash.merge(:rgt.lt => rgt, :lft.gt => lft, :order => [:lft.asc])) end |
#destroy ⇒ Object
Destroys the current node and all children nodes, running their before and after hooks Returns the destroyed objects
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/dm-is-awesome_set.rb', line 265 def destroy return true if destroyed? catch :halt do sads = self_and_descendants # Trigger all the before :destroy methods sads.each { |sad| sad.before_destroy_hook } # dup is called here because destroy! likes to clear out the array, understandably. get_class.transaction do sads.dup.destroy! adjust_gap!(full_set, lft, -(rgt - lft + 1)) end # Now go through after all the after :destroy methods. sads.each { |sad| sad.after_destroy_hook } end destroyed? end |
#destroy! ⇒ Object
Same as @destroy, but does not run the hooks
283 284 285 286 287 288 289 290 291 |
# File 'lib/dm-is-awesome_set.rb', line 283 def destroy! return true if destroyed? sad = self_and_descendants get_class.transaction do sad.dup.destroy! adjust_gap!(full_set, lft, -(rgt - lft + 1)) end destroyed? end |
#full_set ⇒ Object
Returns the full set within this scope
219 220 221 |
# File 'lib/dm-is-awesome_set.rb', line 219 def full_set get_class.all(scope_hash) end |
#leaves ⇒ Object
Retrieves the nodes without any children.
254 255 256 |
# File 'lib/dm-is-awesome_set.rb', line 254 def leaves get_class.leaves(self) end |
#level ⇒ Object
174 175 176 |
# File 'lib/dm-is-awesome_set.rb', line 174 def level ancestors.length end |
#move(vector) ⇒ Object
move self / node to a position in the set. position can only be changed through this
166 167 168 169 170 171 172 |
# File 'lib/dm-is-awesome_set.rb', line 166 def move(vector) get_class.transaction do move_without_saving(vector) save! end reload end |
#next_sibling ⇒ Object
Returns next node with same parent, or nil
209 210 211 |
# File 'lib/dm-is-awesome_set.rb', line 209 def next_sibling get_class.first(scope_and_parent_hash.merge(:lft.gt => rgt, :order => [:lft.asc])) end |
#previous_sibling ⇒ Object
Returns previous node with same parent, or nil
214 215 216 |
# File 'lib/dm-is-awesome_set.rb', line 214 def previous_sibling get_class.first(scope_and_parent_hash.merge(:rgt.lt => lft, :order => [:rgt.desc])) end |
#root ⇒ Object
Gets the root of this node
179 180 181 |
# File 'lib/dm-is-awesome_set.rb', line 179 def root get_class.first(root_hash.merge(:lft.lt => lft, :rgt.gt => rgt)) end |
#roots ⇒ Object
Gets all the roots of this node’s tree
184 185 186 |
# File 'lib/dm-is-awesome_set.rb', line 184 def roots get_class.all(root_hash.merge(:order => [:lft.asc])) end |
#self_and_ancestors ⇒ Object
Same as ancestors, but also including this node
194 195 196 |
# File 'lib/dm-is-awesome_set.rb', line 194 def self_and_ancestors get_class.all(scope_hash.merge(:lft.lte => lft, :rgt.gte => rgt, :order => [:lft.asc])) end |
#self_and_descendants ⇒ Object
251 |
# File 'lib/dm-is-awesome_set.rb', line 251 def self_and_descendants; self_and_descendents; end |
#self_and_descendents ⇒ Object
Same as descendents, but returns self as well
229 230 231 |
# File 'lib/dm-is-awesome_set.rb', line 229 def self_and_descendents get_class.all(scope_hash.merge(:rgt.lte => rgt, :lft.gte => lft, :order => [:lft.asc])) end |
#self_and_siblings ⇒ Object
Same as siblings, but returns this node as well
204 205 206 |
# File 'lib/dm-is-awesome_set.rb', line 204 def self_and_siblings get_class.all(scope_and_parent_hash.merge(:order => [:lft.asc])) end |
#self_or_ancestor?(descendant) ⇒ Boolean
245 246 247 |
# File 'lib/dm-is-awesome_set.rb', line 245 def self_or_ancestor?(descendant) descendant.lft >= lft && descendant.rgt <= rgt end |
#self_or_descendant?(ancestor) ⇒ Boolean
237 238 239 |
# File 'lib/dm-is-awesome_set.rb', line 237 def self_or_descendant?(ancestor) ancestor.lft <= lft && ancestor.rgt >= rgt end |
#siblings ⇒ Object
Gets all nodes that share the same parent node, except for this node
199 200 201 |
# File 'lib/dm-is-awesome_set.rb', line 199 def siblings get_class.all(scope_and_parent_hash.merge(:order => [:lft.asc], :lft.not => lft)) end |