hike 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Sam Stephenson
1
+ Copyright (c) 2014 Sam Stephenson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,8 +1,8 @@
1
1
  module Hike
2
2
  VERSION = "1.2.0"
3
3
 
4
+ autoload :CachedTrail, "hike/cached_trail"
4
5
  autoload :Extensions, "hike/extensions"
5
- autoload :Index, "hike/index"
6
6
  autoload :NormalizedArray, "hike/normalized_array"
7
7
  autoload :Paths, "hike/paths"
8
8
  autoload :Trail, "hike/trail"
@@ -1,22 +1,22 @@
1
1
  require 'pathname'
2
2
 
3
3
  module Hike
4
- # `Index` is an internal cached variant of `Trail`. It assumes the
4
+ # `CachedTrail` is an internal cached variant of `Trail`. It assumes the
5
5
  # file system does not change between `find` calls. All `stat` and
6
- # `entries` calls are cached for the lifetime of the `Index` object.
7
- class Index
8
- # `Index#paths` is an immutable `Paths` collection.
6
+ # `entries` calls are cached for the lifetime of the `CachedTrail` object.
7
+ class CachedTrail
8
+ # `CachedTrail#paths` is an immutable `Paths` collection.
9
9
  attr_reader :paths
10
10
 
11
- # `Index#extensions` is an immutable `Extensions` collection.
11
+ # `CachedTrail#extensions` is an immutable `Extensions` collection.
12
12
  attr_reader :extensions
13
13
 
14
- # `Index#aliases` is an immutable `Hash` mapping an extension to
14
+ # `CachedTrail#aliases` is an immutable `Hash` mapping an extension to
15
15
  # an `Array` of aliases.
16
16
  attr_reader :aliases
17
17
 
18
- # `Index.new` is an internal method. Instead of constructing it
19
- # directly, create a `Trail` and call `Trail#index`.
18
+ # `CachedTrail.new` is an internal method. Instead of constructing it
19
+ # directly, create a `Trail` and call `Trail#CachedTrail`.
20
20
  def initialize(root, paths, extensions, aliases)
21
21
  @root = root
22
22
 
@@ -35,41 +35,48 @@ module Hike
35
35
  @patterns = {}
36
36
  end
37
37
 
38
- # `Index#root` returns root path as a `String`. This attribute is immutable.
38
+ # `CachedTrail#root` returns root path as a `String`. This attribute is immutable.
39
39
  def root
40
40
  @root.to_s
41
41
  end
42
42
 
43
- # `Index#index` returns `self` to be compatable with the `Trail` interface.
44
- def index
43
+ # `CachedTrail#cached` returns `self` to be compatable with the `Trail` interface.
44
+ def cached
45
45
  self
46
46
  end
47
47
 
48
+ # Deprecated alias for `cached`.
49
+ alias_method :index, :cached
50
+
48
51
  # The real implementation of `find`. `Trail#find` generates a one
49
- # time index and delegates here.
52
+ # time cache and delegates here.
50
53
  #
51
54
  # See `Trail#find` for usage.
