Module: Tree::Utils::TreePathHandler

Included in:
TreeNode
Defined in:
lib/tree/utils/path_methods.rb

Overview

Provides utility methods for path extraction

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

noinspection RubyUnusedLocalVariable


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tree/utils/path_methods.rb', line 45

def self.included(base)

  # @!group Node Path

  # 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.
  def path_as_string(separator = '=>')
    path_as_array.join(separator)
  end

  # 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
  def path_as_array
    get_path_name_array.reverse
  end

  # @!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.
  def get_path_name_array(current_array_path = [])
    path_array = current_array_path + [name]

    if !parent              # If detached node or root node.
      path_array
    else                    # Else recurse to parent node.
      path_array = parent.get_path_name_array(path_array)
    end
  end

  protected :get_path_name_array

  # @!endgroup
end

Instance Method Details

#path_as_arrayArray

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.

node

Returns:

  • (Array)

    The array containing the node names for the path to this


67
68
69
# File 'lib/tree/utils/path_methods.rb', line 67

def path_as_array
  get_path_name_array.reverse
end

#path_as_string(separator = '=>') ⇒ String

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.

Parameters:

  • separator (defaults to: '=>')

    The optional separator to use. The default separator is '+=>+'.

Returns:

  • (String)

    The node path with names separated using the specified separator.


58
59
60
# File 'lib/tree/utils/path_methods.rb', line 58

def path_as_string(separator = '=>')
  path_as_array.join(separator)
end