Class: Sprockets::Base
- Inherits:
-
Object
- Object
- Sprockets::Base
- Includes:
- Caching, Processing, Server, Trail
- 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.
-
#attributes_for(path) ⇒ Object
Internal.
-
#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.
-
#stat(path) ⇒ Object
Works like ‘File.stat`.
Methods included from Processing
#bundle_processors, #css_compressor, #css_compressor=, #format_extensions, #js_compressor, #js_compressor=, #postprocessors, #preprocessors, #processors, #register_bundle_processor, #register_engine, #register_mime_type, #register_postprocessor, #register_preprocessor, #register_processor, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor, #unregister_processor
Methods included from Engines
#engine_extensions, #engines, #register_engine
Methods included from Mime
#encoding_for_mime_type, #extension_for_mime_type, #mime_types, #register_mime_type
Methods included from Server
Methods included from Trail
#append_path, #clear_paths, #extensions, #paths, #prepend_path, #resolve, #root
Instance Attribute Details
#cache ⇒ Object
Get persistent cache store
89 90 91 |
# File 'lib/sprockets/base.rb', line 89 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
86 87 88 |
# File 'lib/sprockets/base.rb', line 86 def context_class @context_class end |
#default_external_encoding ⇒ Object
Define ‘default_external_encoding` accessor on 1.9. Defaults to UTF-8.
109 110 111 |
# File 'lib/sprockets/base.rb', line 109 def default_external_encoding @default_external_encoding end |
#digest_class ⇒ Object
Returns a ‘Digest` implementation class.
Defaults to ‘Digest::MD5`.
19 20 21 |
# File 'lib/sprockets/base.rb', line 19 def digest_class @digest_class end |
#logger ⇒ Object
Get and set ‘Logger` instance.
75 76 77 |
# File 'lib/sprockets/base.rb', line 75 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.
42 43 44 |
# File 'lib/sprockets/base.rb', line 42 def version @version end |
Instance Method Details
#[](*args) ⇒ Object
Preferred ‘find_asset` shorthand.
environment['application.js']
176 177 178 |
# File 'lib/sprockets/base.rb', line 176 def [](*args) find_asset(*args) end |
#attributes_for(path) ⇒ Object
Internal. Return a ‘AssetAttributes` for `path`.
144 145 146 |
# File 'lib/sprockets/base.rb', line 144 def attributes_for(path) AssetAttributes.new(self, path) end |
#content_type_of(path) ⇒ Object
Internal. Return content type of ‘path`.
149 150 151 |
# File 'lib/sprockets/base.rb', line 149 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.
64 65 66 67 68 69 70 71 72 |
# File 'lib/sprockets/base.rb', line 64 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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/sprockets/base.rb', line 180 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
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sprockets/base.rb', line 201 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
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/sprockets/base.rb', line 213 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.
115 116 117 |
# File 'lib/sprockets/base.rb', line 115 def entries(pathname) trail.entries(pathname) end |
#file_digest(path) ⇒ Object
Read and compute digest of filename.
Subclasses may cache this method.
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sprockets/base.rb', line 129 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.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/sprockets/base.rb', line 154 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.
102 103 104 |
# File 'lib/sprockets/base.rb', line 102 def index raise NotImplementedError end |
#inspect ⇒ Object
Pretty inspect
227 228 229 230 231 232 233 |
# File 'lib/sprockets/base.rb', line 227 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} " + "root=#{root.to_s.inspect}, " + "paths=#{paths.inspect}, " + "digest=#{digest.to_s.inspect}" + ">" end |
#stat(path) ⇒ Object
Works like ‘File.stat`.
Subclasses may cache this method.
122 123 124 |
# File 'lib/sprockets/base.rb', line 122 def stat(path) trail.stat(path) end |