00000040
  7   ˆl–Ü4ý( ©¤ýÔâ�jàAq°)‹Fÿ‘äÇò 8àùdyá2 ?Âƒh¹ÁÀ¿      00000008
      ˆ¾‘äÇò 8àùdyá2 ?Âƒh¹ÁÀ¿      1
      ˆ¾‘äÇò 8àùdyçÀàŸÂƒh¹ÁÀ¿      512
      	ˆ¾‘äÇò 8àùdyù]#?ÏÂƒh¹ÁÀ¿      	1
  E   ˆl–Ðz¾”Ê_)ÕÀ†ã-)‹Fÿ�þBó¢Y¸ÝyÉ•çÂð?ŸÃƒpM½Â_�uÐb&=LyZËcõÂÁ r    require 'pathname'

module Hike
  # `Index` is an internal cached variant of `Trail`. It assumes the
  # file system does not change between `find` calls. All `stat` and
  # `entries` calls are cached for the lifetime of the `Index` object.
  class Index
    # `Index#paths` is an immutable `Paths` collection.
    attr_reader :paths

    # `Index#extensions` is an immutable `Extensions` collection.
    attr_reader :extensions

    # `Index#aliases` is an immutable `Hash` mapping an extension to
    # an `Array` of aliases.
    attr_reader :aliases

    # `Index.new` is an internal method. Instead of constructing it
    # directly, create a `Trail` and call `Trail#index`.
    def initialize(root, paths, extensions, aliases)
      @root = root

      # Freeze is used here so an error is throw if a mutator method
      # is called on the array. Mutating `@paths`, `@extensions`, or
      # `@aliases` would have unpredictable results.
      @paths      = paths.dup.freeze
      @extensions = extensions.dup.freeze
      @aliases    = aliases.inject({}) { |h, (k, a)|
                      h[k] = a.dup.freeze; h
                   }.freeze
      @pathnames  = paths.map { |path| Pathname.new(path) }

      @stats    = {}
      @entries  = {}
      @patterns = {}
    end

    # `Index#root` returns root path as a `String`. This attribute is immutable.
    def root
      @root.to_s
    end

    # `Index#index` returns `self` to be compatable with the `Trail` interface.
    def index
      self
    end

    # The real implementation of `find`. `Trail#find` generates a one
    # time index and delegates here.
    #
    # See `Trail#find` for usage.
    def find(*logical_paths, &block)
      if block_given?
        options = extract_options!(logical_paths)
        base_path = Pathname.new(options[:base_path] || @root)

        logical_paths.each do |logical_path|
          logical_path = Pathname.new(logical_path.sub(/^\//, ''))

          if relative?(logical_path)
            find_in_base_path(logical_path, base_path, &block)
          else
            find_in_paths(logical_path, &block)
          end
        end

        nil
      else
        find(*logical_paths) do |path|
          return path
        end
      end
    end

    # A cached version of `Dir.entries` that filters out `.` files and
    # `~` swap files. Returns an empty `Array` if the directory does
    # not exist.
    def entries(path)
      key = path.to_s
      @entries[key] ||= Pathname.new(path).entries.reject { |entry| entry.to_s =~ /^\.|~$|^\#.*\#$/ }.sort
    rescue Errno::ENOENT
      @entries[key] = []
    end

    # A cached version of `File.stat`. Returns nil if the file does
    # not exist.
    def stat(path)
      key = path.to_s
      if @stats.key?(key)
        @stats[key]
      else
        begin
          @stats[key] = File.stat(path)
        rescue Errno::ENOENT
          @stats[key] = nil
        end
      end
    end

    protected
      def extract_options!(arguments)
        arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
      end

      def relative?(logical_path)
        logical_path.to_s =~ /^\.\.?\//
      end

      # Finds logical path across all `paths`
      def find_in_paths(logical_path, &block)
        dirname, basename = logical_path.split
        @pathnames.each do |base_path|
          match(base_path.join(dirname), basename, &block)
        end
      end

      # Finds relative logical path, `../test/test_trail`. Requires a
      # `base_path` for reference.
      def find_in_base_path(logical_path, base_path, &block)
        candidate = base_path.join(logical_path)
        dirname, basename = candidate.split
        match(dirname, basename, &block) if paths_contain?(dirname)
      end

      # C