Sha256: e75dc77db7b8c4e4e0bb7690d1d85487906d7e67b45d60dbb8dcdbad686edeeb

Contents?: true

Size: 1.78 KB

Versions: 97

Compression:

Stored size: 1.78 KB

Contents

module CC
  class Workspace
    class PathTree
      class DirNode
        def initialize(root_path, children = {})
          @root_path = root_path.dup.freeze
          @children = children.each_with_object({}) do |(k, v), memo|
            memo[k.clone] = v.clone
          end
        end

        def clone
          self.class.new(root_path, children)
        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.dup.freeze] ||= PathTree.node_for_pathname(entry)
            children[entry.basename.to_s.dup.freeze].add(*tail)
          else
            Analyzer.logger.debug("Couldn't include because part of path doesn't exist: #{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

97 entries across 97 versions & 2 rubygems

Version Path
codeclimate-0.85.27 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.85.26 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.85.25 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-0.85.24 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.54 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.53 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.52 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.51 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.50 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.49 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.48 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.47 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.46 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.45 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.44 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.43 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.42 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.41 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.40 lib/cc/workspace/path_tree/dir_node.rb
codeclimate-fede-0.85.39 lib/cc/workspace/path_tree/dir_node.rb