Sha256: 7fa3d98292d346d291dcc772e6ae5c4829292ba4010e5a98a5dc53d0135e916f

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Owners
  # Accepts an array of file paths and returns an array of
  # owners that have subscribed to the specified files.
  #
  # @api private
  class Search
    def initialize(files, configs = nil)
      @files = files.map(&:dup)
      @configs = configs
    end

    def owners
      search do |(path, config), results|
        owners = config.owners(path.to_s)
        results.merge(owners) if owners
      end
    end

    private

    def search(&block)
      attempts.each_with_object(SortedSet.new, &block).to_a
    end

    def attempts
      paths.product(configs)
    end

    def configs
      if @configs
        @configs.map { |file, contents| Config.new(file, contents) }
      else
        subscriptions.map { |file| Config.new(file) }
      end
    end

    def subscriptions
      trees.flat_map(&:owner_files).uniq
    end

    def trees
      paths.map { |path| Tree.new(path) }
    end

    def paths
      @files.map do |file|
        file.prepend("./") unless file =~ /^\.?\//
        Pathname.new(file)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
owners-0.0.8 lib/owners/search.rb
owners-0.0.7 lib/owners/search.rb