hike 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hike.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Hike
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
 
4
4
  autoload :DirectoryIndex, "hike/directory_index"
5
5
  autoload :Extensions, "hike/extensions"
@@ -1,3 +1,5 @@
1
+ require 'pathname'
2
+
1
3
  module Hike
2
4
  class DirectoryIndex
3
5
  def initialize
@@ -11,10 +13,10 @@ module Hike
11
13
  end
12
14
 
13
15
  def entries(dirname)
14
- dirname = File.expand_path(dirname)
15
- @entries[dirname] ||= if File.directory?(dirname)
16
- Dir.entries(dirname).reject do |entry|
17
- entry =~ /^\.\.?$/
16
+ dirname = Pathname.new(dirname).expand_path
17
+ @entries[dirname] ||= if dirname.directory?
18
+ dirname.entries.reject do |entry|
19
+ entry.to_s =~ /^\.\.?$/
18
20
  end.sort
19
21
  else
20
22
  []
@@ -22,10 +24,10 @@ module Hike
22
24
  end
23
25
 
24
26
  def files(dirname)
25
- dirname = File.expand_path(dirname)
27
+ dirname = Pathname.new(dirname).expand_path
26
28
  @files[dirname] ||= entries(dirname).select do |entry|
27
- File.file?(File.join(dirname, entry))
28
- end
29
+ dirname.join(entry).file?
30
+ end.map(&:to_s)
29
31
  end
30
32
  end
31
33
  end
data/lib/hike/paths.rb CHANGED
@@ -1,13 +1,16 @@
1
+ require 'pathname'
2
+
1
3
  module Hike
2
4
  class Paths < NormalizedArray
3
5
  def initialize(root = ".")
4
- @root = root
6
+ @root = Pathname.new(root)
5
7
  super()
6
8
  end
7
9
 
8
10
  def normalize_element(path)
9
- path = File.join(@root, path) unless path[/^\//]
10
- File.expand_path(path)
11
+ path = Pathname.new(path)
12
+ path = @root.join(path) if path.relative?
13
+ path.expand_path.to_s
11
14
  end
12
15
  end
13
16
  end
data/lib/hike/trail.rb CHANGED
@@ -1,25 +1,35 @@
1
+ require 'pathname'
2
+
1
3
  module Hike
2
4
  class Trail
3
- attr_reader :root, :paths, :extensions
5
+ attr_reader :paths, :extensions
4
6
 
5
7
  def initialize(root = ".")
6
- @root = File.expand_path(root)
7
- @index = DirectoryIndex.new
8
+ @root = Pathname.new(root).expand_path
8
9
  @paths = Paths.new(@root)
9
10
  @extensions = Extensions.new
10
11
  end
11
12
 
13
+ def root
14
+ @root.to_s
15
+ end
16
+
12
17
  def find(*logical_paths, &block)
13
18
  if block_given?
14
19
  options = extract_options!(logical_paths)
15
- base_path = options[:base_path] || root
16
- reset!
20
+ base_path = Pathname.new(options[:base_path] || @root)
21
+
22
+ options[:directories] ||= false
23
+
24
+ options[:index_cache] = DirectoryIndex.new
25
+ options[:patterns_cache] = {}
17
26
 
18
27
  logical_paths.each do |logical_path|
28
+ logical_path = Pathname.new(logical_path)
19
29
  if relative?(logical_path)
20
- find_in_base_path(logical_path, base_path, &block)
30
+ find_in_base_path(logical_path, base_path, options, &block)
21
31
  else
22
- find_in_paths(logical_path, &block)
32
+ find_in_paths(logical_path, options, &block)
23
33
  end
24
34
  end
25
35
 
@@ -32,53 +42,57 @@ module Hike
32
42
  end
33
43
 
34
44
  protected
35
- def reset!
36
- @index.expire_cache
37
- @patterns = {}
38
- end
39
-
40
45
  def extract_options!(arguments)
41
- arguments.last.is_a?(Hash) ? arguments.pop : {}
46
+ arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
42
47
  end
43
48
 
44
49
  def relative?(logical_path)
45
- logical_path =~ /^\.\.?\//
50
+ logical_path.to_s =~ /^\.\.?\//
51
+ end
52
+
53
+ def pathnames
54
+ paths.map { |path| Pathname.new(path) }
46
55
  end
47
56
 
48
- def find_in_paths(logical_path, &block)
49
- dirname, basename = File.split(logical_path)
50
- paths.each do |base_path|
51
- match(File.join(base_path, dirname), basename, &block)
57
+ def find_in_paths(logical_path, options, &block)
58
+ dirname, basename = logical_path.split
59
+ pathnames.each do |base_path|
60
+ match(base_path.join(dirname), basename, options, &block)
52
61
  end
53
62
  end
54
63
 
55
- def find_in_base_path(logical_path, base_path, &block)
56
- candidate = File.expand_path(File.join(base_path, logical_path))
57
- dirname, basename = File.split(candidate)
58
- match(dirname, basename, &block) if paths_contain?(dirname)
64
+ def find_in_base_path(logical_path, base_path, options, &block)
65
+ candidate = base_path.join(logical_path).expand_path
66
+ dirname, basename = candidate.split
67
+ match(dirname, basename, options, &block) if paths_contain?(dirname)
59
68
  end
60
69
 
61
- def match(dirname, basename)
62
- matches = @index.files(dirname).grep(pattern_for(basename))
70
+ def match(dirname, basename, options)
71
+ index = options[:index_cache]
72
+ pattern = pattern_for(basename, options)
73
+
74
+ matches = options[:directories] ? index.entries(dirname) : index.files(dirname)
75
+ matches = matches.select { |m| m.to_s =~ pattern }
76
+
63
77
  sort_matches(matches, basename).each do |path|
64
- yield File.expand_path(File.join(dirname, path))
78
+ yield dirname.join(path).expand_path.to_s
65
79
  end
66
80
  end
67
81
 
68
82
  def paths_contain?(dirname)
69
- paths.any? { |path| dirname[0, path.length] == path }
83
+ paths.any? { |path| dirname.to_s[0, path.to_s.length] == path }
70
84
  end
71
85
 
72
- def pattern_for(basename)
73
- @patterns[basename] ||= begin
86
+ def pattern_for(basename, options)
87
+ options[:patterns_cache][basename] ||= begin
74
88
  extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
75
- /^#{Regexp.escape(basename)}(?:#{extension_pattern}|)+$/
89
+ /^#{Regexp.escape(basename.to_s)}(?:#{extension_pattern}|)+$/
76
90
  end
77
91
  end
78
92
 
79
93
  def sort_matches(matches, basename)
80
94
  matches.sort_by do |match|
81
- extnames = match[basename.length..-1].scan(/.[^.]+/)
95
+ extnames = match.to_s[basename.to_s.length..-1].scan(/.[^.]+/)
82
96
  extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 }
83
97
  end
84
98
  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: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.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-02-04 00:00:00 -06:00
18
+ date: 2011-03-21 00:00:00 -05: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.5.0
68
+ rubygems_version: 1.5.3
69
69
  signing_key:
70
70
  specification_version: 3
71
71
  summary: Find files in a set of paths