hike 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/hike.rb +2 -1
  2. data/lib/hike/index.rb +114 -0
  3. data/lib/hike/trail.rb +8 -108
  4. metadata +6 -5
@@ -1,7 +1,8 @@
1
1
  module Hike
2
- VERSION = "0.5.1"
2
+ VERSION = "0.7.0"
3
3
 
4
4
  autoload :Extensions, "hike/extensions"
5
+ autoload :Index, "hike/index"
5
6
  autoload :NormalizedArray, "hike/normalized_array"
6
7
  autoload :Paths, "hike/paths"
7
8
  autoload :Trail, "hike/trail"
@@ -0,0 +1,114 @@
1
+ require 'pathname'
2
+
3
+ module Hike
4
+ class Index
5
+ attr_reader :root, :paths, :extensions
6
+
7
+ def initialize(root, paths, extensions)
8
+ @root = Pathname.new(root).expand_path
9
+ @paths = paths.map { |path| Pathname.new(path).expand_path }
10
+ @extensions = extensions.to_a
11
+
12
+ @stats = {}
13
+ @entries = {}
14
+ @patterns = {}
15
+ end
16
+
17
+ def find(*logical_paths, &block)
18
+ if block_given?
19
+ options = extract_options!(logical_paths)
20
+ base_path = Pathname.new(options[:base_path] || @root)
21
+
22
+ logical_paths.each do |logical_path|
23
+ logical_path = Pathname.new(logical_path.sub(/^\//, ''))
24
+
25
+ if relative?(logical_path)
26
+ find_in_base_path(logical_path, base_path, &block)
27
+ else
28
+ find_in_paths(logical_path, &block)
29
+ end
30
+ end
31
+
32
+ nil
33
+ else
34
+ find(*logical_paths) do |path|
35
+ return path
36
+ end
37
+ end
38
+ end
39
+
40
+ protected
41
+ def extract_options!(arguments)
42
+ arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
43
+ end
44
+
45
+ def relative?(logical_path)
46
+ logical_path.to_s =~ /^\.\.?\//
47
+ end
48
+
49
+ def find_in_paths(logical_path, &block)
50
+ dirname, basename = logical_path.split
51
+ paths.each do |base_path|
52
+ match(base_path.join(dirname), basename, &block)
53
+ end
54
+ end
55
+
56
+ def find_in_base_path(logical_path, base_path, &block)
57
+ candidate = base_path.join(logical_path)
58
+ dirname, basename = candidate.split
59
+ match(dirname, basename, &block) if paths_contain?(dirname)
60
+ end
61
+
62
+ def match(dirname, basename)
63
+ matches = entries(dirname)
64
+
65
+ pattern = pattern_for(basename)
66
+ matches = matches.select { |m| m.to_s =~ pattern }
67
+
68
+ sort_matches(matches, basename).each do |path|
69
+ pathname = dirname.join(path)
70
+ stat = stat(pathname)
71
+
72
+ if stat && stat.file?
73
+ yield pathname.to_s
74
+ end
75
+ end
76
+ end
77
+
78
+ def stat(pathname)
79
+ if @stats.key?(pathname)
80
+ @stats[pathname]
81
+ else
82
+ begin
83
+ @stats[pathname] = pathname.stat
84
+ rescue Errno::ENOENT
85
+ @stats[pathname] = nil
86
+ end
87
+ end
88
+ end
89
+
90
+ def entries(pathname)
91
+ @entries[pathname] ||= pathname.entries.reject { |entry| entry.to_s =~ /^\.\.?$/ }
92
+ rescue Errno::ENOENT
93
+ @entries[pathname] = []
94
+ end
95
+
96
+ def paths_contain?(dirname)
97
+ paths.any? { |path| dirname.to_s[0, path.to_s.length] == path.to_s }
98
+ end
99
+
100
+ def pattern_for(basename)
101
+ @patterns[basename] ||= begin
102
+ extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
103
+ /^#{Regexp.escape(basename.to_s)}(?:#{extension_pattern}|)+$/
104
+ end
105
+ end
106
+
107
+ def sort_matches(matches, basename)
108
+ matches.sort_by do |match|
109
+ extnames = match.to_s[basename.to_s.length..-1].scan(/.[^.]+/)
110
+ extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 }
111
+ end
112
+ end
113
+ end
114
+ end
@@ -1,5 +1,6 @@
1
1
  require 'pathname'
2
2
  require 'hike/extensions'
3
+ require 'hike/index'
3
4
  require 'hike/paths'
4
5
 
5
6
  module Hike
@@ -7,8 +8,8 @@ module Hike
7
8
  attr_reader :paths, :extensions
8
9
 
9
10
  def initialize(root = ".")
10
- @root = Pathname.new(root).expand_path
11
- @paths = Paths.new(@root)
11
+ @root = Pathname.new(root).expand_path
12
+ @paths = Paths.new(@root)
12
13
  @extensions = Extensions.new
13
14
  end
14
15
 
@@ -16,113 +17,12 @@ module Hike
16
17
  @root.to_s
17
18
  end
18
19
 
19
- def find(*logical_paths, &block)
20
- if block_given?
21
- options = extract_options!(logical_paths)
22
- base_path = Pathname.new(options[:base_path] || @root)
23
-
24
- options[:directories] ||= false
25
-
26
- options[:stat_cache] = {}
27
- options[:entries_cache] = {}
28
- options[:patterns_cache] = {}
29
-
30
- logical_paths.each do |logical_path|
31
- logical_path = Pathname.new(logical_path.sub(/^\//, ''))
32
-
33
- if relative?(logical_path)
34
- find_in_base_path(logical_path, base_path, options, &block)
35
- else
36
- find_in_paths(logical_path, options, &block)
37
- end
38
- end
39
-
40
- nil
41
- else
42
- find(*logical_paths) do |path|
43
- return path
44
- end
45
- end
20
+ def index
21
+ Index.new(root, paths, extensions)
46
22
  end
47
23
 
48
- protected
49
- def extract_options!(arguments)
50
- arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
51
- end
52
-
53
- def relative?(logical_path)
54
- logical_path.to_s =~ /^\.\.?\//
55
- end
56
-
57
- def pathnames
58
- paths.map { |path| Pathname.new(path) }
59
- end
60
-
61
- def find_in_paths(logical_path, options, &block)
62
- dirname, basename = logical_path.split
63
- pathnames.each do |base_path|
64
- match(base_path.join(dirname), basename, options, &block)
65
- end
66
- end
67
-
68
- def find_in_base_path(logical_path, base_path, options, &block)
69
- candidate = base_path.join(logical_path)
70
- dirname, basename = candidate.split
71
- match(dirname, basename, options, &block) if paths_contain?(dirname)
72
- end
73
-
74
- def match(dirname, basename, options)
75
- matches = entries(options[:entries_cache], dirname)
76
-
77
- pattern = pattern_for(options[:patterns_cache], basename)
78
- matches = matches.select { |m| m.to_s =~ pattern }
79
-
80
- cache = options[:stat_cache]
81
- sort_matches(matches, basename).each do |path|
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
101
- end
102
- end
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
-
110
- def paths_contain?(dirname)
111
- paths.any? { |path| dirname.to_s[0, path.to_s.length] == path }
112
- end
113
-
114
- def pattern_for(cache, basename)
115
- cache[basename] ||= begin
116
- extension_pattern = extensions.map { |e| Regexp.escape(e) }.join("|")
117
- /^#{Regexp.escape(basename.to_s)}(?:#{extension_pattern}|)+$/
118
- end
119
- end
120
-
121
- def sort_matches(matches, basename)
122
- matches.sort_by do |match|
123
- extnames = match.to_s[basename.to_s.length..-1].scan(/.[^.]+/)
124
- extnames.inject(0) { |sum, ext| sum + extensions.index(ext) + 1 }
125
- end
126
- end
24
+ def find(*args, &block)
25
+ index.find(*args, &block)
26
+ end
127
27
  end
128
28
  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: 7
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
8
+ - 7
9
9
  - 0
10
- version: 0.6.0
10
+ version: 0.7.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-30 00:00:00 -05:00
18
+ date: 2011-04-03 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,6 +30,7 @@ extra_rdoc_files: []
30
30
 
31
31
  files:
32
32
  - lib/hike/extensions.rb
33
+ - lib/hike/index.rb
33
34
  - lib/hike/normalized_array.rb
34
35
  - lib/hike/paths.rb
35
36
  - lib/hike/trail.rb
@@ -64,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
65
  requirements: []
65
66
 
66
67
  rubyforge_project:
67
- rubygems_version: 1.5.3
68
+ rubygems_version: 1.5.0
68
69
  signing_key:
69
70
  specification_version: 3
70
71
  summary: Find files in a set of paths