hike 0.2.0 → 0.3.0

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 +51 -41
  3. metadata +4 -4
@@ -1,5 +1,5 @@
1
1
  module Hike
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
 
4
4
  autoload :DirectoryIndex, "hike/directory_index"
5
5
  autoload :Extensions, "hike/extensions"
@@ -10,77 +10,87 @@ module Hike
10
10
  end
11
11
 
12
12
  def find(*logical_paths)
13
- options = logical_paths.last.is_a?(Hash) ? logical_paths.pop : {}
14
- if relative_to = options[:relative_to]
15
- base_path = File.dirname(relative_to)
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.each do |logical_path|
21
- if relative_to
22
- result = find_path_relative(logical_path, base_path)
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
- result = find_path(logical_path)
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
- attr_reader :index
34
-
35
- def find_path(logical_path)
36
- dirname, basename = File.split(logical_path)
37
- pattern = filename_pattern_for(basename)
27
+ def reset!
28
+ @index.expire_cache
29
+ @patterns = {}
30
+ end
38
31
 
39
- paths.each do |base_path|
40
- if path = find_in_base(File.join(base_path, dirname), basename, pattern)
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 find_path_relative(logical_path, base_path)
48
- dirname, basename = File.split(File.join(base_path, logical_path))
49
- dirname = File.expand_path(dirname)
50
- pattern = filename_pattern_for(basename)
36
+ def relative?(logical_path)
37
+ logical_path =~ /^\.\.?\//
38
+ end
51
39
 
52
- if paths.any? { |path| dirname[0, path.length] == path }
53
- find_in_base(File.expand_path(dirname), basename, pattern)
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 find_in_base(base_path, base_name, pattern)
58
- matches = match_files_in(base_path, pattern)
59
- File.join(base_path, match_from(matches, base_name)) unless matches.empty?
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 match_files_in(dirname, pattern)
63
- index.files(dirname).grep(pattern)
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 filename_pattern_for(basename)
67
- extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
68
- /^#{Regexp.escape(basename)}(?:#{extension_pattern}|)+$/
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 match_from(matches, basename)
72
+ def select_match_from(matches, basename)
72
73
  if matches.length == 1
73
74
  matches.first
74
75
  elsif matches.length > 1
75
- ordered_match_from(matches, basename)
76
+ select_ordered_match_from(matches, basename)
76
77
  end
77
78
  end
78
79
 
79
- def ordered_match_from(matches, basename)
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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.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-23 00:00:00 -06:00
18
+ date: 2010-11-25 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21