Class: CC::Workspace::PathTree::DirNode

Inherits:
Object
  • Object
show all
Defined in:
lib/cc/workspace/path_tree/dir_node.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_path, children = {}) ⇒ DirNode

Returns a new instance of DirNode.



5
6
7
8
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 5

def initialize(root_path, children = {})
  @root_path = root_path.dup.freeze
  @children = children
end

Instance Method Details

#add(head = nil, *tail) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 36

def add(head = nil, *tail)
  return if head.nil? && tail.empty?

  if (entry = find_direct_child(head))
    children[entry.basename.to_s] = PathTree.node_for_pathname(entry)
    children[entry.basename.to_s].add(*tail)
  else
    CLI.debug("Couldn't include because part of path doesn't exist.", path: File.join(root_path, head))
  end
end

#all_pathsObject



14
15
16
17
18
19
20
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 14

def all_paths
  if populated?
    children.values.flat_map(&:all_paths)
  else
    [File.join(root_path, File::SEPARATOR)]
  end
end

#cloneObject



10
11
12
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 10

def clone
  self.class.new(root_path, children.dup)
end

#populated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 22

def populated?
  children.present?
end

#remove(head = nil, *tail) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/cc/workspace/path_tree/dir_node.rb', line 26

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