Module: Edge::Forest::InstanceMethods

Defined in:
lib/edge/forest.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns all ancestors ordered by nearest ancestors first.



141
142
143
144
145
146
147
148
149
# File 'lib/edge/forest.rb', line 141

def ancestors
  _ancestors = []
  node = self
  while(node = node.parent)
    _ancestors.push(node)
  end

  _ancestors
end

#descendantsObject

Returns all descendants



152
153
154
155
156
157
158
# File 'lib/edge/forest.rb', line 152

def descendants
  if children.present?
    children + children.map(&:descendants).flatten
  else
    []
  end
end

#rootObject

Returns the root of this node. If this node is root returns self.



125
126
127
# File 'lib/edge/forest.rb', line 125

def root
  parent ? parent.root : self
end

#root?Boolean

Returns true is this node is a root or false otherwise



130
131
132
# File 'lib/edge/forest.rb', line 130

def root?
  !parent_id
end

#siblingsObject

Returns all sibling nodes (nodes that have the same parent). If this node is a root node it returns an empty array.



136
137
138
# File 'lib/edge/forest.rb', line 136

def siblings
  parent ? parent.children - [self] : []
end