Class: Condenser::Context
- Inherits:
-
Object
- Object
- Condenser::Context
- Defined in:
- lib/condenser/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
-
#content_type ⇒ Object
readonly
Returns content type of file.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#load_path ⇒ Object
(also: #root_path)
readonly
Returns the environment path that contains the file.
-
#logical_path ⇒ Object
readonly
Returns logical path without any file extensions.
Instance Method Summary collapse
-
#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.
-
#asset_path(path, options = {}) ⇒ Object
Expands logical path to full url to asset.
-
#audio_path(path) ⇒ Object
Expand logical audio asset path.
-
#depend_on(path) ⇒ Object
‘depend_on` allows you to state a dependency on a file without including it.
-
#depend_on_env(key) ⇒ Object
‘depend_on_env` allows you to state a dependency on an environment variable.
- #env_proxy ⇒ Object
-
#font_path(path) ⇒ Object
Expand logical font asset path.
-
#image_path(path) ⇒ Object
Expand logical image asset path.
-
#initialize(environment) ⇒ Context
constructor
A new instance of Context.
-
#javascript_path(path) ⇒ Object
Expand logical javascript asset path.
-
#link_asset(path) ⇒ Object
‘link_asset` declares an external dependency on an asset without directly including it.
-
#load(uri) ⇒ Object
Public: Load Asset by AssetURI and track it as a dependency.
- #metadata ⇒ Object
-
#resolve(path, **kargs) ⇒ Object
Public: Given a logical path, ‘resolve` will find and return an Asset URI.
-
#stylesheet_path(path) ⇒ Object
Expand logical stylesheet asset path.
-
#video_path(path) ⇒ Object
Expand logical video asset path.
Constructor Details
#initialize(environment) ⇒ Context
Returns a new instance of Context.
40 41 42 43 44 |
# File 'lib/condenser/context.rb', line 40 def initialize(environment) @environment = environment @dependencies = Set.new @links = Set.new end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns content type of file
'application/javascript'
'text/css'
77 78 79 |
# File 'lib/condenser/context.rb', line 77 def content_type @content_type end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
38 39 40 |
# File 'lib/condenser/context.rb', line 38 def dependencies @dependencies end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
38 39 40 |
# File 'lib/condenser/context.rb', line 38 def environment @environment end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
38 39 40 |
# File 'lib/condenser/context.rb', line 38 def filename @filename end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
38 39 40 |
# File 'lib/condenser/context.rb', line 38 def links @links end |
#load_path ⇒ Object (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`.
62 63 64 |
# File 'lib/condenser/context.rb', line 62 def load_path @load_path end |
#logical_path ⇒ Object (readonly)
Returns logical path without any file extensions.
'app/javascripts/application.js'
# => 'application'
70 71 72 |
# File 'lib/condenser/context.rb', line 70 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' %>')
153 154 155 156 157 158 159 160 |
# File 'lib/condenser/context.rb', line 153 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.
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/condenser/context.rb', line 168 def asset_path(path, = {}) = "Custom asset_path helper is not implemented\n\nExtend your environment context with a custom method.\n\nenvironment.context_class.class_eval do\n def asset_path(path, options = {})\n end\nend\n EOS\n raise LoadError, message # Rack does not catch NotImplementedError\nend\n" |
#audio_path(path) ⇒ Object
Expand logical audio asset path.
193 194 195 |
# File 'lib/condenser/context.rb', line 193 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 that would invalidate the dependency asset will invalidate the source file.
116 117 118 119 120 121 |
# File 'lib/condenser/context.rb', line 116 def depend_on(path) d = environment.decompose_path(path) @dependencies << [File.join(*d[0], d[1]), [d[3]]] nil 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.
128 129 130 |
# File 'lib/condenser/context.rb', line 128 def depend_on_env(key) @dependencies << "env:#{key}" end |
#env_proxy ⇒ Object
53 54 55 |
# File 'lib/condenser/context.rb', line 53 def env_proxy ENVProxy.new(self) end |
#font_path(path) ⇒ Object
Expand logical font asset path.
198 199 200 |
# File 'lib/condenser/context.rb', line 198 def font_path(path) asset_path(path, type: :font) end |
#image_path(path) ⇒ Object
Expand logical image asset path.
183 184 185 |
# File 'lib/condenser/context.rb', line 183 def image_path(path) asset_path(path, type: :image) end |
#javascript_path(path) ⇒ Object
Expand logical javascript asset path.
203 204 205 |
# File 'lib/condenser/context.rb', line 203 def javascript_path(path) asset_path(path, type: :javascript) end |
#link_asset(path) ⇒ Object
‘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.
137 138 139 140 |
# File 'lib/condenser/context.rb', line 137 def link_asset(path) depend_on(path) @links << path end |
#load(uri) ⇒ Object
Public: Load Asset by AssetURI and track it as a dependency.
uri - AssetURI
Returns Asset.
104 105 106 107 108 |
# File 'lib/condenser/context.rb', line 104 def load(uri) asset = environment.load(uri) @dependencies.merge(asset.[:dependencies]) asset end |
#metadata ⇒ Object
46 47 48 49 50 51 |
# File 'lib/condenser/context.rb', line 46 def { links: @links, dependencies: @dependencies } 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.
93 94 95 96 97 |
# File 'lib/condenser/context.rb', line 93 def resolve(path, **kargs) uri, deps = environment.resolve!(path, @dirname, **kargs) @dependencies.merge(deps) uri end |
#stylesheet_path(path) ⇒ Object
Expand logical stylesheet asset path.
208 209 210 |
# File 'lib/condenser/context.rb', line 208 def stylesheet_path(path) asset_path(path, type: :stylesheet) end |
#video_path(path) ⇒ Object
Expand logical video asset path.
188 189 190 |
# File 'lib/condenser/context.rb', line 188 def video_path(path) asset_path(path, type: :video) end |