class Opal::Hike::Trail

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

Attributes

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/opal/hike.rb, line 217
def initialize(root = '.')
  @root       = Pathname.new(root).expand_path
  @paths      = []
  @extensions = []
end

Public Instance Methods

append_extensions(*extensions) click to toggle source

Append ‘extension` to `Extensions` collection

# File lib/opal/hike.rb, line 234
def append_extensions(*extensions)
  @extensions.concat(extensions.map { |e| normalize_extension(e) })
end
append_paths(*paths) click to toggle source

Append ‘path` to `Paths` collection

# File lib/opal/hike.rb, line 229
def append_paths(*paths)
  @paths.concat(paths.map { |p| normalize_path(p) })
end
entries(path) click to toggle source

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

# File lib/opal/hike.rb, line 272
def entries(path)
  pathname = Pathname.new(path)
  if pathname.directory?
    pathname.entries.reject { |entry| entry.to_s =~ /^\.|~$|^\#.*\#$/ }.sort
  else
    []
  end
end
find(*args, &block) 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"
# File lib/opal/hike.rb, line 251
def find(*args, &block)
  index.find(*args, &block)
end
index() click to toggle source

‘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"
# File lib/opal/hike.rb, line 265
def index
  Index.new(root, paths, extensions)
end
root() click to toggle source

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

# File lib/opal/hike.rb, line 224
def root
  @root.to_s
end
stat(path) click to toggle source

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

# File lib/opal/hike.rb, line 284
def stat(path)
  if File.exist?(path)
    File.stat(path.to_s)
  else
    # nil
  end
end

Private Instance Methods

normalize_extension(ext) click to toggle source
# File lib/opal/hike.rb, line 294
def normalize_extension(ext)
  ext.start_with?('.') ? ext : ".#{ext}"
end
normalize_path(path) click to toggle source
# File lib/opal/hike.rb, line 298
def normalize_path(path)
  path = Pathname.new(path)
  path = @root.join(path) if path.relative?
  path.expand_path.to_s
end