Class: Opal::Hike::Trail

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/hike.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



217
218
219
220
221
# File 'opal/lib/opal/hike.rb', line 217

def initialize(root = '.')
  @root       = Pathname.new(root).expand_path
  @paths      = []
  @extensions = []
end

Instance Attribute Details

#extensionsObject (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.



212
213
214
# File 'opal/lib/opal/hike.rb', line 212

def extensions
  @extensions
end

#pathsObject (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.



201
202
203
# File 'opal/lib/opal/hike.rb', line 201

def paths
  @paths
end

Instance Method Details

#append_extensions(*extensions) ⇒ Object

Append extension to Extensions collection



234
235
236
# File 'opal/lib/opal/hike.rb', line 234

def append_extensions(*extensions)
  @extensions.concat(extensions.map { |e| normalize_extension(e) })
end

#append_paths(*paths) ⇒ Object

Append path to Paths collection



229
230
231
# File 'opal/lib/opal/hike.rb', line 229

def append_paths(*paths)
  @paths.concat(paths.map { |p| normalize_path(p) })
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.



272
273
274
275
276
277
278
279
# File 'opal/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) ⇒ 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"


251
252
253
# File 'opal/lib/opal/hike.rb', line 251

def find(*args, &block)
  index.find(*args, &block)
end

#indexObject

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"


265
266
267
# File 'opal/lib/opal/hike.rb', line 265

def index
  Index.new(root, paths, extensions)
end

#rootObject

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



224
225
226
# File 'opal/lib/opal/hike.rb', line 224

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.



284
285
286
287
288
289
290
# File 'opal/lib/opal/hike.rb', line 284

def stat(path)
  if File.exist?(path)
    File.stat(path.to_s)
  else
    # nil
  end
end