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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/psd/nodes/ancestry.rb', line 70

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

#ancestorsObject

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

#childless?Boolean

Inverse of has_children?

Returns:

  • (Boolean)


30
31
32
# File 'lib/psd/nodes/ancestry.rb', line 30

def childless?
  !has_children?
end

#depthObject

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

#descendantsObject

Recursively get all descendant nodes, not including this node.



52
53
54
# File 'lib/psd/nodes/ancestry.rb', line 52

def descendants
  children.map(&:subtree).flatten
end

#has_children?Boolean

Does this node have any children nodes?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


42
43
44
# File 'lib/psd/nodes/ancestry.rb', line 42

def has_siblings?
  siblings.length > 1
end

#only_child?Boolean

Is this node the only descendant of its parent?

Returns:

  • (Boolean)


47
48
49
# File 'lib/psd/nodes/ancestry.rb', line 47

def only_child?
  siblings.length == 1
end

#pathObject



66
67
68
# File 'lib/psd/nodes/ancestry.rb', line 66

def path
  (ancestors.map(&:name) + [name]).join('/')
end

#rootObject

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?

Returns:

  • (Boolean)


12
13
14
# File 'lib/psd/nodes/ancestry.rb', line 12

def root?
  self.is_a?(PSD::Node::Root)
end

#siblingsObject

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

#subtreeObject

Same as descendants, except it includes this node.



57
58
59
# File 'lib/psd/nodes/ancestry.rb', line 57

def subtree
  [self] + descendants
end