Sha256: a8de3ea35895486e59f93c30e1c74e597ecf8b2b5391da37d13daf269cb957ab

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 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? || (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

10 entries across 10 versions & 1 rubygems

Version Path
rake-funnel-0.22.2 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.22.1 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.22.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.21.2 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.21.1 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.21.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.20.2 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.20.1 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.20.0 lib/rake/funnel/support/internal/finder.rb
rake-funnel-0.19.0 lib/rake/funnel/support/internal/finder.rb