Sha256: 57029b59fd9dc91e9ed3ea93cf52d34e158cac9b36ea047584ac78578f4f2ac4

Contents?: true

Size: 980 Bytes

Versions: 27

Compression:

Stored size: 980 Bytes

Contents

module ZTK

  # Locator Error Class
  #
  # @author Zachary Patten <zachary AT jovelabs DOT com>
  class LocatorError < Error; end

  # @author Zachary Patten <zachary AT jovelabs DOT com>
  class Locator

    class << self

      # Locate a file or directory
      #
      # Attempts to locate the file or directory supplied, starting with
      # the current working directory and crawling it up looking for a match
      # at each step of the way.
      #
      # @param [String,Array<String>] args A string or array of strings to
      #   attempt to locate.
      #
      # @return [String] The expanded path to the located entry.
      def find(*args)
        pwd = Dir.pwd.split(File::SEPARATOR)

        (pwd.length - 1).downto(0) do |i|
          candidate = File.expand_path(File.join(pwd[0..i], args))
          return candidate if File.exists?(candidate)
        end

        raise LocatorError, "Could not locate '#{File.join(args)}'!"
      end

    end

  end

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ztk-1.6.2 lib/ztk/locator.rb
ztk-1.6.1 lib/ztk/locator.rb
ztk-1.6.0 lib/ztk/locator.rb
ztk-1.5.5 lib/ztk/locator.rb
ztk-1.5.4 lib/ztk/locator.rb
ztk-1.5.3 lib/ztk/locator.rb
ztk-1.5.2 lib/ztk/locator.rb
ztk-1.5.1 lib/ztk/locator.rb
ztk-1.5.0 lib/ztk/locator.rb
ztk-1.4.22 lib/ztk/locator.rb
ztk-1.4.21 lib/ztk/locator.rb
ztk-1.4.20 lib/ztk/locator.rb
ztk-1.4.19 lib/ztk/locator.rb
ztk-1.4.18 lib/ztk/locator.rb
ztk-1.4.17 lib/ztk/locator.rb
ztk-1.4.16 lib/ztk/locator.rb
ztk-1.4.15 lib/ztk/locator.rb
ztk-1.4.14 lib/ztk/locator.rb
ztk-1.4.13 lib/ztk/locator.rb
ztk-1.4.12 lib/ztk/locator.rb