class TermUtils::PropertyTreeNode

Represents a general-purpose tree node that holds a key-value pair.

Attributes

child_nodes[RW]

@return [Array<PropertyTreeNode>]

key[RW]

@return [Object]

parent_node[RW]

@return [PropertyTreeNode]

value[RW]

@return [Object]

Public Class Methods

new(opts = {}, &block) click to toggle source

Creates a new PropertyTreeNode. @param opts [Hash] option opts [Object] :key option opts [Object] :value

# File lib/term_utils/property_tree_node.rb, line 35
def initialize(opts = {}, &block)
  @parent_node = nil
  @child_nodes = nil
  @key = opts.fetch(:key, nil)
  @value = opts.fetch(:value, nil)
  block.call(self) if block
end

Public Instance Methods

child_node(key) click to toggle source

Returns the child node identified by a given key. @param key [Object] @return [PropertyTreeNode, nil]

# File lib/term_utils/property_tree_node.rb, line 72
def child_node(key)
  if @child_nodes
    @child_nodes.find { |n| n.key == key }
  end
end
collect_nodes(opts = {}) click to toggle source

Collects nodes. @param opts [Hash] @option opts [Array] :path @option opts [Boolean] :leaf_only @return [Array<PropertyTreeNode>]

# File lib/term_utils/property_tree_node.rb, line 155
def collect_nodes(opts = {})
  nodes = []
  each_node(opts) do |n|
    nodes << n
  end
  nodes
end
collect_paths(opts = {}) click to toggle source

Collects node paths. @param opts [Hash] @option opts [Array] :path @option opts [Boolean] :leaf_only @return [Array<Array<Object>>]

# File lib/term_utils/property_tree_node.rb, line 168
def collect_paths(opts = {})
  paths = []
  each_node(opts) do |n|
    paths << n.path
  end
  paths
end
collect_values(opts = {}) click to toggle source

Collect node values. @param opts [Hash] @option opts [Array] :path @option opts [Boolean] :leaf_only @return [Array<Object>]

# File lib/term_utils/property_tree_node.rb, line 181
def collect_values(opts = {})
  vals = []
  each_node(opts) do |n|
    vals << n.value if n.value
  end
  vals
end
define_node(opts = {}, &block) click to toggle source

Creates a new node and adds it as a child. @param opts [Hash] @option opts [Object] :key @option opts [Object] :value @return [PropertyTreeNode]

# File lib/term_utils/property_tree_node.rb, line 83
def define_node(opts = {}, &block)
  new_node = TermUtils::PropertyTreeNode.new(opts)
  new_node.parent_node = self
  @child_nodes = [] unless @child_nodes
  @child_nodes << new_node
  block.call(new_node) if block
  new_node
end
each_node(opts = {}, &block) click to toggle source

Iterates over every node. @param opts [Hash] @option opts [Array] :path @option opts [Boolean] :leaf_only @return [nil]

# File lib/term_utils/property_tree_node.rb, line 105
def each_node(opts = {}, &block)
  rpath = nil
  if opts.key? :path
    rpath = opts[:path].dup
  end
  dive = true
  if @key
    hide = false
    if rpath
      if rpath.shift == @key
        unless rpath.empty?
          hide = true
        end
      else
        dive = false
        hide = true
      end
    end
    unless hide || (opts[:leaf_only] && @child_nodes)
      if opts.key? :block
        opts[:block].call(self)
      elsif block
        block.call(self)
      end
    end
  end # if @key
  if dive && @child_nodes
    ropts = opts.dup
    if rpath
      if rpath.empty?
        ropts.delete :path
      else
        ropts[:path] = rpath
      end
    end
    if block
      ropts[:block] = block
    end
    @child_nodes.each do |n|
      n.each_node(ropts)
    end
  end
  nil
end
eval_child_count(path) click to toggle source

Evaluates the total number of nodes in the tree represented by this one. @param path [Array<Object>] @return [Integer]

# File lib/term_utils/property_tree_node.rb, line 211
def eval_child_count(path)
  node = find_node(path)
  if node
    node.child_nodes ? node.child_nodes.length : 0
  end
end
find_node(path) click to toggle source

Finds the node identified by a given path of keys. @param path [Array<Object>] @return [PropertyTreeNode]

# File lib/term_utils/property_tree_node.rb, line 192
def find_node(path)
  catch :found do
    each_node(path: path) do |n|
      throw :found, n
    end
    nil
  end
end
find_node_value(path) click to toggle source

Finds the node identified by a given path of keys and returns its value. @param path [Array<Object>] @return [Object]

# File lib/term_utils/property_tree_node.rb, line 221
def find_node_value(path)
  node = find_node(path)
  if node
    node.value
  end
end
head?() click to toggle source

Tests whether this one is the head of the tree (i.e. has no parent). @return [Boolean]

# File lib/term_utils/property_tree_node.rb, line 59
def head?
  @parent_node == nil
end
initialize_dup(other) click to toggle source

For dup method.

Calls superclass method
# File lib/term_utils/property_tree_node.rb, line 44
def initialize_dup(other)
  @parent_node = nil
  if other.child_nodes
    @child_nodes = []
    other.child_nodes.each do |n|
      new_node = n.dup
      new_node.parent_node = self
      @child_nodes << new_node
    end
  end
  super
end
leaf?() click to toggle source

Tests whether this one is a leaf of the tree (i.e. has no child). @return [Boolean]

# File lib/term_utils/property_tree_node.rb, line 65
def leaf?
  @child_nodes == nil
end
node_exists?(path) click to toggle source

Tests whether the node identified by a given path of keys exists. @param path [Array<Object>] @return [Boolean]

# File lib/term_utils/property_tree_node.rb, line 204
def node_exists?(path)
  find_node(path) != nil
end
path() click to toggle source

Builds the path of keys. @return [Array<Object>]

# File lib/term_utils/property_tree_node.rb, line 94
def path
  p = @parent_node ? @parent_node.path : []
  p << @key if @key
  p
end