Sha256: 6c80c7ae4127af16c2f074bee6933f165d1e5a84ab3ef8dbea18df3cd9156d76

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

module Sprockets
  module Transformers
    # Public: Two level mapping of a source mime type to a target mime type.
    #
    #   environment.transformers
    #   # => { 'text/coffeescript' => {
    #            'application/javascript' => ConvertCoffeeScriptToJavaScript
    #          }
    #        }
    #
    attr_reader :transformers

    # 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.
    def register_transformer(from, to, proc)
      mutate_hash_config(:transformers, from) do |transformers|
        transformers.merge(to => proc)
      end
    end

    # Public: 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.
    def resolve_transform_type(type, accept = nil)
      find_best_mime_type_match(accept || '*/*', [type].compact + transformers[type].keys)
    end

    # Internal: Find and load transformer by from and to mime type.
    #
    # from - String mime type
    # to   - String mime type
    #
    # Returns Array of Procs.
    def unwrap_transformer(from, to)
      if processor = transformers[from][to]
        [unwrap_processor(processor)]
      else
        []
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sprockets-3.0.0.beta.3 lib/sprockets/transformers.rb
sprockets-3.0.0.beta.2 lib/sprockets/transformers.rb
sprockets-3.0.0.beta.1 lib/sprockets/transformers.rb