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
-
#extensions ⇒ Object
readonly
‘Trail#extensions` is a mutable `Extensions` collection.
-
#paths ⇒ Object
readonly
‘Trail#paths` is a mutable `Paths` collection.
Instance Method Summary collapse
-
#entries(*args) ⇒ 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.
-
#root ⇒ Object
‘Trail#root` returns root path as a `String`.
-
#stat(*args) ⇒ Object
‘Trail#stat` is equivalent to `File#stat`.
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.
34 35 36 37 38 |
# File 'lib/hike/trail.rb', line 34 def initialize(root = ".") @root = Pathname.new(root). @paths = Paths.new(@root) @extensions = Extensions.new end |
Instance Attribute Details
#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
#entries(*args) ⇒ 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`.
100 101 102 |
# File 'lib/hike/trail.rb', line 100 def entries(*args) index.entries(*args) 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
79 80 81 |
# File 'lib/hike/trail.rb', line 79 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"
93 94 95 |
# File 'lib/hike/trail.rb', line 93 def index Index.new(root, paths, extensions) end |
#root ⇒ Object
‘Trail#root` returns root path as a `String`. This attribute is immutable.
41 42 43 |
# File 'lib/hike/trail.rb', line 41 def root @root.to_s end |
#stat(*args) ⇒ 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`.
107 108 109 |
# File 'lib/hike/trail.rb', line 107 def stat(*args) index.stat(*args) end |