Module: PSD::Node::Ancestry

Included in:
Base
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



83
84
85
86
87
88
89
90
91
# File 'lib/psd/nodes/ancestry.rb', line 83

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

#childless? ⇒ Boolean

Inverse of has_children?

Returns:

  • (Boolean)


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

def childless?
  !has_children?
end

#depth ⇒ Object

Depth from the root node. Root depth is 0.



74
75
76
# File 'lib/psd/nodes/ancestry.rb', line 74

def depth
  return ancestors.length + 1
end

#descendants ⇒ Object

Recursively get all descendant nodes, not including this node.



64
65
66
# File 'lib/psd/nodes/ancestry.rb', line 64

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)


54
55
56
# File 'lib/psd/nodes/ancestry.rb', line 54

def has_siblings?
  siblings.length > 1
end

#next_sibling ⇒ Object



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

def next_sibling
  return nil if parent.nil?
  index = siblings.index(self)
  siblings[index + 1]
end

#only_child? ⇒ Boolean

Is this node the only descendant of its parent?

Returns:

  • (Boolean)


59
60
61
# File 'lib/psd/nodes/ancestry.rb', line 59

def only_child?
  siblings.length == 1
end

#path(as_array = false) ⇒ Object



78
79
80
81
# File 'lib/psd/nodes/ancestry.rb', line 78

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

#prev_sibling ⇒ Object



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

def prev_sibling
  return nil if parent.nil?
  index = siblings.index(self)
  siblings[index - 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?

Returns:

  • (Boolean)


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.



69
70
71
# File 'lib/psd/nodes/ancestry.rb', line 69

def subtree
  [self] + descendants
end