Class: Spoom::FileTree

Inherits:
Object
  • 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: CollectNodes, CollectScores, CollectStrictnesses, Node, Printer, Visitor

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ FileTree

Returns a new instance of FileTree.



10
11
12
13
# File 'lib/spoom/file_tree.rb', line 10

def initialize(paths = [])
  @roots = T.let({}, T::Hash[String, Node])
  add_paths(paths)
end

Instance Method Details

#add_path(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spoom/file_tree.rb', line 25

def add_path(path)
  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



17
18
19
# File 'lib/spoom/file_tree.rb', line 17

def add_paths(paths)
  paths.each { |path| add_path(path) }
end

#nodesObject



45
46
47
48
49
# File 'lib/spoom/file_tree.rb', line 45

def nodes
  v = CollectNodes.new
  v.visit_tree(self)
  v.nodes
end

#nodes_strictness_scores(context) ⇒ Object



67
68
69
70
71
# File 'lib/spoom/file_tree.rb', line 67

def nodes_strictness_scores(context)
  v = CollectScores.new(context)
  v.visit_tree(self)
  v.scores
end

#nodes_strictnesses(context) ⇒ Object



59
60
61
62
63
# File 'lib/spoom/file_tree.rb', line 59

def nodes_strictnesses(context)
  v = CollectStrictnesses.new(context)
  v.visit_tree(self)
  v.strictnesses
end

#pathsObject



53
54
55
# File 'lib/spoom/file_tree.rb', line 53

def paths
  nodes.map(&:path)
end

#paths_strictness_scores(context) ⇒ Object



75
76
77
# File 'lib/spoom/file_tree.rb', line 75

def paths_strictness_scores(context)
  nodes_strictness_scores(context).map { |node, score| [node.path, score] }.to_h
end


80
81
82
83
# File 'lib/spoom/file_tree.rb', line 80

def print(out: $stdout, colors: true)
  printer = Printer.new({}, out: out, colors: colors)
  printer.visit_tree(self)
end


86
87
88
89
90
91
# File 'lib/spoom/file_tree.rb', line 86

def print_with_strictnesses(context, out: $stdout, colors: true)
  strictnesses = nodes_strictnesses(context)

  printer = Printer.new(strictnesses, out: out, colors: colors)
  printer.visit_tree(self)
end

#rootsObject



39
40
41
# File 'lib/spoom/file_tree.rb', line 39

def roots
  @roots.values
end