Module: DataMapper::Is::NestedSet
Public Visibility
Public Class Method Summary
| included(base) |
|---|
Public Instance Method Summary
| #ancestor |
get the parent of this node. Returns: |
|---|---|
| #ancestors |
get all ancestors of this node. Returns: |
| #descendants |
get all descendants of this node. Returns: |
| #leaf? |
check if this node is a leaf (does not have subnodes). |
| #leaves |
get all descendants of this node that does not have any children. Returns: |
| #left_sibling |
get sibling to the left of/above this node in the nested tree. Returns: |
| #level |
get the level of this node, where 0 is root. Returns: |
| #right_sibling |
get sibling to the right of/above this node in the nested tree. Returns: |
| #root |
get the root this node belongs to. Returns: |
| #self_and_ancestors |
all methods for finding related nodes following. Returns: |
| #self_and_descendants |
get all descendants of this node, including self. Returns: |
| #self_and_siblings |
get all siblings of this node, and include self. Returns: |
| #siblings |
get all siblings of this node. Returns: |
Public Class Method Details
included
4 5 6
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 4 def self.included(base) base.extend(GenerateMethod) end
Public Instance Method Details
ancestor
get the parent of this node. Same as #parent, but finds it from lft/rgt instead of parent-key
255 256 257 258 259 260
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 255 def ancestor ancestors.reverse.first end
ancestors
get all ancestors of this node
247 248 249 250 251 252
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 247 def ancestors self_and_ancestors.reject{|r| r.key == self.key } # because identitymap is not used in console end
descendants
get all descendants of this node
281 282 283 284 285 286
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 281 def descendants self_and_descendants.reject{|r| r.key == self.key } # because identitymap is not used in console end
leaf?
check if this node is a leaf (does not have subnodes). use this instead ofdescendants.empty?
226 227 228 229 230 231
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 226 def leaf? rgt-lft == 1 end
leaves
get all descendants of this node that does not have any children
289 290 291 292 293 294
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 289 def leaves self.class.all(:lft => (lft+1)..rgt, :conditions=>["rgt=lft+1"], :order => [:lft.asc]) end
left_sibling
get sibling to the left of/above this node in the nested tree
316 317 318 319 320 321
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 316 def left_sibling self_and_siblings.find {|v| v.rgt == lft-1} end
level
get the level of this node, where 0 is root. temporary solution
217 218 219 220 221 222
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 217 def level ancestors.length end
right_sibling
get sibling to the right of/above this node in the nested tree
325 326 327 328 329 330
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 325 def right_sibling self_and_siblings.find {|v| v.lft == rgt+1} end
root
get the root this node belongs to. this will atm always be the same as Resource.root, but has a meaning when scoped sets is implemented
264 265 266 267 268 269
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 264 def root ancestors.first end
self_and_ancestors
all methods for finding related nodes following
get all ancestors of this node, up to (and including) self
238 239 240 241 242 243
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 238 def self_and_ancestors self.class.all(:lft.lte => lft, :rgt.gte => rgt, :order => [:lft.asc]) end
self_and_descendants
get all descendants of this node, including self
272 273 274 275 276 277
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 272 def self_and_descendants self.class.all(:lft => lft..rgt, :order => [:lft.asc]) end
self_and_siblings
get all siblings of this node, and include self
297 298 299 300 301 302 303
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 297 def self_and_siblings parent_key = self.class.relationships(:default)[:parent].child_key.to_a.first parent ? self.class.all(parent_key => parent.key) : [self] end
siblings
get all siblings of this node
307 308 309 310 311 312
# File 'dm-more/dm-is-nested_set/lib/dm-is-nested_set/is/nested_set.rb', line 307 def siblings self_and_siblings.reject{|r| r.key == self.key } # because identitymap is not used in console end