Sha256: a1c8b79e8623af9bb2242cfc471c2492c8aa6af84cb0639159aeafed983dbaee

Contents?: true

Size: 907 Bytes

Versions: 6

Compression:

Stored size: 907 Bytes

Contents

require 'pathname'

module Soloist
  class NotFound < RuntimeError; end

  class Spotlight
    attr_reader :pathname

    def self.find(*file_names)
      new(Dir.pwd).find(*file_names)
    end

    def self.find!(*file_names)
      new(Dir.pwd).find!(*file_names)
    end

    def initialize(path)
      @pathname = Pathname.new(path)
    end

    def find(*file_names)
      pathname.ascend do |path|
        file_name = file_names.detect { |fn| path.join(fn).file? }
        break path.join(file_name) if file_name
      end
    end

    def find!(*file_names)
      file_path = find(*file_names)
      unless file_path
        file_names = if file_names.length > 2
          file_names[0...-1].join(", ") + " or " + file_names.last
        else
          file_names.join(" or ")
        end
        raise Soloist::NotFound.new("Could not find #{file_names}")
      end
      file_path
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
soloist-1.0.3 lib/soloist/spotlight.rb
soloist-1.0.2 lib/soloist/spotlight.rb
soloist-1.0.1 lib/soloist/spotlight.rb
soloist-rvm-0.0.1 lib/soloist/spotlight.rb
soloist-1.0.0 lib/soloist/spotlight.rb
soloist-1.0.0.pre lib/soloist/spotlight.rb