Module: Sprockets::Trail

Included in:
Base
Defined in:
lib/sprockets/trail.rb

Overview

Trail is an internal mixin whose public methods are exposed on the Environment and Index classes.

Instance Method Summary collapse

Instance Method Details

#append_path(path) ⇒ Object

Append a path to the paths list.

Paths at the beginning of the Array have a higher priority.



38
39
40
41
# File 'lib/sprockets/trail.rb', line 38

def append_path(path)
  expire_index!
  @trail.append_path(path)
end

#clear_pathsObject

Clear all paths and start fresh.

There is no mechanism for reordering paths, so its best to completely wipe the paths list and reappend them in the order you want.



48
49
50
51
# File 'lib/sprockets/trail.rb', line 48

def clear_paths
  expire_index!
  @trail.paths.dup.each { |path| @trail.remove_path(path) }
end

#extensionsObject

Returns an Array of extensions.

These extensions maybe omitted from logical path searches.

# => [".js", ".css", ".coffee", ".sass", ...]


59
60
61
# File 'lib/sprockets/trail.rb', line 59

def extensions
  trail.extensions.dup
end

#pathsObject

Returns an Array of path ‘String`s.

These paths will be used for asset logical path lookups.

Note that a copy of the Array is returned so mutating will have no affect on the environment. See append_path, prepend_path, and clear_paths.



23
24
25
# File 'lib/sprockets/trail.rb', line 23

def paths
  trail.paths.dup
end

#prepend_path(path) ⇒ Object

Prepend a path to the paths list.

Paths at the end of the Array have the least priority.



30
31
32
33
# File 'lib/sprockets/trail.rb', line 30

def prepend_path(path)
  expire_index!
  @trail.prepend_path(path)
end

#resolve(logical_path, options = {}) ⇒ Object

Finds the expanded real path for a given logical path by searching the environment’s paths.

resolve("application.js")
# => "/path/to/app/javascripts/application.js.coffee"

A FileNotFound exception is raised if the file does not exist.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sprockets/trail.rb', line 70

def resolve(logical_path, options = {})
  # If a block is given, preform an iterable search
  if block_given?
    args = attributes_for(logical_path).search_paths + [options]
    trail.find(*args) do |path|
      yield Pathname.new(path)
    end
  else
    resolve(logical_path, options) do |pathname|
      return pathname
    end
    raise FileNotFound, "couldn't find file '#{logical_path}'"
  end
end

#rootObject

Returns Environment root.

All relative paths are expanded with root as its base. To be useful set this to your applications root directory. (Rails.root)



12
13
14
# File 'lib/sprockets/trail.rb', line 12

def root
  trail.root.dup
end