Sha256: 78e1963e9180da16105bed6ac331694ffee2b3e7365513d33dfd0ec352e6e3f7

Contents?: true

Size: 1.42 KB

Versions: 81

Compression:

Stored size: 1.42 KB

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

      # Returns the root for the filesystem we are operating on.  Ignores
      # mount boundries on *nix.
      #
      # For all flavors of *nix this should always return "/".
      #
      # Windows should expect something similar to "C:\".
      #
      # @return [String] The root path of the file-system.  For unix this should
      #   always be "/".  For windows this should be something like "C:\".
      def root
        Dir.pwd.split(File::SEPARATOR).first
      end

    end

  end

end

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
ztk-3.1.0 lib/ztk/locator.rb
ztk-3.0.4 lib/ztk/locator.rb
ztk-3.0.3 lib/ztk/locator.rb
ztk-3.0.2 lib/ztk/locator.rb
ztk-3.0.1 lib/ztk/locator.rb
ztk-3.0.0 lib/ztk/locator.rb
ztk-2.4.2 lib/ztk/locator.rb
ztk-2.4.1 lib/ztk/locator.rb
ztk-2.4.0 lib/ztk/locator.rb
ztk-2.3.1 lib/ztk/locator.rb
ztk-2.3.0 lib/ztk/locator.rb
ztk-2.2.0 lib/ztk/locator.rb
ztk-2.1.1 lib/ztk/locator.rb
ztk-2.1.0 lib/ztk/locator.rb
ztk-2.0.1 lib/ztk/locator.rb
ztk-2.0.0 lib/ztk/locator.rb
ztk-1.19.2 lib/ztk/locator.rb
ztk-1.19.1 lib/ztk/locator.rb
ztk-1.19.0 lib/ztk/locator.rb
ztk-1.18.5 lib/ztk/locator.rb