Module: Sprockets::Transformers

### Home Classes Methods

### In Files

- lib/sprockets/transformers.rb

### Methods

- #compose_transformers
- #expand_transform_accepts
- #register_transformer
- #resolve_transform_type
- #transformers

### Included Modules

- Sprockets::HTTPUtils
- Sprockets::ProcessorUtils
- Sprockets::Utils

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Transformers

### Public Instance Methods

compose_transformers(transformers, types) click to toggle source

Internal: Compose multiple transformer steps into a single processor function.

transformers - Two level Hash of a source mime type to a target mime type types - Array of mime type steps

Returns Processor.

    # File lib/sprockets/transformers.rb, line 92
    def compose_transformers(transformers, types)
      if types.length < 2
        raise ArgumentError, "too few transform types: #{types.inspect}"
      end

      i = 0
      processors = []

      loop do
        src = types[i]
        dst = types[i+1]
        break unless src && dst

        unless processor = transformers[src][dst]
          raise ArgumentError, "missing transformer for type: #{src} to #{dst}"
        end
        processors.concat config[:postprocessors][src]
        processors << processor
        processors.concat config[:preprocessors][dst]

        i += 1
      end

      if processors.size > 1
        compose_processors(*processors.reverse)
      elsif processors.size == 1
        processors.first
      end
    end

expand_transform_accepts(parsed_accepts) click to toggle source

Internal: Expand accept type list to include possible transformed types.

parsed_accepts - Array of accept q values

Examples

    expand_transform_accepts([['application/javascript', 1.0]])
    # => [['application/javascript', 1.0], ['text/coffeescript', 0.8]]

Returns an expanded Array of q values.

    # File lib/sprockets/transformers.rb, line 74
    def expand_transform_accepts(parsed_accepts)
      accepts = []
      parsed_accepts.each do |(type, q)|
        accepts.push([type, q])
        config[:inverted_transformers][type].each do |subtype|
          accepts.push([subtype, q * 0.8])
        end
      end
      accepts
    end

register_transformer(from, to, proc) click to toggle source

Public: Register a transformer from and to a mime type.

from - String mime type to - String mime type proc - Callable block that accepts an input Hash.

Examples

    register_transformer 'text/coffeescript', 'application/javascript',
      ConvertCoffeeScriptToJavaScript

    register_transformer 'image/svg+xml', 'image/png', ConvertSvgToPng

Returns nothing.

    # File lib/sprockets/transformers.rb, line 35
    def register_transformer(from, to, proc)
      self.config = hash_reassoc(config, :registered_transformers, from) do |transformers|
        transformers.merge(to => proc)
      end
      compute_transformers!
    end

resolve_transform_type(type, accept) click to toggle source

Internal: Resolve target mime type that the source type should be transformed to.

type - String from mime type accept - String accept type list (default: '/')

Examples

    resolve_transform_type('text/plain', 'text/plain')
    # => 'text/plain'

    resolve_transform_type('image/svg+xml', 'image/png, image/*')
    # => 'image/png'

    resolve_transform_type('text/css', 'image/png')
    # => nil

Returns String mime type or nil is no type satisfied the accept value.

    # File lib/sprockets/transformers.rb, line 60
    def resolve_transform_type(type, accept)
      find_best_mime_type_match(accept || '*/*', [type].compact + config[:transformers][type].keys)
    end

transformers() click to toggle source

Public: Two level mapping of a source mime type to a target mime type.

    environment.transformers
    # => { 'text/coffeescript' => {
             'application/javascript' => ConvertCoffeeScriptToJavaScript
           }
         }

    # File lib/sprockets/transformers.rb, line 17
    def transformers
      config[:transformers]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.