Class: Sprockets::Context

### Home Classes Methods

### In Files

- lib/sprockets/context.rb
- lib/sprockets/legacy.rb

### Parent

Object

### Methods

- ::new
- #asset_data_uri
- #asset_path
- #audio_path
- #depend_on
- #depend_on_asset
- #font_path
- #image_path
- #javascript_path
- #link_asset
- #load
- #metadata
- #require_asset
- #resolve
- #resolve_with_compat
- #resolve_without_compat
- #stub_asset
- #stylesheet_path
- #video_path

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Context

Deprecated: `Context` provides helper methods to all processors. They are typically accessed by ERB templates. You can mix in custom helpers by injecting them into `Environment#context_class`. Do not mix them into `Context` directly.

    environment.context_class.class_eval do
      include MyHelper
      def asset_url; end
    end

    <%= asset_url "foo.png" %>

The `Context` also collects dependencies declared by assets. See `DirectiveProcessor` for an example of this.

### Attributes

__LINE__[RW]

Deprecated

content_type[R]

Returns content type of file

    'application/javascript'
    'text/css'

environment[R]
filename[R]
load_path[R]

Returns the environment path that contains the file.

If `app/javascripts` and `app/stylesheets` are in your path, and current file is `app/javascripts/foo/bar.js`, `load_path` would return `app/javascripts`.

logical_path[R]

Returns logical path without any file extensions.

    'app/javascripts/application.js'
    # => 'application'

pathname[R]
root_path[R]

Returns the environment path that contains the file.

If `app/javascripts` and `app/stylesheets` are in your path, and current file is `app/javascripts/foo/bar.js`, `load_path` would return `app/javascripts`.

### Public Class Methods

new(input) click to toggle source

    # File lib/sprockets/context.rb, line 27
    def initialize(input)
      @environment  = input[:environment]
      @metadata     = input[:metadata]
      @load_path    = input[:load_path]
      @logical_path = input[:name]
      @filename     = input[:filename]
      @dirname      = File.dirname(@filename)
      @pathname     = Pathname.new(@filename)
      @content_type = input[:content_type]

      @required     = Set.new(@metadata[:required])
      @stubbed      = Set.new(@metadata[:stubbed])
      @links        = Set.new(@metadata[:links])
      @dependencies = Set.new(input[:metadata][:dependencies])
    end

### Public Instance Methods

asset_data_uri(path) click to toggle source

Returns a Base64-encoded `data:` URI with the contents of the asset at the specified path, and marks that path as a dependency of the current file.

Use `asset_data_uri` from ERB with CSS or JavaScript assets:

    #logo { background: url(<%= asset_data_uri 'logo.png' %>) }

    $('<img>').attr('src', '<%= asset_data_uri 'avatar.jpg' %>')

    # File lib/sprockets/context.rb, line 175
    def asset_data_uri(path)
      asset = depend_on_asset(path)
      data = EncodingUtils.base64(asset.source)
      "data:#{asset.content_type};base64,#{Rack::Utils.escape(data)}"
    end

asset_path(path, options = {}) click to toggle source

Expands logical path to full url to asset.

NOTE: This helper is currently not implemented and should be customized by the application. Though, in the future, some basics implemention may be provided with different methods that are required to be overridden.

    # File lib/sprockets/context.rb, line 187
    def asset_path(path, options = {})
      message = Custom asset_path helper is not implementedExtend your environment context with a custom method.    environment.context_class.class_eval do      def asset_path(path, options = {})      end    end
      raise NotImplementedError, message
    end

audio_path(path) click to toggle source

Expand logical audio asset path.

    # File lib/sprockets/context.rb, line 212
    def audio_path(path)
      asset_path(path, type: :audio)
    end

depend_on(path) click to toggle source

`depend_on` allows you to state a dependency on a file without including it.

This is used for caching purposes. Any changes made to the dependency file with invalidate the cache of the source file.

    # File lib/sprockets/context.rb, line 110
    def depend_on(path)
      path = path.to_s if path.is_a?(Pathname)

      if environment.absolute_path?(path) && environment.stat(path)
        @dependencies << environment.build_file_digest_uri(path)
      else
        resolve(path, compat: false)
      end
      nil
    end

depend_on_asset(path) click to toggle source

`depend_on_asset` allows you to state an asset dependency without including it.

This is used for caching purposes. Any changes that would invalidate the dependency asset will invalidate the source file. Unlike `depend_on`, this will include recursively include the target asset's dependencies.

    # File lib/sprockets/context.rb, line 128
    def depend_on_asset(path)
      load(resolve(path, compat: false))
    end

