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

public included(base)
[View source]


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

public ancestor

get the parent of this node. Same as #parent, but finds it from lft/rgt instead of parent-key

Meta Tags

Returns:

<Resource, NilClass> returns the parent-object, or nil if this is root/detached

[View source]


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

public ancestors

get all ancestors of this node

Meta Tags

Returns:

<Collection> collection of all parents, with root as first item

[View source]


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

public descendants

get all descendants of this node

Meta Tags

Returns:

<Collection> flat collection, sorted according to nested_set positions

[View source]


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?

public leaf?

check if this node is a leaf (does not have subnodes). use this instead ofdescendants.empty?

[View source]


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

public leaves

get all descendants of this node that does not have any children

Meta Tags

Returns:

<Collection>

[View source]


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

public left_sibling

get sibling to the left of/above this node in the nested tree

Meta Tags

Returns:

<Resource, NilClass> the resource to the left, or nil if self is leftmost

See Also:

#self_and_siblings
[View source]


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

public level

get the level of this node, where 0 is root. temporary solution

Meta Tags

Returns:

<Integer>

[View source]


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

public right_sibling

get sibling to the right of/above this node in the nested tree

Meta Tags

Returns:

<Resource, NilClass> the resource to the right, or nil if self is rightmost

See Also:

#self_and_siblings
[View source]


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

public 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

Meta Tags

Returns:

<Resource, NilClass>

[View source]


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

public self_and_ancestors

all methods for finding related nodes following

get all ancestors of this node, up to (and including) self

Meta Tags

Returns:

<Collection> Returns

[View source]


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

public self_and_descendants

get all descendants of this node, including self

Meta Tags

Returns:

<Collection> flat collection, sorted according to nested_set positions

[View source]


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

public self_and_siblings

get all siblings of this node, and include self

Meta Tags

Returns:

<Collection>

[View source]


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

public siblings

get all siblings of this node

Meta Tags

Returns:

<Collection>

See Also:

#self_and_siblings
[View source]


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