Sha256: e6709d6682a755eca4ef11026374a1fb8282a02208eca15673b5f0d56c6780ec
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 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_cache logical_paths.each do |logical_path| if result = find_path(logical_path) return File.expand_path(result) end end nil end protected attr_reader :index def find_path(logical_path) dirname, basename = File.split(logical_path) pattern = filename_pattern_for(basename) paths.each do |root| path = File.join(root, dirname) matches = match_files_in(path, pattern) return File.join(path, match_from(matches, basename)) unless matches.empty? end nil end def match_files_in(dirname, pattern) index.files(dirname).grep(pattern) end def filename_pattern_for(basename) extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|") /^#{Regexp.escape(basename)}(?:#{extension_pattern}|)+$/ end def match_from(matches, basename) if matches.length == 1 matches.first elsif matches.length > 1 ordered_match_from(matches, basename) end end def ordered_match_from(matches, basename) matches.sort_by do |match| extnames = match[basename.length..-1].scan(/.[^.]+/) extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 } end.first end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hike-0.1.5 | lib/hike/trail.rb |