module Tree::Utils::TreePathHandler

Provides utility methods for path extraction

Public Instance Methods

path_as_array() click to toggle source

Returns the node-names from this node to the root as an array. The first element is the root node name, and the last element is this node’s name.

@return [Array] The array containing the node names for the path to this node

# File lib/tree/utils/path_methods.rb, line 64
def path_as_array
  get_path_name_array.reverse
end
path_as_string(separator = '=>') click to toggle source

Returns the path of this node from the root as a string, with the node names separated using the specified separator. The path is listed left to right from the root node.

@param separator The optional separator to use. The default separator is

'+=>+'.

@return [String] The node path with names separated using the specified

separator.
# File lib/tree/utils/path_methods.rb, line 55
def path_as_string(separator = '=>')
  path_as_array.join(separator)
end

Protected Instance Methods

get_path_name_array(current_array_path = []) click to toggle source

@!visibility private

Returns the path names in an array. The first element is the name of this node, and the last element is the root node name.

@return [Array] An array of the node names for the path from this node

to its root.
# File lib/tree/utils/path_methods.rb, line 75
def get_path_name_array(current_array_path = [])
  path_array = current_array_path + [name]

  if parent               # Recurse to parent node.
    parent.get_path_name_array(path_array)
  else                    # else If detached node or root node.
    path_array
  end
end