Sha256: 88c8c9d9c2889ceb653f4dc672eac1f4e5dfabac8106b95eacc4766032183c8c

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 KB

Contents

module CC
  class Workspace
    class PathTree
      class DirNode
        def initialize(root_path, children = {})
          @root_path = root_path.dup.freeze
          @children = children
        end

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

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

        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) if !child.populated? || tail.empty?
          end
        end

        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

        private

        attr_reader :children, :root_path

        def populate_direct_children
          return if populated?

          Pathname.new(root_path).each_child do |child_path|
            children[child_path.basename.to_s] = PathTree.node_for_pathname(child_path)
          end
        end

        def find_direct_child(name)
          Pathname.new(root_path).children.detect { |c| c.basename.to_s == name }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
codeclimate-0.24.0 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.23.0 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.22.3 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.22.2 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.22.1 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.22.0 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.21.4 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.21.3 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.21.2 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.21.1 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.21.0 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.20.2 lib/cc/workspace/path_tree/dir_node.rb