Class: Hike::Trail
- Inherits:
-
Object
- Object
- Hike::Trail
- Defined in:
- lib/hike/trail.rb
Overview
‘Trail` is the public container class for holding paths and extensions.
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
‘Index#aliases` is a mutable `Hash` mapping an extension to an `Array` of aliases.
-
#extensions ⇒ Object
readonly
‘Trail#extensions` is a mutable `Extensions` collection.
-
#paths ⇒ Object
readonly
‘Trail#paths` is a mutable `Paths` collection.
Instance Method Summary collapse
-
#alias_extension(new_extension, old_extension) ⇒ Object
Alias ‘new_extension` to `old_extension`.
-
#append_extensions(*extensions) ⇒ Object
(also: #append_extension)
Append ‘extension` to `Extensions` collection.
-
#append_paths(*paths) ⇒ Object
(also: #append_path)
Append ‘path` to `Paths` collection.
-
#entries(path) ⇒ Object
‘Trail#entries` is equivalent to `Dir#entries`.
-
#find(*args, &block) ⇒ Object
‘Trail#find` returns a the expand path for a logical path in the path collection.
-
#index ⇒ Object
‘Trail#index` returns an `Index` object that has the same interface as `Trail`.
-
#initialize(root = ".") ⇒ Trail
constructor
A Trail accepts an optional root path that defaults to your current working directory.
-
#prepend_extensions(*extensions) ⇒ Object
(also: #prepend_extension)
Prepend ‘extension` to `Extensions` collection.
-
#prepend_paths(*paths) ⇒ Object
(also: #prepend_path)
Prepend ‘path` to `Paths` collection.
-
#remove_extension(extension) ⇒ Object
Remove ‘extension` from `Extensions` collection.
-
#remove_path(path) ⇒ Object
Remove ‘path` from `Paths` collection.
-
#root ⇒ Object
‘Trail#root` returns root path as a `String`.
-
#stat(path) ⇒ Object
‘Trail#stat` is equivalent to `File#stat`.
-
#unalias_extension(extension) ⇒ Object
Remove the alias for ‘extension`.
Constructor Details
#initialize(root = ".") ⇒ Trail
A Trail accepts an optional root path that defaults to your current working directory. Any relative paths added to ‘Trail#paths` will expanded relative to the root.
48 49 50 51 52 53 |
# File 'lib/hike/trail.rb', line 48 def initialize(root = ".") @root = Pathname.new(root). @paths = Paths.new(@root) @extensions = Extensions.new @aliases = Hash.new { |h, k| h[k] = Extensions.new } end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
‘Index#aliases` is a mutable `Hash` mapping an extension to an `Array` of aliases.
trail = Hike::Trail.new
trail.paths.push "~/Projects/hike/site"
trail.aliases['.htm'] = 'html'
trail.aliases['.xhtml'] = 'html'
trail.aliases['.php'] = 'html'
Aliases provide a fallback when the primary extension is not matched. In the example above, a lookup for “foo.html” will check for the existence of “foo.htm”, “foo.xhtml”, or “foo.php”.
43 44 45 |
# File 'lib/hike/trail.rb', line 43 def aliases @aliases end |
#extensions ⇒ Object (readonly)
‘Trail#extensions` is a mutable `Extensions` collection.
trail = Hike::Trail.new
trail.paths.push "~/Projects/hike/lib"
trail.extensions.push ".rb"
Extensions allow you to find files by just their name omitting their extension. Is similar to Ruby’s require mechanism that allows you to require files with specifiying ‘foo.rb`.
29 30 31 |
# File 'lib/hike/trail.rb', line 29 def extensions @extensions end |
#paths ⇒ Object (readonly)
‘Trail#paths` is a mutable `Paths` collection.
trail = Hike::Trail.new
trail.paths.push "~/Projects/hike/lib", "~/Projects/hike/test"
The order of the paths is significant. Paths in the beginning of the collection will be checked first. In the example above, ‘~/Projects/hike/lib/hike.rb` would shadow the existent of `~/Projects/hike/test/hike.rb`.
18 19 20 |
# File 'lib/hike/trail.rb', line 18 def paths @paths end |
Instance Method Details
#alias_extension(new_extension, old_extension) ⇒ Object
Alias ‘new_extension` to `old_extension`
95 96 97 |
# File 'lib/hike/trail.rb', line 95 def alias_extension(new_extension, old_extension) aliases[normalize_extension(new_extension)] = normalize_extension(old_extension) end |
#append_extensions(*extensions) ⇒ Object Also known as: append_extension
Append ‘extension` to `Extensions` collection
84 85 86 |
# File 'lib/hike/trail.rb', line 84 def append_extensions(*extensions) self.extensions.push(*extensions) end |
#append_paths(*paths) ⇒ Object Also known as: append_path
Append ‘path` to `Paths` collection
67 68 69 |
# File 'lib/hike/trail.rb', line 67 def append_paths(*paths) self.paths.push(*paths) end |
#entries(path) ⇒ Object
‘Trail#entries` is equivalent to `Dir#entries`. It is not recommend to use this method for general purposes. It exists for parity with `Index#entries`.
159 160 161 162 163 164 165 166 |
# File 'lib/hike/trail.rb', line 159 def entries(path) pathname = Pathname.new(path) if pathname.directory? pathname.entries.reject { |entry| entry.to_s =~ /^\.|~$|^\#.*\#$/ }.sort else [] end end |
#find(*args, &block) ⇒ Object
‘Trail#find` returns a the expand path for a logical path in the path collection.
trail = Hike::Trail.new "~/Projects/hike"
trail.extensions.push ".rb"
trail.paths.push "lib", "test"
trail.find "hike/trail"
# => "~/Projects/hike/lib/hike/trail.rb"
trail.find "test_trail"
# => "~/Projects/hike/test/test_trail.rb"
‘find` accepts multiple fallback logical paths that returns the first match.
trail.find "hike", "hike/index"
is equivalent to
trail.find("hike") || trail.find("hike/index")
Though ‘find` always returns the first match, it is possible to iterate over all shadowed matches and fallbacks by supplying a block.
trail.find("hike", "hike/index") { |path| warn path }
This allows you to filter your matches by any condition.
trail.find("application") do |path|
return path if mime_type_for(path) == "text/css"
end
138 139 140 |
# File 'lib/hike/trail.rb', line 138 def find(*args, &block) index.find(*args, &block) end |
#index ⇒ Object
‘Trail#index` returns an `Index` object that has the same interface as `Trail`. An `Index` is a cached `Trail` object that does not update when the file system changes. If you are confident that you are not making changes the paths you are searching, `index` will avoid excess system calls.
index = trail.index
index.find "hike/trail"
index.find "test_trail"
152 153 154 |
# File 'lib/hike/trail.rb', line 152 def index Index.new(root, paths, extensions, aliases) end |
#prepend_extensions(*extensions) ⇒ Object Also known as: prepend_extension
Prepend ‘extension` to `Extensions` collection
78 79 80 |
# File 'lib/hike/trail.rb', line 78 def prepend_extensions(*extensions) self.extensions.unshift(*extensions) end |
#prepend_paths(*paths) ⇒ Object Also known as: prepend_path
Prepend ‘path` to `Paths` collection
61 62 63 |
# File 'lib/hike/trail.rb', line 61 def prepend_paths(*paths) self.paths.unshift(*paths) end |
#remove_extension(extension) ⇒ Object
Remove ‘extension` from `Extensions` collection
90 91 92 |
# File 'lib/hike/trail.rb', line 90 def remove_extension(extension) self.extensions.delete(extension) end |
#remove_path(path) ⇒ Object
Remove ‘path` from `Paths` collection
73 74 75 |
# File 'lib/hike/trail.rb', line 73 def remove_path(path) self.paths.delete(path) end |
#root ⇒ Object
‘Trail#root` returns root path as a `String`. This attribute is immutable.
56 57 58 |
# File 'lib/hike/trail.rb', line 56 def root @root.to_s end |
#stat(path) ⇒ Object
‘Trail#stat` is equivalent to `File#stat`. It is not recommend to use this method for general purposes. It exists for parity with `Index#stat`.
171 172 173 174 175 176 177 |
# File 'lib/hike/trail.rb', line 171 def stat(path) if File.exist?(path) File.stat(path.to_s) else nil end end |
#unalias_extension(extension) ⇒ Object
Remove the alias for ‘extension`
100 101 102 |
# File 'lib/hike/trail.rb', line 100 def unalias_extension(extension) aliases.delete(normalize_extension(extension)) end |