Sha256: d7df110710c56fbd633a1260261c8ecf747592c1ea60c306cba01a37a9113e81

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module Hike
  class Trail
    attr_reader :root, :paths, :extensions

    def initialize(root)
      @root = File.expand_path(root)
      @index = DirectoryIndex.new
      @paths = Paths.new(@root)
      @extensions = Extensions.new
    end

    def find(*logical_paths)
      @index.expire_mtimes
      candidates = candidates_for_paths(logical_paths)

      paths.each do |path|
        candidates.each do |candidate|
          filename = File.join(path, candidate)
          return filename if exists?(filename)
        end
      end

      nil
    end

    protected
      def candidates_for_paths(logical_paths)
        logical_paths.map do |logical_path|
          candidates_for_path(logical_path)
        end.flatten
      end

      def candidates_for_path(logical_path)
        candidates = extensions.map { |ext| logical_path + ext }
        candidates.unshift(logical_path) if has_extension?(logical_path)
        candidates
      end

      def has_extension?(logical_path)
        extensions.include?(File.extname(logical_path))
      end

      def exists?(path)
        dirname, basename = File.dirname(path), File.basename(path)
        @index.files(dirname).include?(basename)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hike-0.1.1 lib/hike/trail.rb