Class: Sprockets::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/context.rb

Overview

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.

Defined Under Namespace

Classes: ENVProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Context

Returns a new instance of Context.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sprockets/context.rb', line 41

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)
  @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

Instance Attribute Details

#content_typeObject (readonly)

Returns content type of file

'application/javascript'
'text/css'


87
88
89
# File 'lib/sprockets/context.rb', line 87

def content_type
  @content_type
end

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#load_pathObject (readonly) Also known as: root_path

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



72
73
74
# File 'lib/sprockets/context.rb', line 72

def load_path
  @load_path
end

#logical_pathObject (readonly)

Returns logical path without any file extensions.

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


80
81
82
# File 'lib/sprockets/context.rb', line 80

def logical_path
  @logical_path
end

Instance Method Details

#asset_data_uri(path) ⇒ Object

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

Uses URI encoding for SVG files, base64 encoding for all the other files.

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' %>')


200
201
202
203
204
205
206
207
# File 'lib/sprockets/context.rb', line 200

def asset_data_uri(path)
  asset = depend_on_asset(path)
  if asset.content_type == 'image/svg+xml'
    svg_asset_data_uri(asset)
  else
    base64_asset_data_uri(asset)
  end
end

#asset_path(path, options = {}) ⇒ Object

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.



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/sprockets/context.rb', line 215

def asset_path(path, options = {})
  message = <<-EOS
Custom asset_path helper is not implemented

Extend your environment context with a custom method.

environment.context_class.class_eval do
  def asset_path(path, options = {})
  end
end
  EOS
  raise NotImplementedError, message
end

#audio_path(path) ⇒ Object

Expand logical audio asset path.



240
241
242
# File 'lib/sprockets/context.rb', line 240

def audio_path(path)
  asset_path(path, type: :audio)
end

#depend_on(path) ⇒ Object

‘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 will invalidate the cache of the source file.



127
128
129
130
131
132
133
134
# File 'lib/sprockets/context.rb', line 127

def depend_on(path)
  if environment.absolute_path?(path) && environment.stat(path)
    @dependencies << environment.build_file_digest_uri(path)
  else
    resolve(path)
  end
  nil
end

#depend_on_asset(path) ⇒ Object

‘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 recursively include the target asset’s dependencies.



143
144
145
# File 'lib/sprockets/context.rb', line 143

def depend_on_asset(path)
  load(resolve(path))
end

#depend_on_env(key) ⇒ Object

‘depend_on_env` allows you to state a dependency on an environment variable.

This is used for caching purposes. Any changes in the value of the environment variable will invalidate the cache of the source file.



152
153
154
# File 'lib/sprockets/context.rb', line 152

def depend_on_env(key)
  @dependencies << "env:#{key}"
end

#env_proxyObject



63
64
65
# File 'lib/sprockets/context.rb', line 63

def env_proxy
  ENVProxy.new(self)
end

#font_path(path) ⇒ Object

Expand logical font asset path.



245
246
247
# File 'lib/sprockets/context.rb', line 245

def font_path(path)
  asset_path(path, type: :font)
end

#image_path(path) ⇒ Object

Expand logical image asset path.



230
231
232
# File 'lib/sprockets/context.rb', line 230

def image_path(path)
  asset_path(path, type: :image)
end

#javascript_path(path) ⇒ Object

Expand logical javascript asset path.



250
251
252
# File 'lib/sprockets/context.rb', line 250

def javascript_path(path)
  asset_path(path, type: :javascript)
end

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



183
184
185
186
187
# File 'lib/sprockets/context.rb', line 183

def link_asset(path)
  asset = depend_on_asset(path)
  @links << asset.uri
  asset
end

#load(uri) ⇒ Object

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

uri - AssetURI

Returns Asset.



115
116
117
118
119
# File 'lib/sprockets/context.rb', line 115

def load(uri)
  asset = environment.load(uri)
  @dependencies.merge(asset.[:dependencies])
  asset
end

#metadataObject



56
57
58
59
60
61
# File 'lib/sprockets/context.rb', line 56

def 
  { required: @required,
    stubbed: @stubbed,
    links: @links,
    dependencies: @dependencies }
end

#require_asset(path) ⇒ Object

‘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" %>


165
166
167
168
# File 'lib/sprockets/context.rb', line 165

def require_asset(path)
  @required << resolve(path, accept: @content_type, pipeline: :self)
  nil
end

#resolve(path, **kargs) ⇒ Object

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 accept - String content accept type

Returns an Asset URI String.



103
104
105
106
107
108
# File 'lib/sprockets/context.rb', line 103

def resolve(path, **kargs)
  kargs[:base_path] = @dirname
  uri, deps = environment.resolve!(path, **kargs)
  @dependencies.merge(deps)
  uri
end

#stub_asset(path) ⇒ Object

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



173
174
175
176
# File 'lib/sprockets/context.rb', line 173

def stub_asset(path)
  @stubbed << resolve(path, accept: @content_type, pipeline: :self)
  nil
end

#stylesheet_path(path) ⇒ Object

Expand logical stylesheet asset path.



255
256
257
# File 'lib/sprockets/context.rb', line 255

def stylesheet_path(path)
  asset_path(path, type: :stylesheet)
end

#video_path(path) ⇒ Object

Expand logical video asset path.



235
236
237
# File 'lib/sprockets/context.rb', line 235

def video_path(path)
  asset_path(path, type: :video)
end