Class: Sprockets::Base
- Inherits:
-
Object
- Object
- Sprockets::Base
- Defined in:
- lib/sprockets/base.rb
Overview
‘Base` class for `Environment` and `Index`.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cache ⇒ Object
Get persistent cache store.
-
#context_class ⇒ Object
readonly
Get ‘Context` class.
-
#default_external_encoding ⇒ Object
Define ‘default_external_encoding` accessor on 1.9.
-
#digest_class ⇒ Object
Returns a ‘Digest` implementation class.
-
#logger ⇒ Object
Get and set ‘Logger` instance.
-
#version ⇒ Object
The ‘Environment#version` is a custom value used for manually expiring all asset caches.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Preferred ‘find_asset` shorthand.
- #append_path(path) ⇒ Object
-
#attributes_for(path) ⇒ Object
Internal.
- #clear_paths ⇒ Object
-
#content_type_of(path) ⇒ Object
Internal.
-
#digest ⇒ Object
Returns a ‘Digest` instance for the `Environment`.
- #each_entry(root, &block) ⇒ Object
- #each_file ⇒ Object
- #each_logical_path(*args) ⇒ Object
-
#entries(pathname) ⇒ Object
Works like ‘Dir.entries`.
-
#file_digest(path) ⇒ Object
Read and compute digest of filename.
-
#find_asset(path, options = {}) ⇒ Object
Find asset by logical path or expanded path.
-
#index ⇒ Object
Return an ‘Index`.
-
#inspect ⇒ Object
Pretty inspect.
- #prepend_path(path) ⇒ Object
- #register_bundle_processor(mime_type, klass, &block) ⇒ Object
-
#register_engine(ext, klass) ⇒ Object
Registers a new Engine ‘klass` for `ext`.
-
#register_mime_type(mime_type, ext) ⇒ Object
Register a new mime type.
- #register_postprocessor(mime_type, klass, &block) ⇒ Object
- #register_preprocessor(mime_type, klass, &block) ⇒ Object
-
#resolve(logical_path, options = {}) ⇒ Object
Finds the expanded real path for a given logical path by searching the environment’s paths.
-
#stat(path) ⇒ Object
Works like ‘File.stat`.
- #unregister_bundle_processor(mime_type, klass) ⇒ Object
- #unregister_postprocessor(mime_type, klass) ⇒ Object
- #unregister_preprocessor(mime_type, klass) ⇒ Object
Methods included from Caching
Methods included from Paths
Methods included from Mime
#encoding_for_mime_type, #extension_for_mime_type, #mime_types, #registered_mime_types
Methods included from Processing
#bundle_processors, #css_compressor, #css_compressor=, #format_extensions, #js_compressor, #js_compressor=, #postprocessors, #preprocessors, #processors, #register_processor, #unregister_processor
Methods included from Engines
Methods included from Server
Instance Attribute Details
#cache ⇒ Object
Get persistent cache store
88 89 90 |
# File 'lib/sprockets/base.rb', line 88 def cache @cache end |
#context_class ⇒ Object (readonly)
Get ‘Context` class.
This class maybe mutated and mixed in with custom helpers.
environment.context_class.instance_eval do
include MyHelpers
def asset_url; end
end
85 86 87 |
# File 'lib/sprockets/base.rb', line 85 def context_class @context_class end |
#default_external_encoding ⇒ Object
Define ‘default_external_encoding` accessor on 1.9. Defaults to UTF-8.
200 201 202 |
# File 'lib/sprockets/base.rb', line 200 def default_external_encoding @default_external_encoding end |
#digest_class ⇒ Object
Returns a ‘Digest` implementation class.
Defaults to ‘Digest::MD5`.
18 19 20 |
# File 'lib/sprockets/base.rb', line 18 def digest_class @digest_class end |
#logger ⇒ Object
Get and set ‘Logger` instance.
74 75 76 |
# File 'lib/sprockets/base.rb', line 74 def logger @logger end |
#version ⇒ Object
The ‘Environment#version` is a custom value used for manually expiring all asset caches.
Sprockets is able to track most file and directory changes and will take care of expiring the cache for you. However, its impossible to know when any custom helpers change that you mix into the ‘Context`.
It would be wise to increment this value anytime you make a configuration change to the ‘Environment` object.
41 42 43 |
# File 'lib/sprockets/base.rb', line 41 def version @version end |
Instance Method Details
#[](*args) ⇒ Object
Preferred ‘find_asset` shorthand.
environment['application.js']
267 268 269 |
# File 'lib/sprockets/base.rb', line 267 def [](*args) find_asset(*args) end |
#append_path(path) ⇒ Object
106 107 108 109 110 |
# File 'lib/sprockets/base.rb', line 106 def append_path(path) # Overrides the global behavior to expire the index expire_index! super end |
#attributes_for(path) ⇒ Object
Internal. Return a ‘AssetAttributes` for `path`.
235 236 237 |
# File 'lib/sprockets/base.rb', line 235 def attributes_for(path) AssetAttributes.new(self, path) end |
#clear_paths ⇒ Object
112 113 114 115 116 |
# File 'lib/sprockets/base.rb', line 112 def clear_paths # Overrides the global behavior to expire the index expire_index! super end |
#content_type_of(path) ⇒ Object
Internal. Return content type of ‘path`.
240 241 242 |
# File 'lib/sprockets/base.rb', line 240 def content_type_of(path) attributes_for(path).content_type end |
#digest ⇒ Object
Returns a ‘Digest` instance for the `Environment`.
This value serves two purposes. If two ‘Environment`s have the same digest value they can be treated as equal. This is more useful for comparing environment states between processes rather than in the same. Two equal `Environment`s can share the same cached assets.
The value also provides a seed digest for all ‘Asset` digests. Any change in the environment digest will affect all of its assets.
63 64 65 66 67 68 69 70 71 |
# File 'lib/sprockets/base.rb', line 63 def digest # Compute the initial digest using the implementation class. The # Sprockets release version and custom environment version are # mixed in. So any new releases will affect all your assets. @digest ||= digest_class.new.update(VERSION).update(version.to_s) # Returned a dupped copy so the caller can safely mutate it with `.update` @digest.dup end |
#each_entry(root, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/sprockets/base.rb', line 271 def each_entry(root, &block) return to_enum(__method__, root) unless block_given? root = Pathname.new(root) unless root.is_a?(Pathname) paths = [] entries(root).sort.each do |filename| path = root.join(filename) paths << path if stat(path).directory? each_entry(path) do |subpath| paths << subpath end end end paths.sort_by(&:to_s).each(&block) nil end |
#each_file ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/sprockets/base.rb', line 292 def each_file return to_enum(__method__) unless block_given? paths.each do |root| each_entry(root) do |path| if !stat(path).directory? yield path end end end nil end |
#each_logical_path(*args) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/sprockets/base.rb', line 304 def each_logical_path(*args) return to_enum(__method__, *args) unless block_given? filters = args.flatten files = {} each_file do |filename| if logical_path = logical_path_for_filename(filename, filters) yield logical_path unless files[logical_path] files[logical_path] = true end end nil end |
#entries(pathname) ⇒ Object
Works like ‘Dir.entries`.
Subclasses may cache this method.
206 207 208 |
# File 'lib/sprockets/base.rb', line 206 def entries(pathname) @trail.entries(pathname) end |
#file_digest(path) ⇒ Object
Read and compute digest of filename.
Subclasses may cache this method.
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/sprockets/base.rb', line 220 def file_digest(path) if stat = self.stat(path) # If its a file, digest the contents if stat.file? digest.file(path.to_s) # If its a directive, digest the list of filenames elsif stat.directory? contents = self.entries(path).join(',') digest.update(contents) end end end |
#find_asset(path, options = {}) ⇒ Object
Find asset by logical path or expanded path.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/sprockets/base.rb', line 245 def find_asset(path, = {}) logical_path = path pathname = Pathname.new(path) if pathname.absolute? return unless stat(pathname) logical_path = attributes_for(pathname).logical_path else begin pathname = resolve(logical_path) rescue FileNotFound return nil end end build_asset(logical_path, pathname, ) end |
#index ⇒ Object
Return an ‘Index`. Must be implemented by the subclass.
193 194 195 |
# File 'lib/sprockets/base.rb', line 193 def index raise NotImplementedError end |
#inspect ⇒ Object
Pretty inspect
318 319 320 321 322 323 324 |
# File 'lib/sprockets/base.rb', line 318 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} " + "root=#{root.to_s.inspect}, " + "paths=#{paths.inspect}, " + "digest=#{digest.to_s.inspect}" + ">" end |
#prepend_path(path) ⇒ Object
100 101 102 103 104 |
# File 'lib/sprockets/base.rb', line 100 def prepend_path(path) # Overrides the global behavior to expire the index expire_index! super end |
#register_bundle_processor(mime_type, klass, &block) ⇒ Object
180 181 182 183 184 |
# File 'lib/sprockets/base.rb', line 180 def register_bundle_processor(mime_type, klass, &block) # Overrides the global behavior to expire the index expire_index! super end |
#register_engine(ext, klass) ⇒ Object
Registers a new Engine ‘klass` for `ext`.
149 150 151 152 153 154 |
# File 'lib/sprockets/base.rb', line 149 def register_engine(ext, klass) # Overrides the global behavior to expire the index expire_index! add_engine_to_trail(ext, klass) super end |
#register_mime_type(mime_type, ext) ⇒ Object
Register a new mime type.
141 142 143 144 145 146 |
# File 'lib/sprockets/base.rb', line 141 def register_mime_type(mime_type, ext) # Overrides the global behavior to expire the index expire_index! @trail.append_extension(ext) super end |
#register_postprocessor(mime_type, klass, &block) ⇒ Object
168 169 170 171 172 |
# File 'lib/sprockets/base.rb', line 168 def register_postprocessor(mime_type, klass, &block) # Overrides the global behavior to expire the index expire_index! super end |
#register_preprocessor(mime_type, klass, &block) ⇒ Object
156 157 158 159 160 |
# File 'lib/sprockets/base.rb', line 156 def register_preprocessor(mime_type, klass, &block) # Overrides the global behavior to expire the index expire_index! super end |
#resolve(logical_path, options = {}) ⇒ Object
Finds the expanded real path for a given logical path by searching the environment’s paths.
resolve("application.js")
# => "/path/to/app/javascripts/application.js.coffee"
A ‘FileNotFound` exception is raised if the file does not exist.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sprockets/base.rb', line 125 def resolve(logical_path, = {}) # If a block is given, preform an iterable search if block_given? args = attributes_for(logical_path).search_paths + [] @trail.find(*args) do |path| yield Pathname.new(path) end else resolve(logical_path, ) do |pathname| return pathname end raise FileNotFound, "couldn't find file '#{logical_path}'" end end |
#stat(path) ⇒ Object
Works like ‘File.stat`.
Subclasses may cache this method.
213 214 215 |
# File 'lib/sprockets/base.rb', line 213 def stat(path) @trail.stat(path) end |
#unregister_bundle_processor(mime_type, klass) ⇒ Object
186 187 188 189 190 |
# File 'lib/sprockets/base.rb', line 186 def unregister_bundle_processor(mime_type, klass) # Overrides the global behavior to expire the index expire_index! super end |
#unregister_postprocessor(mime_type, klass) ⇒ Object
174 175 176 177 178 |
# File 'lib/sprockets/base.rb', line 174 def unregister_postprocessor(mime_type, klass) # Overrides the global behavior to expire the index expire_index! super end |
#unregister_preprocessor(mime_type, klass) ⇒ Object
162 163 164 165 166 |
# File 'lib/sprockets/base.rb', line 162 def unregister_preprocessor(mime_type, klass) # Overrides the global behavior to expire the index expire_index! super end |