Class: Sprockets::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/context.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]
       = 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([:required])
  @stubbed          = Set.new([:stubbed])
  @links            = Set.new([:links])
  @dependency_paths = Set.new([:dependency_paths])
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

#_resolve(method, path, options = {}) ⇒ Object

Internal TODO: Cleanup relative resolver logic shared between directive processor.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sprockets/context.rb', line 74

def _resolve(method, path, options = {})
  options[:content_type] = self.content_type if options[:content_type] == :self
  options[:accept] = options.delete(:content_type)

  if environment.absolute_path?(path)
    filename = path
  elsif environment.relative_path?(path)
    path = File.expand_path(path, @dirname)
    if logical_path = @environment.split_subpath(load_path, path)
      if filename = environment.send(method, logical_path, options.merge(load_paths: [load_path]))
        accept = options[:accept]
        message = "couldn't find file '#{logical_path}' under '#{load_path}'"
        message << " with type '#{accept}'" if accept
        raise FileNotFound, message
      end
    else
      raise FileOutsidePaths, "#{path} isn't under path: #{load_path}"
    end
  else
    filename = environment.send(method, path, options)
  end

  if filename
    filename
  else
    accept = options[:accept]
    message = "couldn't find file '#{path}'"
    message << " with type '#{accept}'" if accept
    raise FileNotFound, message
  end
end

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


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

def asset_data_uri(path)
  depend_on_asset(path)
  asset = environment.find_asset(path, accept_encoding: 'base64')
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(asset.to_s)}"
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.



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/sprockets/context.rb', line 211

def asset_path(path, options = {})
  message = "Custom asset_path helper is not implemented\n\nExtend your environment context with a custom method.\n\nenvironment.context_class.class_eval do\ndef asset_path(path, options = {})\nend\nend\n"
  raise NotImplementedError, message
end

#audio_path(path) ⇒ Object

Expand logical audio asset path.



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

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.



135
136
137
138
# File 'lib/sprockets/context.rb', line 135

def depend_on(path)
  @dependency_paths << resolve(path).to_s
  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.



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

def depend_on_asset(path)
  if asset = @environment.load(locate(path))
    @dependency_paths.merge(asset.[:dependency_paths])
  end
  nil
end

#font_path(path) ⇒ Object

Expand logical font asset path.



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

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

#image_path(path) ⇒ Object

Expand logical image asset path.



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

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

#javascript_path(path) ⇒ Object

Expand logical javascript asset path.



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

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.



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

def link_asset(path)
  if asset = @environment.load(locate(path))
    @dependency_paths.merge(asset.[:dependency_paths])
    @links << asset.uri
  end
  asset
end

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



121
122
123
124
125
126
127
# File 'lib/sprockets/context.rb', line 121

def locate(path, options = {})
  if environment.valid_asset_uri?(path)
    path
  else
    _resolve(:locate, path, options)
  end
end

#metadataObject



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

def 
  { required: @required,
    stubbed: @stubbed,
    links: @links,
    dependency_paths: @dependency_paths }
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" %>


163
164
165
166
# File 'lib/sprockets/context.rb', line 163

def require_asset(path)
  @required << locate(path, accept: @content_type, bundle: false)
  nil
end

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

Given a logical path, `resolve` will find and return the fully expanded path. Relative paths will also be resolved. An optional `:content_type` restriction can be supplied to restrict the search.

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

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


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

def resolve(path, options = {})
  _resolve(:resolve, path, options)
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.



171
172
173
174
# File 'lib/sprockets/context.rb', line 171

def stub_asset(path)
  @stubbed << locate(path, accept: @content_type, bundle: false)
  nil
end

#stylesheet_path(path) ⇒ Object

Expand logical stylesheet asset path.



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

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

#video_path(path) ⇒ Object

Expand logical video asset path.



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

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