Class | Merb::Assets::AbstractAssetBundler |
In: |
merb-assets/lib/merb-assets/assets.rb
|
Parent: | Object |
An abstract class for bundling text assets into single files.
add_callback | -> | after_bundling |
&block: | A block to add as a post-bundle callback. |
add_callback { |filename| `yuicompressor #{filename}` }
# File merb-assets/lib/merb-assets/assets.rb, line 139 139: def add_callback(&block) 140: callbacks << block 141: end
The type of asset for which the bundler is responsible. Override this method in your bundler code.
NotImplementedError: | This method is implemented by the bundler. |
Symbol: | The type of the asset |
# File merb-assets/lib/merb-assets/assets.rb, line 161 161: def asset_type 162: raise NotImplementedError, "should return a symbol for the first argument to be passed to asset_path" 163: end
Mark a bundle as cached.
name<~to_s>: | Name of the bundle |
# File merb-assets/lib/merb-assets/assets.rb, line 110 110: def cache_bundle(name) 111: cached_bundles.push(name.to_s) 112: end
name<~to_s>: | Name of the bundle. If name is true, it will be converted to :all. |
*files<String>: | Names of the files to bundle. |
# File merb-assets/lib/merb-assets/assets.rb, line 170 170: def initialize(name, *files) 171: @bundle_name = name == true ? :all : name 172: @bundle_filename = Merb.root / asset_path(self.class.asset_type, @bundle_name, true) 173: @files = files.map { |f| Merb.root / asset_path(self.class.asset_type, f, true) } 174: end
Purge a bundle from the cache.
name<~to_s>: | Name of the bundle |
# File merb-assets/lib/merb-assets/assets.rb, line 119 119: def purge_bundle(name) 120: cached_bundles.delete(name.to_s) 121: end
Creates the new bundled file, executing all the callbacks.
Symbol: | Name of the bundle. |
# File merb-assets/lib/merb-assets/assets.rb, line 180 180: def bundle! 181: # TODO: push it out to the helper level so we don't have to create the helper object. 182: unless self.class.cached_bundle?(@bundle_name) 183: # skip regeneration of new bundled files - preventing multiple merb apps stepping on eachother 184: # file needs to be older than 60 seconds to be regenerated 185: if File.exist?(@bundle_filename) && File.mtime(@bundle_filename) >= Time.now - 60 186: return @bundle_name # serve the old file for now - to be regenerated later 187: end 188: bundle_files(@bundle_filename, *@files) 189: if File.exist?(@bundle_filename) 190: self.class.callbacks.each { |c| c.call(@bundle_filename) } 191: Merb.logger.info("Assets: bundled :#{@bundle_name} into #{File.basename(@bundle_filename)}") 192: self.class.cache_bundle(@bundle_name) 193: end 194: end 195: return @bundle_name 196: end
Bundle all the files into one.
filename<String>: | Name of the bundle file. |
*files<String>: | Filenames to be bundled. |
# File merb-assets/lib/merb-assets/assets.rb, line 207 207: def bundle_files(filename, *files) 208: File.open(filename, "w") do |f| 209: f.flock(File::LOCK_EX) 210: files.each { |file| f.puts(File.read(file)) } 211: f.flock(File::LOCK_UN) 212: end 213: end