Module: Sprockets::Transformers

Includes:
Utils
Included in:
Configuration, Loader
Defined in:
lib/sprockets/transformers.rb

Constant Summary

Constants included from Utils

Utils::UNBOUND_METHODS_BIND_TO_ANY_OBJECT

Instance Method Summary collapse

Methods included from Utils

#concat_javascript_sources, #dfs, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #normalize_extension, #string_end_with_semicolon?

Instance Method Details

#expand_transform_accepts(parsed_accepts) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
95
# File 'lib/sprockets/transformers.rb', line 86

def expand_transform_accepts(parsed_accepts)
  accepts = []
  parsed_accepts.each do |(type, q)|
    accepts.push([type, q])
    inverted_transformers[type].keys.each do |subtype|
      accepts.push([subtype, q * 0.8])
    end
  end
  accepts
end

#inverted_transformersObject

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

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


27
28
29
# File 'lib/sprockets/transformers.rb', line 27

def inverted_transformers
  config[:inverted_transformers]
end

#register_transformer(from, to, proc) ⇒ Object

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.



45
46
47
48
49
50
51
52
# File 'lib/sprockets/transformers.rb', line 45

def register_transformer(from, to, proc)
  self.config = hash_reassoc(config, :transformers, from) do |transformers|
    transformers.merge(to => proc)
  end
  self.config = hash_reassoc(config, :inverted_transformers, to) do |transformers|
    transformers.merge(from => proc)
  end
end

#resolve_transform_type(type, accept) ⇒ Object

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.



72
73
74
# File 'lib/sprockets/transformers.rb', line 72

def resolve_transform_type(type, accept)
  find_best_mime_type_match(accept || '*/*', [type].compact + transformers[type].keys)
end

#transformersObject

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

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


15
16
17
# File 'lib/sprockets/transformers.rb', line 15

def transformers
  config[:transformers]
end

#unwrap_transformer(from, to) ⇒ Object

Internal: Find and load transformer by from and to mime type.

from - String mime type to - String mime type

Returns Array of Procs.



103
104
105
106
107
108
109
# File 'lib/sprockets/transformers.rb', line 103

def unwrap_transformer(from, to)
  if processor = transformers[from][to]
    [unwrap_processor(processor)]
  else
    []
  end
end