Object
An abstract class for bundling text assets into single files.
&block | A block to add as a post-bundle callback. |
add_callback { |filename| `yuicompressor #{filename}` }
# File lib/merb-assets/assets.rb, line 144 144: def add_callback(&block) 145: callbacks << block 146: 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 lib/merb-assets/assets.rb, line 166 166: def asset_type 167: raise NotImplementedError, "should return a symbol for the first argument to be passed to asset_path" 168: end
Mark a bundle as cached.
name<~to_s> | Name of the bundle |
# File lib/merb-assets/assets.rb, line 115 115: def cache_bundle(name) 116: cached_bundles.push(name.to_s) 117: end
Test if a bundle has been cached.
name<~to_s> | Name of the bundle |
Boolean | Whether the bundle has been cached or not. |
# File lib/merb-assets/assets.rb, line 135 135: def cached_bundle?(name) 136: cached_bundles.include?(name.to_s) 137: end
Retrieve existing callbacks.
Array[Proc] | An array of existing callbacks. |
# File lib/merb-assets/assets.rb, line 153 153: def callbacks 154: @callbacks ||= [] 155: return @callbacks 156: end
name<~to_s> | Name of the bundle. If name is true, it will be converted to :all. |
*files | Names of the files to bundle. |
# File lib/merb-assets/assets.rb, line 175 175: def initialize(name, *files) 176: @bundle_name = name == true ? :all : name 177: @bundle_filename = Merb.root / asset_path(self.class.asset_type, @bundle_name, true) 178: @files = files.map { |f| Merb.root / asset_path(self.class.asset_type, f, true) } 179: end
Creates the new bundled file, executing all the callbacks.
Symbol | Name of the bundle. |
# File lib/merb-assets/assets.rb, line 185 185: def bundle! 186: # TODO: push it out to the helper level so we don't have to create the helper object. 187: unless self.class.cached_bundle?(@bundle_name) 188: # skip regeneration of new bundled files - preventing multiple merb apps stepping on eachother 189: # file needs to be older than 60 seconds to be regenerated 190: if File.exist?(@bundle_filename) && File.mtime(@bundle_filename) >= Time.now - 60 191: return @bundle_name # serve the old file for now - to be regenerated later 192: end 193: bundle_files(@bundle_filename, *@files) 194: if File.exist?(@bundle_filename) 195: self.class.callbacks.each { |c| c.call(@bundle_filename) } 196: Merb.logger.info("Assets: bundled :#{@bundle_name} into #{File.basename(@bundle_filename)}") 197: self.class.cache_bundle(@bundle_name) 198: end 199: end 200: return @bundle_name 201: end
Bundle all the files into one.
filename | Name of the bundle file. |
*files | Filenames to be bundled. |
# File lib/merb-assets/assets.rb, line 212 212: def bundle_files(filename, *files) 213: File.open(filename, "w") do |f| 214: f.flock(File::LOCK_EX) 215: files.each { |file| f.puts(File.read(file)) } 216: f.flock(File::LOCK_UN) 217: end 218: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.