hike 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hike.rb +1 -1
- data/lib/hike/trail.rb +51 -41
- metadata +4 -4
data/lib/hike.rb
CHANGED
data/lib/hike/trail.rb
CHANGED
@@ -10,77 +10,87 @@ module Hike
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def find(*logical_paths)
|
13
|
-
options = logical_paths
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
index.expire_cache
|
13
|
+
options = extract_options!(logical_paths)
|
14
|
+
base_path = options[:base_path] || root
|
15
|
+
reset!
|
19
16
|
|
20
|
-
logical_paths
|
21
|
-
if
|
22
|
-
|
17
|
+
searching(logical_paths) do |logical_path|
|
18
|
+
if relative?(logical_path)
|
19
|
+
find_in_base_path(logical_path, base_path)
|
23
20
|
else
|
24
|
-
|
21
|
+
find_in_paths(logical_path)
|
25
22
|
end
|
26
|
-
|
27
|
-
return File.expand_path(result) if result
|
28
23
|
end
|
29
|
-
nil
|
30
24
|
end
|
31
25
|
|
32
26
|
protected
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
pattern = filename_pattern_for(basename)
|
27
|
+
def reset!
|
28
|
+
@index.expire_cache
|
29
|
+
@patterns = {}
|
30
|
+
end
|
38
31
|
|
39
|
-
|
40
|
-
|
41
|
-
return path
|
42
|
-
end
|
43
|
-
end
|
44
|
-
nil
|
32
|
+
def extract_options!(arguments)
|
33
|
+
arguments.last.is_a?(Hash) ? arguments.pop : {}
|
45
34
|
end
|
46
35
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
pattern = filename_pattern_for(basename)
|
36
|
+
def relative?(logical_path)
|
37
|
+
logical_path =~ /^\.\.?\//
|
38
|
+
end
|
51
39
|
|
52
|
-
|
53
|
-
|
40
|
+
def find_in_paths(logical_path)
|
41
|
+
dirname, basename = File.split(logical_path)
|
42
|
+
searching(paths) do |base_path|
|
43
|
+
match(File.join(base_path, dirname), basename)
|
54
44
|
end
|
55
45
|
end
|
56
46
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
47
|
+
def find_in_base_path(logical_path, base_path)
|
48
|
+
candidate = File.expand_path(File.join(base_path, logical_path))
|
49
|
+
dirname, basename = File.split(candidate)
|
50
|
+
match(dirname, basename) if paths_contain?(dirname)
|
60
51
|
end
|
61
52
|
|
62
|
-
def
|
63
|
-
index.files(dirname).grep(
|
53
|
+
def match(dirname, basename)
|
54
|
+
matches = @index.files(dirname).grep(pattern_for(basename))
|
55
|
+
unless matches.empty?
|
56
|
+
path = select_match_from(matches, basename)
|
57
|
+
File.expand_path(File.join(dirname, path))
|
58
|
+
end
|
64
59
|
end
|
65
60
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
61
|
+
def paths_contain?(dirname)
|
62
|
+
paths.any? { |path| dirname[0, path.length] == path }
|
63
|
+
end
|
64
|
+
|
65
|
+
def pattern_for(basename)
|
66
|
+
@patterns[basename] ||= begin
|
67
|
+
extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
|
68
|
+
/^#{Regexp.escape(basename)}(?:#{extension_pattern}|)+$/
|
69
|
+
end
|
69
70
|
end
|
70
71
|
|
71
|
-
def
|
72
|
+
def select_match_from(matches, basename)
|
72
73
|
if matches.length == 1
|
73
74
|
matches.first
|
74
75
|
elsif matches.length > 1
|
75
|
-
|
76
|
+
select_ordered_match_from(matches, basename)
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
|
-
def
|
80
|
+
def select_ordered_match_from(matches, basename)
|
80
81
|
matches.sort_by do |match|
|
81
82
|
extnames = match[basename.length..-1].scan(/.[^.]+/)
|
82
83
|
extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 }
|
83
84
|
end.first
|
84
85
|
end
|
86
|
+
|
87
|
+
def searching(collection)
|
88
|
+
collection.each do |value|
|
89
|
+
if result = yield(value)
|
90
|
+
return result
|
91
|
+
end
|
92
|
+
end
|
93
|
+
nil
|
94
|
+
end
|
85
95
|
end
|
86
96
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
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-11-
|
18
|
+
date: 2010-11-25 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|