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
def add_callback(&block)
  callbacks << block
end
Also aliased as: after_bundling
after_bundling(&block) click to toggle source
Alias for: add_callback
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
def asset_type
  raise NotImplementedError, "should return a symbol for the first argument to be passed to asset_path"
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
def cache_bundle(name)
  cached_bundles.push(name.to_s)
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
def cached_bundle?(name)
  cached_bundles.include?(name.to_s)
end
callbacks() click to toggle source

Retrieve existing callbacks.

Returns

Array

An array of existing callbacks.

# File lib/merb-assets/assets.rb, line 153
def callbacks
  @callbacks ||= []
  return @callbacks
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<String>

Names of the files to bundle.

# File lib/merb-assets/assets.rb, line 175
def initialize(name, *files)
  @bundle_name = name == true ? :all : name
  @bundle_filename = Merb.root / asset_path(self.class.asset_type, @bundle_name, true)
  @files = files.map { |f| Merb.root / asset_path(self.class.asset_type, f, true) }
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
def purge_bundle(name)
  cached_bundles.delete(name.to_s)
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
def bundle!
  # TODO: push it out to the helper level so we don't have to create the helper object.
  unless self.class.cached_bundle?(@bundle_name)
    # skip regeneration of new bundled files - preventing multiple merb apps stepping on eachother
    # file needs to be older than 60 seconds to be regenerated
    if File.exist?(@bundle_filename) && File.mtime(@bundle_filename) >= Time.now - 60
      return @bundle_name # serve the old file for now - to be regenerated later
    end
    bundle_files(@bundle_filename, *@files)
    if File.exist?(@bundle_filename)
      self.class.callbacks.each { |c| c.call(@bundle_filename) }
      Merb.logger.info("Assets: bundled :#{@bundle_name} into #{File.basename(@bundle_filename)}")
      self.class.cache_bundle(@bundle_name)
    end
  end
  return @bundle_name
end

Protected Instance Methods

bundle_files(filename, *files) click to toggle source

Bundle all the files into one.

Parameters

filename<String>

Name of the bundle file.

*files<String>

Filenames to be bundled.

# File lib/merb-assets/assets.rb, line 212
def bundle_files(filename, *files)
  File.open(filename, "w") do |f|
    f.flock(File::LOCK_EX)
    files.each { |file| f.puts(File.read(file)) }
    f.flock(File::LOCK_UN)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.