Parent

Included Modules

Merb::Assets::AbstractAssetBundler

An abstract class for bundling text assets into single files.

Public Class Methods

add_callback(&block) click to toggle source

Parameters

&block

A block to add as a post-bundle callback.

Examples

  add_callback { |filename| `yuicompressor #{filename}` }
     # File lib/merb-assets/assets.rb, line 144
144:         def add_callback(&block)
145:           callbacks << block
146:         end
asset_type() click to toggle source

The type of asset for which the bundler is responsible. Override this method in your bundler code.

Raises

NotImplementedError

This method is implemented by the bundler.

Returns

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
cache_bundle(name) click to toggle source

Mark a bundle as cached.

Parameters

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
cached_bundle?(name) click to toggle source

Test if a bundle has been cached.

Parameters

name<~to_s>

Name of the bundle

Returns

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
callbacks() click to toggle source

Retrieve existing callbacks.

Returns

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
new(name, *files) click to toggle source

Parameters

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
purge_bundle(name) click to toggle source

Purge a bundle from the cache.

Parameters

name<~to_s>

Name of the bundle

     # File lib/merb-assets/assets.rb, line 124
124:         def purge_bundle(name)
125:           cached_bundles.delete(name.to_s)
126:         end

Public Instance Methods

bundle!() click to toggle source

Creates the new bundled file, executing all the callbacks.

Returns

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

Protected Instance Methods

bundle_files(filename, *files) click to toggle source

Bundle all the files into one.

Parameters

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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.