Class: Merb::Assets::AbstractAssetBundler

Inherits:
Object
  • Object
show all
Includes:
AssetHelpers
Defined in:
lib/merb-assets/assets.rb

Overview

An abstract class for bundling text assets into single files.

Constant Summary

Constants included from AssetHelpers

Merb::Assets::AssetHelpers::ASSET_FILE_EXTENSIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AssetHelpers

#asset_path

Constructor Details

#initialize(name, *files) ⇒ AbstractAssetBundler

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.



170
171
172
173
174
# File 'lib/merb-assets/assets.rb', line 170

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

Class Method Details

.add_callback(&block) ⇒ Object Also known as: after_bundling

Parameters

&block

A block to add as a post-bundle callback.

Examples

add_callback { |filename| `yuicompressor #{filename}` }


139
140
141
# File 'lib/merb-assets/assets.rb', line 139

def add_callback(&block)
  callbacks << block
end

.asset_typeObject

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

Raises:

  • (NotImplementedError)


161
162
163
# File 'lib/merb-assets/assets.rb', line 161

def asset_type
  raise NotImplementedError, "should return a symbol for the first argument to be passed to asset_path"
end

.cache_bundle(name) ⇒ Object

Mark a bundle as cached.

Parameters

name<~to_s>

Name of the bundle



110
111
112
# File 'lib/merb-assets/assets.rb', line 110

def cache_bundle(name)
  cached_bundles.push(name.to_s)
end

.cached_bundle?(name) ⇒ Boolean

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.

Returns:

  • (Boolean)


130
131
132
# File 'lib/merb-assets/assets.rb', line 130

def cached_bundle?(name)
  cached_bundles.include?(name.to_s)
end

.callbacksObject

Retrieve existing callbacks.

Returns

Array

An array of existing callbacks.



148
149
150
151
# File 'lib/merb-assets/assets.rb', line 148

def callbacks
  @callbacks ||= []
  return @callbacks
end

.purge_bundle(name) ⇒ Object

Purge a bundle from the cache.

Parameters

name<~to_s>

Name of the bundle



119
120
121
# File 'lib/merb-assets/assets.rb', line 119

def purge_bundle(name)
  cached_bundles.delete(name.to_s)
end

Instance Method Details

#bundle!Object

Creates the new bundled file, executing all the callbacks.

Returns

Symbol

Name of the bundle.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/merb-assets/assets.rb', line 180

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