class Hike::Trail

`Trail` is the public container class for holding paths and extensions.

Attributes

aliases[R]

`Trail#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”.

extensions[R]

`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`.

paths[R]

`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`.

Public Class Methods

new(root = ".") click to toggle source

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.

# File lib/hike/trail.rb, line 50
def initialize(root = ".")
  @root       = Pathname.new(root).expand_path
  @paths      = Paths.new(@root)
  @extensions = Extensions.new
  @aliases    = {}
end

Public Instance Methods

alias_extension(new_extension, old_extension) click to toggle source

Alias `new_extension` to `old_extension`

# File lib/hike/trail.rb, line 97
def alias_extension(new_extension, old_extension)
  aliases[normalize_extension(new_extension)] = normalize_extension(old_extension)
end
append_extension(*extensions)
Alias for: append_extensions
append_extensions(*extensions) click to toggle source

Append `extension` to `Extensions` collection

# File lib/hike/trail.rb, line 86
def append_extensions(*extensions)
  self.extensions.push(*extensions)
end
Also aliased as: append_extension
append_path(*paths)
Alias for: append_paths
append_paths(*paths) click to toggle source

Append `path` to `Paths` collection

# File lib/hike/trail.rb, line 69
def append_paths(*paths)
  self.paths.push(*paths)
end
Also aliased as: append_path
cached() click to toggle source

`Trail#cached` returns an `CachedTrail` object that has the same interface as `Trail`. An `CachedTrail` 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, `cached` will avoid excess system calls.

cached = trail.cached
cached.find "hike/trail"
cached.find "test_trail"
# File lib/hike/trail.rb, line 158
def cached
  CachedTrail.new(root, paths, extensions, aliases)
end
Also aliased as: index
find(*args) click to toggle source

`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")
# File lib/hike/trail.rb, line 128
def find(*args)
  index.find(*args)
end
find_all(*args, &block) click to toggle source

`Trail#find_all` returns all matching paths including fallbacks and

shadowed matches.

   trail.find_all("hike", "hike/index").each { |path| warn path }

`find_all` returns an `Enumerator`. This allows you to filter your matches by any condition.

trail.find_all("application").find do |path|
  mime_type_for(path) == "text/css"
end
# File lib/hike/trail.rb, line 144
def find_all(*args, &block)
  cached.find_all(*args, &block)
end
index()

Deprecated alias for `cached`.

Alias for: cached
prepend_extension(*extensions)
Alias for: prepend_extensions
prepend_extensions(*extensions) click to toggle source

Prepend `extension` to `Extensions` collection

# File lib/hike/trail.rb, line 80
def prepend_extensions(*extensions)
  self.extensions.unshift(*extensions)
end
Also aliased as: prepend_extension
prepend_path(*paths)
Alias for: prepend_paths
prepend_paths(*paths) click to toggle source

Prepend `path` to `Paths` collection

# File lib/hike/trail.rb, line 63
def prepend_paths(*paths)
  self.paths.unshift(*paths)
end
Also aliased as: prepend_path
remove_extension(extension) click to toggle source

Remove `extension` from `Extensions` collection

# File lib/hike/trail.rb, line 92
def remove_extension(extension)
  self.extensions.delete(extension)
end
remove_path(path) click to toggle source

Remove `path` from `Paths` collection

# File lib/hike/trail.rb, line 75
def remove_path(path)
  self.paths.delete(path)
end
root() click to toggle source

`Trail#root` returns root path as a `String`. This attribute is immutable.

# File lib/hike/trail.rb, line 58
def root
  @root.to_s
end
unalias_extension(extension) click to toggle source

Remove the alias for `extension`

# File lib/hike/trail.rb, line 102
def unalias_extension(extension)
  aliases.delete(normalize_extension(extension))
end

Private Instance Methods

normalize_extension(extension) click to toggle source
# File lib/hike/trail.rb, line 166
def normalize_extension(extension)
  if extension[/^\./]
    extension
  else
    ".#{extension}"
  end
end