lib/cc/workspace/path_tree.rb in codeclimate-0.20.1 vs lib/cc/workspace/path_tree.rb in codeclimate-0.20.2

- old
+ new

@@ -2,109 +2,48 @@ require "set" module CC class Workspace class PathTree - def self.create(pathname) + autoload :DirNode, "cc/workspace/path_tree/dir_node" + autoload :FileNode, "cc/workspace/path_tree/file_node" + + def self.node_for_pathname(pathname) if pathname.directory? - new(pathname.to_s) + DirNode.new(pathname.to_s) else FileNode.new(pathname.to_s) end end - def initialize(root_path, children = {}) - @root_path = root_path.dup.freeze - @children = children + def self.for_path(path) + new(node_for_pathname(Pathname.new(path))) end + def initialize(root_node) + @root_node = root_node + end + def clone - self.class.new(root_path, children.dup) + self.class.new(root_node.clone) end def exclude_paths(paths) - paths.each { |path| remove(*normalized_path_pieces(path)) } + paths.each { |path| root_node.remove(*normalized_path_pieces(path)) } end def include_paths(paths) - paths.each { |path| add(*normalized_path_pieces(path)) } + paths.each { |path| root_node.add(*normalized_path_pieces(path)) } end - def all_paths - if populated? - children.values.flat_map(&:all_paths) - else - [File.join(root_path, File::SEPARATOR)] - end - end + delegate :all_paths, to: :root_node - protected - - def populated? - children.present? - end - - def remove(head = nil, *tail) - return if head.nil? && tail.empty? - populate_direct_children - - if (child = children[head]) - child.remove(*tail) - children.delete(head) unless child.populated? - end - end - - def add(head = nil, *tail) - return if head.nil? && tail.empty? - - if (entry = find_direct_child(head)) - children[entry.basename.to_s] = self.class.create(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 - private - attr_reader :children, :root_path + attr_reader :root_node - def populate_direct_children - return if populated? - - Pathname.new(root_path).each_child do |child_path| - children[child_path.basename.to_s] = self.class.create(child_path) - end - end - - def find_direct_child(name) - Pathname.new(root_path).children.detect { |c| c.basename.to_s == name } - end - def normalized_path_pieces(path) Pathname.new(path).cleanpath.to_s.split(File::SEPARATOR).reject(&:blank?) - end - - class FileNode - def initialize(root_path) - @root_path = root_path.dup.freeze - end - - def all_paths - [@root_path] - end - - def populated? - false - end - - def remove(*) - # this space intentionally left blank - end - - def add(*) - # this space intentionally left blank - end end end end end