Sha256: facdb91f1ac1f1a26b0fed0e2539430363bfb660c814b4ee0191b438b41ff5d1

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'git_dump/path_object'
require 'git_dump/entry'

class GitDump
  class Tree < PathObject
    # Common methods in Tree and Builder
    module Base
      # Retrive tree or entry at path, return nil if there is nothing at path
      def [](path)
        get_at(parse_path(path))
      end

      # Iterate over every tree/entry of this tree, return enumerator if no
      # block given
      def each(&block)
        return to_enum(:each) unless block

        @entries.each_value do |entry|
          block[entry]
        end
      end

      # Iterate over all entries recursively, return enumerator if no block
      # given
      def each_recursive(&block)
        return to_enum(:each_recursive) unless block

        @entries.each_value do |entry|
          if entry.is_a?(Entry)
            block[entry]
          else
            entry.each_recursive(&block)
          end
        end
      end

    protected

      def get_at(parts)
        return unless (entry = @entries[parts.first])

        if parts.length == 1
          entry
        elsif entry.is_a?(self.class)
          entry.get_at(parts.drop(1))
        end
      end

    private

      def parse_path(path)
        path = Array(path).join('/') unless path.is_a?(String)
        path.scan(%r{[^/]+})
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_dump-0.1.1 lib/git_dump/tree/base.rb