Skip to content

Commit 52531f5

Browse files
authored
Merge pull request #314 from r4do/master
Added instance methods to determine the relationship between 2 nodes
2 parents 6fcfe12 + a510241 commit 52531f5

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,22 +346,27 @@ When you include ```has_closure_tree``` in your model, you can provide a hash to
346346
347347
* ```tag.root``` returns the root for this node
348348
* ```tag.root?``` returns true if this is a root node
349+
* ```tag.root_of?(node)``` returns true if current node is root of another one
349350
* ```tag.child?``` returns true if this is a child node. It has a parent.
350351
* ```tag.leaf?``` returns true if this is a leaf node. It has no children.
351352
* ```tag.leaves``` is scoped to all leaf nodes in self_and_descendants.
352353
* ```tag.depth``` returns the depth, or "generation", for this node in the tree. A root node will have a value of 0.
353354
* ```tag.parent``` returns the node's immediate parent. Root nodes will return nil.
355+
* ```tag.parent_of?(node)``` returns true if current node is parent of another one
354356
* ```tag.children``` is a ```has_many``` of immediate children (just those nodes whose parent is the current node).
355357
* ```tag.child_ids``` is an array of the IDs of the children.
358+
* ```tag.child_of?(node)``` returns true if current node is child of another one
356359
* ```tag.ancestors``` is a ordered scope of [ parent, grandparent, great grandparent, … ]. Note that the size of this array will always equal ```tag.depth```.
357360
* ```tag.ancestor_ids``` is an array of the IDs of the ancestors.
361+
* ```tag.ancestor_of?(node)``` returns true if current node is ancestor of another one
358362
* ```tag.self_and_ancestors``` returns a scope containing self, parent, grandparent, great grandparent, etc.
359363
* ```tag.self_and_ancestors_ids``` returns IDs containing self, parent, grandparent, great grandparent, etc.
360364
* ```tag.siblings``` returns a scope containing all nodes with the same parent as ```tag```, excluding self.
361365
* ```tag.sibling_ids``` returns an array of the IDs of the siblings.
362366
* ```tag.self_and_siblings``` returns a scope containing all nodes with the same parent as ```tag```, including self.
363367
* ```tag.descendants``` returns a scope of all children, childrens' children, etc., excluding self ordered by depth.
364368
* ```tag.descendant_ids``` returns an array of the IDs of the descendants.
369+
* ```tag.descendant_of?(node)``` returns true if current node is descendant of another one
365370
* ```tag.self_and_descendants``` returns a scope of self, all children, childrens' children, etc., ordered by depth.
366371
* ```tag.self_and_descendant_ids``` returns IDs of self, all children, childrens' children, etc., ordered by depth.
367372
* ```tag.hash_tree``` returns an [ordered, nested hash](#nested-hashes) that can be depth-limited.

lib/closure_tree/model.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,31 @@ def sibling_ids
134134
_ct.ids_from(siblings)
135135
end
136136

137+
# node's parent is this record
138+
def parent_of?(node)
139+
self == node.parent
140+
end
141+
142+
# node's root is this record
143+
def root_of?(node)
144+
self == node.root
145+
end
146+
147+
# node's ancestors include this record
148+
def ancestor_of?(node)
149+
node.ancestors.include? self
150+
end
151+
152+
# node is record's ancestor
153+
def descendant_of?(node)
154+
self.ancestors.include? node
155+
end
156+
157+
# node is record's parent
158+
def child_of?(node)
159+
self.parent == node
160+
end
161+
137162
# Alias for appending to the children collection.
138163
# You can also add directly to the children collection, if you'd prefer.
139164
def add_child(child_node)

spec/label_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,41 @@ def roots_name_and_order
551551
expect(tree[@b]).to eq({})
552552
end
553553
end
554+
555+
context 'relationship between nodes' do
556+
before do
557+
create_label_tree
558+
end
559+
560+
it "checks parent of node" do
561+
expect(@a1.parent_of?(@b1)).to be_truthy
562+
expect(@c2.parent_of?(@d2)).to be_truthy
563+
expect(@c1.parent_of?(@b1)).to be_falsey
564+
end
565+
566+
it "checks children of node" do
567+
expect(@d1.child_of?(@c1)).to be_truthy
568+
expect(@c2.child_of?(@b1)).to be_truthy
569+
expect(@c3.child_of?(@b1)).to be_falsey
570+
end
571+
572+
it "checks root of node" do
573+
expect(@a1.root_of?(@d1)).to be_truthy
574+
expect(@a1.root_of?(@c2)).to be_truthy
575+
expect(@a2.root_of?(@c2)).to be_falsey
576+
end
577+
578+
it "checks ancestor of node" do
579+
expect(@a1.ancestor_of?(@d1)).to be_truthy
580+
expect(@b1.ancestor_of?(@d1)).to be_truthy
581+
expect(@b1.ancestor_of?(@c3)).to be_falsey
582+
end
583+
584+
it "checks descendant of node" do
585+
expect(@c1.descendant_of?(@a1)).to be_truthy
586+
expect(@d2.descendant_of?(@a1)).to be_truthy
587+
expect(@b1.descendant_of?(@a2)).to be_falsey
588+
end
589+
end
590+
554591
end

0 commit comments

Comments
 (0)