Class: Sprockets::Context

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Context

Returns a new instance of Context.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#__LINE__Object

Deprecated



25
26
27
# File 'lib/sprockets/context.rb', line 25

def __LINE__
  @__LINE__
end

#content_typeObject (readonly)

Returns content type of file

'application/javascript'
'text/css'


70
71
72
# File 'lib/sprockets/context.rb', line 70

def content_type
  @content_type
end

#environmentObject (readonly)

Returns the value of attribute environment.



22
23
24
# File 'lib/sprockets/context.rb', line 22

def environment
  @environment
end

#filenameObject (readonly)

Returns the value of attribute filename.



22
23
24
# File 'lib/sprockets/context.rb', line 22

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



55
56
57
# File 'lib/sprockets/context.rb', line 55

def load_path
  @load_path
end

#logical_pathObject (readonly)

Returns logical path without any file extensions.

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


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

def logical_path
  @logical_path
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



22
23
24
# File 'lib/sprockets/context.rb', line 22

def pathname
  @pathname
end

Instance Method Details

#asset_data_uri(path) ⇒ Object

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


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

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 = {}) ⇒ 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.



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/sprockets/context.rb', line 185

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.



210
211
212
# File 'lib/sprockets/context.rb', line 210

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



110
111
112
113
114
115
116
117
# File 'lib/sprockets/context.rb', line 110

def depend_on(path)
  if environment.absolute_path?(path) && environment.directory?(path)
    @dependencies << environment.build_file_digest_uri(path)
  else
    resolve(path, compat: false)
  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 include recursively include the target asset’s dependencies.



126
127
128
# File 'lib/sprockets/context.rb', line 126

def depend_on_asset(path)
  load(resolve(path, compat: false))
end

#font_path(path) ⇒ Object

Expand logical font asset path.



215
216
217
# File 'lib/sprockets/context.rb', line 215

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

#image_path(path) ⇒ Object

Expand logical image asset path.



200
201
202
# File 'lib/sprockets/context.rb', line 200

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

#javascript_path(path) ⇒ Object

Expand logical javascript asset path.



220
221
222
# File 'lib/sprockets/context.rb', line 220

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.



157
158
159
160
161
# File 'lib/sprockets/context.rb', line 157

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.



98
99
100
101
102
# File 'lib/sprockets/context.rb', line 98

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

#metadataObject



43
44
45
46
47
48
# File 'lib/sprockets/context.rb', line 43

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


139
140
141
142
# File 'lib/sprockets/context.rb', line 139

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

#resolve_with_compat(path, options = {}) ⇒ Object Also known as: resolve

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"


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/sprockets/legacy.rb', line 239

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

#resolve_without_compatObject



256
# File 'lib/sprockets/legacy.rb', line 256

alias_method :resolve_without_compat, :resolve

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



147
148
149
150
# File 'lib/sprockets/context.rb', line 147

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

#stylesheet_path(path) ⇒ Object

Expand logical stylesheet asset path.



225
226
227
# File 'lib/sprockets/context.rb', line 225

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

#video_path(path) ⇒ Object

Expand logical video asset path.



205
206
207
# File 'lib/sprockets/context.rb', line 205

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