Sha256: 99d716467a593045465b478a98327fa93e3b028a9b225dd233129e14494a0d8f

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

module Rake::Funnel::Support
  class Finder
    include Enumerable

    def initialize(search_pattern, task, message = nil)
      @search_pattern = search_pattern
      @task = task
      @message = message
    end

    def each
      block_given? or return enum_for(__method__)
      all_or_default.each { |x| yield x }
    end

    def single_or_default
      first_sln
    end

    def single
      if first_sln.nil?
        raise Rake::Funnel::AmbiguousFileError.new(@message, @task.name, @search_pattern, candidates)
      end

      first_sln
    end

    def all_or_default
      candidates
    end

    def all
      if candidates.empty?
        raise Rake::Funnel::AmbiguousFileError.new(@message, @task.name, @search_pattern, candidates)
      end

      candidates
    end

    private
    def first_sln
      return candidates.first if candidates.one?

      nil
    end

    def candidates
      Dir[*@search_pattern.dup].select { |f| File.file?(f) }.uniq
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rake-funnel-0.3.2.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.3.1.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.3.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.2.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.1.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.0.6.pre lib/rake/funnel/support/finder.rb
rake-funnel-0.0.5.pre lib/rake/funnel/support/finder.rb
rake-funnel-0.0.4.pre lib/rake/funnel/support/finder.rb