font_path(path) click to toggle source

Expand logical font asset path.

    # File lib/sprockets/context.rb, line 217
    def font_path(path)
      asset_path(path, type: :font)
    end

image_path(path) click to toggle source

Expand logical image asset path.

    # File lib/sprockets/context.rb, line 202
    def image_path(path)
      asset_path(path, type: :image)
    end

javascript_path(path) click to toggle source

Expand logical javascript asset path.

    # File lib/sprockets/context.rb, line 222
    def javascript_path(path)
      asset_path(path, type: :javascript)
    end

link_asset(path) click to toggle source

`link_asset` declares an external dependency on an asset without directly including it. The target asset is returned from this function making it easy to construct a link to it.

Returns an Asset or nil.

    # File lib/sprockets/context.rb, line 159
    def link_asset(path)
      asset = depend_on_asset(path)
      @links << asset.uri
      asset
    end

load(uri) click to toggle source

Public: Load Asset by AssetURI and track it as a dependency.

uri - AssetURI

Returns Asset.

    # File lib/sprockets/context.rb, line 98
    def load(uri)
      asset = environment.load(uri)
      @dependencies.merge(asset.metadata[:dependencies])
      asset
    end

metadata() click to toggle source

    # File lib/sprockets/context.rb, line 43
    def metadata
      { required: @required,
        stubbed: @stubbed,
        links: @links,
        dependencies: @dependencies }
    end

require_asset(path) click to toggle source

`require_asset` declares `path` as a dependency of the file. The dependency will be inserted before the file and will only be included once.

If ERB processing is enabled, you can use it to dynamically require assets.

    <%= require_asset "#{framework}.js" %>

    # File lib/sprockets/context.rb, line 141
    def require_asset(path)
      @required << resolve(path, accept: @content_type, pipeline: :self, compat: false)
      nil
    end

resolve(path, options = {}) click to toggle source

Public: Given a logical path, `resolve` will find and return an Asset URI. Relative paths will also be resolved. An accept type maybe given to restrict the search.

    resolve("foo.js")
    # => "file:///path/to/app/javascripts/foo.js?type=application/javascript"

    resolve("./bar.js")
    # => "file:///path/to/app/javascripts/bar.js?type=application/javascript"

path - String logical or absolute path options

    accept - String content accept type

Returns an Asset URI String.

    # File lib/sprockets/context.rb, line 87
    def resolve(path, options = {})
      uri, deps = environment.resolve!(path, options.merge(base_path: @dirname))
      @dependencies.merge(deps)
      uri
    end

Also aliased as: resolve_without_compat
resolve_with_compat(path, options = {}) click to toggle source

Deprecated: Change default return type of resolve() to return 2.x compatible plain filename String. 4.x will always return an Asset URI.

    2.x

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

    3.x

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

      resolve("foo.js", compat: true)
      # => "/path/to/app/javascripts/foo.js"

      resolve("foo.js", compat: false)
      # => "file:///path/to/app/javascripts/foo.js?type=application/javascript"

    4.x

      resolve("foo.js")
      # => "file:///path/to/app/javascripts/foo.js?type=application/javascript"

    # File lib/sprockets/legacy.rb, line 245
    def resolve_with_compat(path, options = {})
      options = options.dup

      # Support old :content_type option, prefer :accept going forward
      if type = options.delete(:content_type)
        type = self.content_type if type == :self
        options[:accept] ||= type
      end

      if options.delete(:compat) { true }
        uri = resolve_without_compat(path, options)
        path, _ = environment.parse_asset_uri(uri)
        path
      else
        resolve_without_compat(path, options)
      end
    end

Also aliased as: resolve
resolve_without_compat(path, options = {}) click to toggle source
Alias for: resolve
stub_asset(path) click to toggle source

`stub_asset` blacklists `path` from being included in the bundle. `path` must be an asset which may or may not already be included in the bundle.

    # File lib/sprockets/context.rb, line 149
    def stub_asset(path)
      @stubbed << resolve(path, accept: @content_type, pipeline: :self, compat: false)
      nil
    end

stylesheet_path(path) click to toggle source

Expand logical stylesheet asset path.

    # File lib/sprockets/context.rb, line 227
    def stylesheet_path(path)
      asset_path(path, type: :stylesheet)
    end

video_path(path) click to toggle source

Expand logical video asset path.

    # File lib/sprockets/context.rb, line 207
    def video_path(path)
      asset_path(path, type: :video)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.