class Spoom::FileTree
Build a file hierarchy from a set of file paths.
Attributes
strip_prefix[R]
Public Class Methods
new(paths = [], strip_prefix: nil)
click to toggle source
# 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
Public Instance Methods
add_path(path)
click to toggle source
# 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)
click to toggle source
# File lib/spoom/file_tree.rb, line 21 def add_paths(paths) paths.each { |path| add_path(path) } end
nodes()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/spoom/file_tree.rb, line 44 def roots @roots.values end
Private Instance Methods
collect_nodes(node, collected_nodes = [])
click to toggle source
# File lib/spoom/file_tree.rb, line 84 def collect_nodes(node, collected_nodes = []) collected_nodes << node node.children.values.each { |child| collect_nodes(child, collected_nodes) } collected_nodes end