Module: PSD::Node::Ancestry
- Included in:
- PSD::Node
- Defined in:
- lib/psd/nodes/ancestry.rb
Overview
Collection of methods to help in traversing the PSD tree structure.
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns all ancestors in the path of this node.
-
#depth ⇒ Object
Depth from the root node.
-
#descendants ⇒ Object
Recursively get all descendant nodes, not including this node.
-
#has_children? ⇒ Boolean
Does this node have any children nodes?.
-
#has_siblings? ⇒ Boolean
Does this node have any siblings?.
-
#is_childless? ⇒ Boolean
Inverse of has_children?.
-
#is_only_child? ⇒ Boolean
Is this node the only descendant of its parent?.
- #method_missing(method, *args, &block) ⇒ Object
-
#root ⇒ Object
Returns the root node.
-
#root? ⇒ Boolean
(also: #is_root?)
Is this node the root node?.
-
#siblings ⇒ Object
Returns all sibling nodes including the current node.
-
#subtree ⇒ Object
Same as descendants, except it includes this node.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/psd/nodes/ancestry.rb', line 66 def method_missing(method, *args, &block) test = /^(.+)_(layers|groups)$/.match(method) if test m = self.respond_to?(test[1]) ? test[1] : "#{test[1]}s" self.send(m).select &method("#{test[2]}_only") else super end end |
Instance Method Details
#ancestors ⇒ Object
Returns all ancestors in the path of this node. This does NOT return the root node.
19 20 21 22 |
# File 'lib/psd/nodes/ancestry.rb', line 19 def ancestors return [] if parent.nil? || parent.is_root? return parent.ancestors + [parent] end |
#depth ⇒ Object
Depth from the root node. Root depth is 0.
62 63 64 |
# File 'lib/psd/nodes/ancestry.rb', line 62 def depth return ancestors.length + 1 end |
#descendants ⇒ Object
Recursively get all descendant nodes, not including this node.
52 53 54 |
# File 'lib/psd/nodes/ancestry.rb', line 52 def descendants children + children.map(&:children).flatten end |
#has_children? ⇒ Boolean
Does this node have any children nodes?
25 26 27 |
# File 'lib/psd/nodes/ancestry.rb', line 25 def has_children? children.length > 0 end |
#has_siblings? ⇒ Boolean
Does this node have any siblings?
42 43 44 |
# File 'lib/psd/nodes/ancestry.rb', line 42 def has_siblings? siblings.length > 1 end |
#is_childless? ⇒ Boolean
Inverse of has_children?
30 31 32 |
# File 'lib/psd/nodes/ancestry.rb', line 30 def is_childless? !has_children? end |
#is_only_child? ⇒ Boolean
Is this node the only descendant of its parent?
47 48 49 |
# File 'lib/psd/nodes/ancestry.rb', line 47 def is_only_child? siblings.length == 1 end |
#root ⇒ Object
Returns the root node
6 7 8 9 |
# File 'lib/psd/nodes/ancestry.rb', line 6 def root return self if is_root? return parent.root end |
#root? ⇒ Boolean Also known as: is_root?
Is this node the root node?
12 13 14 |
# File 'lib/psd/nodes/ancestry.rb', line 12 def root? self.is_a?(PSD::Node::Root) end |
#siblings ⇒ Object
Returns all sibling nodes including the current node. Can also be thought of as all children of the parent of this node.
36 37 38 39 |
# File 'lib/psd/nodes/ancestry.rb', line 36 def siblings return [] if parent.nil? parent.children end |
#subtree ⇒ Object
Same as descendants, except it includes this node.
57 58 59 |
# File 'lib/psd/nodes/ancestry.rb', line 57 def subtree [self] + descendants end |