Sha256: 608a745d6c9c980b46c19c053e4ff59a6954c113190508d131f12bb37108cc1a

Contents?: true

Size: 897 Bytes

Versions: 1

Compression:

Stored size: 897 Bytes

Contents

class Gitdocs::Search
  RepoDescriptor = Struct.new(:name, :index)
  SearchResult   = Struct.new(:file, :context)

  # @param [Array<Gitdocs::Repository>] repositories
  def initialize(repositories)
    @repositories = repositories
  end

  def search(term)
    results = {}
    @repositories.each_with_index do |repository, index|
      descriptor = RepoDescriptor.new(repository.root, index)
      results[descriptor] = search_repository(repository, term)
    end
    results.delete_if { |_key, value| value.empty? }
  end

  private

  def search_repository(repository, term)
    return [] if term.nil? || term.empty?

    results = []
    repository.grep(term) do |file, context|
      result = results.find { |s| s.file == file }
      if result
        result.context += ' ... ' + context
      else
        results << SearchResult.new(file, context)
      end
    end
    results
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gitdocs-0.5.0 lib/gitdocs/search.rb