hike 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/hike.rb +1 -1
  2. data/lib/hike/trail.rb +40 -20
  3. metadata +4 -4
data/lib/hike.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Hike
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
 
4
4
  autoload :DirectoryIndex, "hike/directory_index"
5
5
  autoload :Extensions, "hike/extensions"
data/lib/hike/trail.rb CHANGED
@@ -10,39 +10,59 @@ module Hike
10
10
  end
11
11
 
12
12
  def find(*logical_paths)
13
- @index.expire_mtimes
14
- candidates = candidates_for_paths(logical_paths)
13
+ index.expire_mtimes
15
14
 
16
- paths.each do |path|
17
- candidates.each do |candidate|
18
- filename = File.join(path, candidate)
19
- return filename if exists?(filename)
15
+ logical_paths.each do |logical_path|
16
+ if result = find_path(logical_path)
17
+ return result
20
18
  end
21
19
  end
22
-
23
20
  nil
24
21
  end
25
22
 
26
23
  protected
27
- def candidates_for_paths(logical_paths)
28
- logical_paths.map do |logical_path|
29
- candidates_for_path(logical_path)
30
- end.flatten
24
+ attr_reader :index
25
+
26
+ def find_path(logical_path)
27
+ dirname, basename = File.split(logical_path)
28
+ pattern = filename_pattern_for(basename)
29
+
30
+ paths.each do |root|
31
+ path = File.join(root, dirname)
32
+ matches = match_files_in(path, pattern)
33
+ return File.join(path, match_from(matches, basename)) unless matches.empty?
34
+ end
35
+ nil
31
36
  end
32
37
 
33
- def candidates_for_path(logical_path)
34
- candidates = extensions.map { |ext| logical_path + ext }
35
- candidates.unshift(logical_path) if has_extension?(logical_path)
36
- candidates
38
+ def match_files_in(dirname, pattern)
39
+ index.files(dirname).grep(pattern)
37
40
  end
38
41
 
39
- def has_extension?(logical_path)
40
- extensions.include?(File.extname(logical_path))
42
+ def filename_pattern_for(basename)
43
+ extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
44
+ extension_pattern += "|" if has_registered_extension?(basename)
45
+ /^#{Regexp.escape(basename)}(?:#{extension_pattern})$/
46
+ end
47
+
48
+ def match_from(matches, basename)
49
+ if matches.length == 1
50
+ matches.first
51
+ elsif matches.length > 1
52
+ ordered_match_from(matches, basename)
53
+ end
54
+ end
55
+
56
+ def ordered_match_from(matches, basename)
57
+ extensions.each do |extension|
58
+ candidate = basename + extension
59
+ return candidate if matches.include?(candidate)
60
+ end
61
+ basename
41
62
  end
42
63
 
43
- def exists?(path)
44
- dirname, basename = File.dirname(path), File.basename(path)
45
- @index.files(dirname).include?(basename)
64
+ def has_registered_extension?(logical_path)
65
+ extensions.include?(File.extname(logical_path))
46
66
  end
47
67
  end
48
68
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hike
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Stephenson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-16 00:00:00 -05:00
18
+ date: 2010-10-19 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21