Sha256: c811d89ed1dd249805a94f2a87b09e772d044256c410f1b66eb6ad7a04aef936

Contents?: true

Size: 1.14 KB

Versions: 18

Compression:

Stored size: 1.14 KB

Contents

module Rake
  module Funnel
    module 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
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rake-funnel-0.18.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.17.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.16.1 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.16.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.15.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.14.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.13.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.12.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.11.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.10.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.9.1.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.9.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.8.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.7.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.6.1.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.6.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.5.0.pre lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.4.0.pre lib/rake/funnel/support/internal/finder.rb