class CC::Workspace::PathTree::DirNode

Attributes

children[R]
root_path[R]

Public Class Methods

new(root_path, children = {}) click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 5
def initialize(root_path, children = {})
  @root_path = root_path.dup.freeze
  @children = children.each_with_object({}) do |(k, v), memo|
    memo[k.clone] = v.clone
  end
end

Public Instance Methods

add(head = nil, *tail) click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 38
def add(head = nil, *tail)
  return if head.nil? && tail.empty?

  if (entry = find_direct_child(head))
    children[entry.basename.to_s.dup.freeze] ||= PathTree.node_for_pathname(entry)
    children[entry.basename.to_s.dup.freeze].add(*tail)
  else
    Analyzer.logger.debug("Couldn't include because part of path doesn't exist: #{File.join(root_path, head)}")
  end
end
all_paths() click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 16
def all_paths
  if populated?
    children.values.flat_map(&:all_paths)
  else
    [File.join(root_path, File::SEPARATOR)]
  end
end
clone() click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 12
def clone
  self.class.new(root_path, children)
end
populated?() click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 24
def populated?
  children.present?
end
remove(head = nil, *tail) click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 28
def remove(head = nil, *tail)
  return if head.nil? && tail.empty?
  populate_direct_children

  if (child = children[head])
    child.remove(*tail)
    children.delete(head) if !child.populated? || tail.empty?
  end
end

Private Instance Methods

find_direct_child(name) click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 61
def find_direct_child(name)
  Pathname.new(root_path).children.detect { |c| c.basename.to_s == name }
end
populate_direct_children() click to toggle source
# File lib/cc/workspace/path_tree/dir_node.rb, line 53
def populate_direct_children
  return if populated?

  Pathname.new(root_path).each_child do |child_path|
    children[child_path.basename.to_s] = PathTree.node_for_pathname(child_path)
  end
end