hike 0.3.0 → 0.4.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 +25 -35
  3. metadata +6 -6
data/lib/hike.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Hike
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
 
4
4
  autoload :DirectoryIndex, "hike/directory_index"
5
5
  autoload :Extensions, "hike/extensions"
data/lib/hike/trail.rb CHANGED
@@ -9,16 +9,24 @@ module Hike
9
9
  @extensions = Extensions.new
10
10
  end
11
11
 
12
- def find(*logical_paths)
13
- options = extract_options!(logical_paths)
14
- base_path = options[:base_path] || root
15
- reset!
12
+ def find(*logical_paths, &block)
13
+ if block_given?
14
+ options = extract_options!(logical_paths)
15
+ base_path = options[:base_path] || root
16
+ reset!
16
17
 
17
- searching(logical_paths) do |logical_path|
18
- if relative?(logical_path)
19
- find_in_base_path(logical_path, base_path)
20
- else
21
- find_in_paths(logical_path)
18
+ logical_paths.each do |logical_path|
19
+ if relative?(logical_path)
20
+ find_in_base_path(logical_path, base_path, &block)
21
+ else
22
+ find_in_paths(logical_path, &block)
23
+ end
24
+ end
25
+
26
+ nil
27
+ else
28
+ find(*logical_paths) do |path|
29
+ return path
22
30
  end
23
31
  end
24
32
  end
@@ -37,24 +45,23 @@ module Hike
37
45
  logical_path =~ /^\.\.?\//
38
46
  end
39
47
 
40
- def find_in_paths(logical_path)
48
+ def find_in_paths(logical_path, &block)
41
49
  dirname, basename = File.split(logical_path)
42
- searching(paths) do |base_path|
43
- match(File.join(base_path, dirname), basename)
50
+ paths.each do |base_path|
51
+ match(File.join(base_path, dirname), basename, &block)
44
52
  end
45
53
  end
46
54
 
47
- def find_in_base_path(logical_path, base_path)
55
+ def find_in_base_path(logical_path, base_path, &block)
48
56
  candidate = File.expand_path(File.join(base_path, logical_path))
49
57
  dirname, basename = File.split(candidate)
50
- match(dirname, basename) if paths_contain?(dirname)
58
+ match(dirname, basename, &block) if paths_contain?(dirname)
51
59
  end
52
60
 
53
61
  def match(dirname, basename)
54
62
  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))
63
+ sort_matches(matches, basename).each do |path|
64
+ yield File.expand_path(File.join(dirname, path))
58
65
  end
59
66
  end
60
67
 
@@ -69,28 +76,11 @@ module Hike
69
76
  end
70
77
  end
71
78
 
72
- def select_match_from(matches, basename)
73
- if matches.length == 1
74
- matches.first
75
- elsif matches.length > 1
76
- select_ordered_match_from(matches, basename)
77
- end
78
- end
79
-
80
- def select_ordered_match_from(matches, basename)
79
+ def sort_matches(matches, basename)
81
80
  matches.sort_by do |match|
82
81
  extnames = match[basename.length..-1].scan(/.[^.]+/)
83
82
  extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 }
84
- end.first
85
- end
86
-
87
- def searching(collection)
88
- collection.each do |value|
89
- if result = yield(value)
90
- return result
91
- end
92
83
  end
93
- nil
94
84
  end
95
85
  end
96
86
  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: 19
5
- prerelease: false
4
+ hash: 15
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.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-25 00:00:00 -06:00
18
+ date: 2011-02-04 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  requirements: []
66
66
 
67
67
  rubyforge_project:
68
- rubygems_version: 1.3.7
68
+ rubygems_version: 1.5.0
69
69
  signing_key:
70
70
  specification_version: 3
71
71
  summary: Find files in a set of paths