Class: Spoom::FileTree
- Inherits:
-
Object
show all
- Extended by:
- T::Sig
- Defined in:
- lib/spoom/file_tree.rb
Overview
Build a file hierarchy from a set of file paths.
Defined Under Namespace
Classes: Node, TreePrinter
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(paths = [], strip_prefix: nil) ⇒ FileTree
Returns a new instance of FileTree.
13
14
15
16
17
|
# File 'lib/spoom/file_tree.rb', line 13
def initialize(paths = [], strip_prefix: nil)
@roots = T.let({}, T::Hash[String, Node])
@strip_prefix = strip_prefix
add_paths(paths)
end
|
Instance Attribute Details
#strip_prefix ⇒ Object
Returns the value of attribute strip_prefix.
10
11
12
|
# File 'lib/spoom/file_tree.rb', line 10
def strip_prefix
@strip_prefix
end
|
Instance Method Details
#add_path(path) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/spoom/file_tree.rb', line 29
def add_path(path)
prefix = @strip_prefix
path = path.delete_prefix("#{prefix}/") if prefix
parts = path.split("/")
if path.empty? || parts.size == 1
return @roots[path] ||= Node.new(parent: nil, name: path)
end
parent_path = T.must(parts[0...-1]).join("/")
parent = add_path(parent_path)
name = T.must(parts.last)
parent.children[name] ||= Node.new(parent: parent, name: name)
end
|
#add_paths(paths) ⇒ Object
21
22
23
|
# File 'lib/spoom/file_tree.rb', line 21
def add_paths(paths)
paths.each { |path| add_path(path) }
end
|
#nodes ⇒ Object
50
51
52
53
54
|
# File 'lib/spoom/file_tree.rb', line 50
def nodes
all_nodes = []
@roots.values.each { |root| collect_nodes(root, all_nodes) }
all_nodes
end
|
#paths ⇒ Object
58
59
60
|
# File 'lib/spoom/file_tree.rb', line 58
def paths
nodes.collect(&:path)
end
|
#print(out: $stdout, show_strictness: true, colors: true, indent_level: 0) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/spoom/file_tree.rb', line 70
def print(out: $stdout, show_strictness: true, colors: true, indent_level: 0)
printer = TreePrinter.new(
tree: self,
out: out,
show_strictness: show_strictness,
colors: colors,
indent_level: indent_level
)
printer.print_tree
end
|
#roots ⇒ Object
44
45
46
|
# File 'lib/spoom/file_tree.rb', line 44
def roots
@roots.values
end
|