class Palimpsest::Assets
Flexible asset pipeline using Sprockets. Paths are loaded into a ‘Sprockets::Environment` (relative to {#directory} if given). Asset tags are used in source code and replaced with generated asset path or compiled source if `inline` is used.
For example, if type is set to ‘:javascripts` the following replacements would be made:
[% javascript app %] -> app-9413c7f112033f0c6f2a8e8dd313399c18d93878.js [% javascript lib/jquery %] -> lib/jquery-e2a8cde3f5b3cdb011e38a673556c7a94729e0d1.js [% javascript inline tracking %] -> <compiled source of tracking.js asset>
Constants
- DEFAULT_OPTIONS
Default {#options}.
Attributes
@!attribute directory
@return [String] directory which all paths will be relative to if set
@!attribute paths
@return [Array] paths to load into sprockets environment
@!attribute type
@return [Symbol] type of asset
@!attribute directory
@return [String] directory which all paths will be relative to if set
@!attribute paths
@return [Array] paths to load into sprockets environment
@!attribute type
@return [Symbol] type of asset
@!attribute directory
@return [String] directory which all paths will be relative to if set
@!attribute paths
@return [Array] paths to load into sprockets environment
@!attribute type
@return [Symbol] type of asset
Public Class Methods
# File lib/palimpsest/assets.rb, line 59 def initialize directory: '', options: {}, paths: {} self.options options self.directory = directory self.paths = paths end
Public Instance Methods
@return [Sprockets::Environment] sprockets environment with {#options} and {#paths} loaded
# File lib/palimpsest/assets.rb, line 102 def assets unless @loaded load_options load_paths end @loaded = true sprockets end
Load options into the sprockets environment. Values are loaded from {#options}.
# File lib/palimpsest/assets.rb, line 80 def load_options options[:sprockets_options].each do |opt| sprockets.send "#{opt}=".to_sym, options[opt] if options[opt] end if options[:image_compression] Sprockets::ImageCompressor::Integration.setup sprockets end self end
Load paths into the sprockets environment. Values are loaded from {#paths}.
# File lib/palimpsest/assets.rb, line 94 def load_paths paths.each do |path| sprockets.append_path "#{directory + '/' unless directory.empty?}#{path}" end self end
Uses {DEFAULT_OPTIONS} as initial value. @param options [Hash] merged with current options @return [Hash] current options
# File lib/palimpsest/assets.rb, line 68 def options options={} @options ||= DEFAULT_OPTIONS @options = @options.merge options end
@return [Sprockets::Environment] the current sprockets environment
# File lib/palimpsest/assets.rb, line 74 def sprockets @sprockets ||= Sprockets::Environment.new end
Replaces all asset tags in source string with asset path or asset source. Writes any referenced assets to disk. @param source [String] code to find and replace asset tags @return [String] copy of ‘source` with asset tags replaced
# File lib/palimpsest/assets.rb, line 156 def update_source source s = source update_source! s s end
(see update_source
) @note this modifies the ‘source` `String` in place
# File lib/palimpsest/assets.rb, line 134 def update_source! source # e.g. /\[%\s+javascript\s+((\S+)\s?(\S+))\s+%\]/ regex = /#{Regexp.escape options[:src_pre]}\s+#{type.to_s.singularize}\s+((\S+)\s?(\S+))\s+#{Regexp.escape options[:src_post]}/ source.gsub! regex do if $2 == options[:inline] assets[$3].to_s else asset = write $1 # @todo raise warning or error if asset not found p "asset not found: #{$1}" and next if asset.nil? options[:cdn].empty? ? asset : options[:cdn] + asset end end return true end
Write a target asset to file with a hashed name. @param target [String] logical path to asset @param gzip [Boolean] if the asset should be gzipped @param hash [Boolean] if the asset name should include the hash @return [String, nil] the relative path to the written asset or ‘nil` if no such asset
# File lib/palimpsest/assets.rb, line 116 def write target, gzip: options[:gzip], hash: options[:hash] asset = assets[target] return if asset.nil? name = hash ? asset.digest_path : asset.logical_path.to_s name = "#{options[:output]}/#{name}" unless options[:output].empty? path = directory.empty? ? '' : "#{directory}/" path << name asset.write_to "#{path}.gz", compress: true if gzip asset.write_to path name end