52
- def find(*logical_paths, &block)
53
- if block_given?
54
- options = extract_options!(logical_paths)
55
- base_path = Pathname.new(options[:base_path] || @root)
56
-
57
- logical_paths.each do |logical_path|
58
- logical_path = Pathname.new(logical_path.sub(/^\//, ''))
59
-
60
- if relative?(logical_path)
61
- find_in_base_path(logical_path, base_path, &block)
62
- else
63
- find_in_paths(logical_path, &block)
64
- end
65
- end
55
+ def find(*logical_paths)
56
+ find_all(*logical_paths).first
57
+ end
66
58
 
67
- nil
68
- else
69
- find(*logical_paths) do |path|
70
- return path
59
+ # The real implementation of `find_all`. `Trail#find_all` generates a one
60
+ # time index and delegates here.
61
+ #
62
+ # See `Trail#find_all` for usage.
63
+ def find_all(*logical_paths, &block)
64
+ return to_enum(__method__, *logical_paths) unless block_given?
65
+
66
+ options = extract_options!(logical_paths)
67
+ base_path = Pathname.new(options[:base_path] || @root)
68
+
69
+ logical_paths.each do |logical_path|
70
+ logical_path = Pathname.new(logical_path.sub(/^\//, ''))
71
+
72
+ if relative?(logical_path)
73
+ find_in_base_path(logical_path, base_path, &block)
74
+ else
75
+ find_in_paths(logical_path, &block)
71
76
  end
72
77
  end
78
+
79
+ nil
73
80
  end
74
81
 
75
82
  # A cached version of `Dir.entries` that filters out `.` files and
@@ -1,6 +1,6 @@
1
1
  require 'pathname'
2
+ require 'hike/cached_trail'
2
3
  require 'hike/extensions'
3
- require 'hike/index'
4
4
  require 'hike/paths'
5
5
 
6
6
  module Hike
@@ -28,7 +28,7 @@ module Hike
28
28
  # allows you to require files with specifiying `foo.rb`.
29
29
  attr_reader :extensions
30
30
 
31
- # `Index#aliases` is a mutable `Hash` mapping an extension to
31
+ # `Trail#aliases` is a mutable `Hash` mapping an extension to
32
32
  # an `Array` of aliases.
33
33
  #
34
34
  # trail = Hike::Trail.new
@@ -123,39 +123,46 @@ module Hike
123
123
  #
124
124
  # trail.find("hike") || trail.find("hike/index")
125
125
  #
126
- # Though `find` always returns the first match, it is possible
127
- # to iterate over all shadowed matches and fallbacks by supplying
128
- # a block.
126
+ def find(*args)
127
+ index.find(*args)
128
+ end
129
+
130
+ # `Trail#find_all` returns all matching paths including fallbacks and
131
+ # shadowed matches.
129
132
  #
130
- # trail.find("hike", "hike/index") { |path| warn path }
133
+ # trail.find_all("hike", "hike/index").each { |path| warn path }
131
134
  #
132
- # This allows you to filter your matches by any condition.
135
+ # `find_all` returns an `Enumerator`. This allows you to filter your
136
+ # matches by any condition.
133
137
  #
134
- # trail.find("application") do |path|
135
- # return path if mime_type_for(path) == "text/css"
138
+ # trail.find_all("application").find do |path|
139
+ # mime_type_for(path) == "text/css"
136
140
  # end
137
141
  #
138
- def find(*args, &block)
139
- index.find(*args, &block)
142
+ def find_all(*args, &block)
143
+ cached.find_all(*args, &block)
140
144
  end
141
145
 
142
- # `Trail#index` returns an `Index` object that has the same
143
- # interface as `Trail`. An `Index` is a cached `Trail` object that
146
+ # `Trail#cached` returns an `CachedTrail` object that has the same
147
+ # interface as `Trail`. An `CachedTrail` is a cached `Trail` object that
144
148
  # does not update when the file system changes. If you are
145
149
  # confident that you are not making changes the paths you are
146
- # searching, `index` will avoid excess system calls.
150
+ # searching, `cached` will avoid excess system calls.
147
151
  #
148
- # index = trail.index
149
- # index.find "hike/trail"
150
- # index.find "test_trail"
152
+ # cached = trail.cached
153
+ # cached.find "hike/trail"
154
+ # cached.find "test_trail"
151
155
  #
152
- def index
153
- Index.new(root, paths, extensions, aliases)
156
+ def cached
157
+ CachedTrail.new(root, paths, extensions, aliases)
154
158
  end
155
159
 
160
+ # Deprecated alias for `cached`.
161
+ alias_method :index, :cached
162
+
156
163
  # `Trail#entries` is equivalent to `Dir#entries`. It is not
157
164
  # recommend to use this method for general purposes. It exists for
158
- # parity with `Index#entries`.
165
+ # parity with `CachedTrail#entries`.
159
166
  def entries(path)
160
167
  pathname = Pathname.new(path)
161
168
  if pathname.directory?
@@ -167,7 +174,7 @@ module Hike
167
174
 
168
175
  # `Trail#stat` is equivalent to `File#stat`. It is not
169
176
  # recommend to use this method for general purposes. It exists for
170
- # parity with `Index#stat`.
177
+ # parity with `CachedTrail#stat`.
171
178
  def stat(path)
172
179
  if File.exist?(path)
173
180
  File.stat(path.to_s)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hike
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000 Z
12
+ date: 2014-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -36,8 +36,8 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - README.md
38
38
  - LICENSE
39
+ - lib/hike/cached_trail.rb
39
40
  - lib/hike/extensions.rb
40
- - lib/hike/index.rb
41
41
  - lib/hike/normalized_array.rb
42
42
  - lib/hike/paths.rb
43
43
  - lib/hike/trail.rb
@@ -54,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ! '>='
56
56
  - !ruby/object:Gem::Version
57
- version: '0'
57
+ version: 1.9.3
58
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  none: false
60
60
  requirements: