hike 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hike.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module Hike
2
2
  VERSION = "0.5.1"
3
3
 
4
- autoload :DirectoryIndex, "hike/directory_index"
5
4
  autoload :Extensions, "hike/extensions"
6
5
  autoload :NormalizedArray, "hike/normalized_array"
7
6
  autoload :Paths, "hike/paths"
@@ -1,3 +1,5 @@
1
+ require 'hike/normalized_array'
2
+
1
3
  module Hike
2
4
  class Extensions < NormalizedArray
3
5
  def normalize_element(extension)
data/lib/hike/paths.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pathname'
2
+ require 'hike/normalized_array'
2
3
 
3
4
  module Hike
4
5
  class Paths < NormalizedArray
data/lib/hike/trail.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'pathname'
2
+ require 'hike/extensions'
3
+ require 'hike/paths'
2
4
 
3
5
  module Hike
4
6
  class Trail
@@ -21,7 +23,8 @@ module Hike
21
23
 
22
24
  options[:directories] ||= false
23
25
 
24
- options[:index_cache] = DirectoryIndex.new
26
+ options[:stat_cache] = {}
27
+ options[:entries_cache] = {}
25
28
  options[:patterns_cache] = {}
26
29
 
27
30
  logical_paths.each do |logical_path|
@@ -63,29 +66,53 @@ module Hike
63
66
  end
64
67
 
65
68
  def find_in_base_path(logical_path, base_path, options, &block)
66
- candidate = base_path.join(logical_path).expand_path
69
+ candidate = base_path.join(logical_path)
67
70
  dirname, basename = candidate.split
68
71
  match(dirname, basename, options, &block) if paths_contain?(dirname)
69
72
  end
70
73
 
71
74
  def match(dirname, basename, options)
72
- index = options[:index_cache]
73
- pattern = pattern_for(basename, options)
75
+ matches = entries(options[:entries_cache], dirname)
74
76
 
75
- matches = options[:directories] ? index.entries(dirname) : index.files(dirname)
77
+ pattern = pattern_for(options[:patterns_cache], basename)
76
78
  matches = matches.select { |m| m.to_s =~ pattern }
77
79
 
80
+ cache = options[:stat_cache]
78
81
  sort_matches(matches, basename).each do |path|
79
- yield dirname.join(path).expand_path.to_s
82
+ pathname = dirname.join(path)
83
+
84
+ if options[:directories]
85
+ yield pathname.to_s
86
+ elsif (stat = self.stat(cache, pathname)) && stat.file?
87
+ yield pathname.to_s
88
+ end
89
+ end
90
+ end
91
+
92
+ def stat(cache, pathname)
93
+ if cache.key?(pathname)
94
+ cache[pathname]
95
+ else
96
+ begin
97
+ cache[pathname] = pathname.stat
98
+ rescue Errno::ENOENT
99
+ cache[pathname] = nil
100
+ end
80
101
  end
81
102
  end
82
103
 
104
+ def entries(cache, pathname)
105
+ cache[pathname] ||= pathname.entries.reject { |entry| entry.to_s =~ /^\.\.?$/ }
106
+ rescue Errno::ENOENT
107
+ cache[pathname] = []
108
+ end
109
+
83
110
  def paths_contain?(dirname)
84
111
  paths.any? { |path| dirname.to_s[0, path.to_s.length] == path }
85
112
  end
86
113
 
87
- def pattern_for(basename, options)
88
- options[:patterns_cache][basename] ||= begin
114
+ def pattern_for(cache, basename)
115
+ cache[basename] ||= begin
89
116
  extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
90
117
  /^#{Regexp.escape(basename.to_s)}(?:#{extension_pattern}|)+$/
91
118
  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: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 1
10
- version: 0.5.1
8
+ - 6
9
+ - 0
10
+ version: 0.6.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: 2011-03-21 00:00:00 -05:00
18
+ date: 2011-03-30 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,7 +29,6 @@ extensions: []
29
29
  extra_rdoc_files: []
30
30
 
31
31
  files:
32
- - lib/hike/directory_index.rb
33
32
  - lib/hike/extensions.rb
34
33
  - lib/hike/normalized_array.rb
35
34
  - lib/hike/paths.rb
@@ -1,33 +0,0 @@
1
- require 'pathname'
2
-
3
- module Hike
4
- class DirectoryIndex
5
- def initialize
6
- expire_cache
7
- end
8
-
9
- def expire_cache
10
- @entries = {}
11
- @files = {}
12
- true
13
- end
14
-
15
- def entries(dirname)
16
- dirname = Pathname.new(dirname).expand_path
17
- @entries[dirname] ||= if dirname.directory?
18
- dirname.entries.reject do |entry|
19
- entry.to_s =~ /^\.\.?$/
20
- end.sort
21
- else
22
- []
23
- end
24
- end
25
-
26
- def files(dirname)
27
- dirname = Pathname.new(dirname).expand_path
28
- @files[dirname] ||= entries(dirname).select do |entry|
29
- dirname.join(entry).file?
30
- end.map(&:to_s)
31
- end
32
- end
33
- end