hike 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hike.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Hike
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
 
4
+ autoload :DirectoryIndex, "hike/directory_index"
4
5
  autoload :Extensions, "hike/extensions"
5
6
  autoload :NormalizedArray, "hike/normalized_array"
6
7
  autoload :Paths, "hike/paths"
@@ -0,0 +1,44 @@
1
+ module Hike
2
+ class DirectoryIndex
3
+ def initialize
4
+ expire
5
+ end
6
+
7
+ def expire
8
+ expire_mtimes
9
+ expire_files
10
+ end
11
+
12
+ def expire_mtimes
13
+ @mtimes = {}
14
+ true
15
+ end
16
+
17
+ def expire_files
18
+ @files = {}
19
+ true
20
+ end
21
+
22
+ def mtime(dirname)
23
+ @mtimes[dirname] ||= File.directory?(dirname) && File.mtime(dirname)
24
+ end
25
+
26
+ def files(dirname)
27
+ if current_mtime = mtime(dirname)
28
+ cached_mtime, files = @files[dirname]
29
+ if current_mtime == cached_mtime
30
+ return files
31
+ else
32
+ files = Dir.entries(dirname).select do |entry|
33
+ File.file?(File.join(dirname, entry))
34
+ end
35
+ end
36
+ else
37
+ files = []
38
+ end
39
+
40
+ @files[dirname] = [current_mtime, files]
41
+ files
42
+ end
43
+ end
44
+ end
data/lib/hike/trail.rb CHANGED
@@ -4,12 +4,14 @@ module Hike
4
4
 
5
5
  def initialize(root)
6
6
  @root = File.expand_path(root)
7
+ @index = DirectoryIndex.new
7
8
  @paths = Paths.new(@root)
8
9
  @extensions = Extensions.new
9
10
  end
10
11
 
11
- def find(logical_path)
12
- candidates = candidates_for(logical_path)
12
+ def find(*logical_paths)
13
+ @index.expire_mtimes
14
+ candidates = candidates_for_paths(logical_paths)
13
15
 
14
16
  paths.each do |path|
15
17
  candidates.each do |candidate|
@@ -22,7 +24,13 @@ module Hike
22
24
  end
23
25
 
24
26
  protected
25
- def candidates_for(logical_path)
27
+ def candidates_for_paths(logical_paths)
28
+ logical_paths.map do |logical_path|
29
+ candidates_for_path(logical_path)
30
+ end.flatten
31
+ end
32
+
33
+ def candidates_for_path(logical_path)
26
34
  candidates = extensions.map { |ext| logical_path + ext }
27
35
  candidates.unshift(logical_path) if has_extension?(logical_path)
28
36
  candidates
@@ -33,7 +41,8 @@ module Hike
33
41
  end
34
42
 
35
43
  def exists?(path)
36
- File.exists?(path)
44
+ dirname, basename = File.dirname(path), File.basename(path)
45
+ @index.files(dirname).include?(basename)
37
46
  end
38
47
  end
39
48
  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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Stephenson
@@ -29,6 +29,7 @@ extensions: []
29
29
  extra_rdoc_files: []
30
30
 
31
31
  files:
32
+ - lib/hike/directory_index.rb
32
33
  - lib/hike/extensions.rb
33
34
  - lib/hike/normalized_array.rb
34
35
  - lib/hike/paths.rb