Module: Sprockets::Transformers
- Includes:
- HTTPUtils, ProcessorUtils, Utils
- Included in:
- Configuration, Loader
- Defined in:
- lib/sprockets/transformers.rb
Constant Summary
Constants included from ProcessorUtils
ProcessorUtils::VALID_METADATA_COMPOUND_TYPES, ProcessorUtils::VALID_METADATA_TYPES, ProcessorUtils::VALID_METADATA_VALUE_TYPES
Constants included from Utils
Utils::UNBOUND_METHODS_BIND_TO_ANY_OBJECT
Instance Method Summary collapse
-
#compose_transformers(transformers, types) ⇒ Object
Internal: Compose multiple transformer steps into a single processor function.
-
#expand_transform_accepts(parsed_accepts) ⇒ Object
Internal: Expand accept type list to include possible transformed types.
-
#register_transformer(from, to, proc) ⇒ Object
Public: Register a transformer from and to a mime type.
-
#resolve_transform_type(type, accept) ⇒ Object
Internal: Resolve target mime type that the source type should be transformed to.
-
#transformers ⇒ Object
Public: Two level mapping of a source mime type to a target mime type.
Methods included from HTTPUtils
#find_best_mime_type_match, #find_best_q_match, #find_mime_type_matches, #find_q_matches, #match_mime_type?, #match_mime_type_keys, #parse_q_values
Methods included from ProcessorUtils
#call_processor, #call_processors, #compose_processors, #processor_cache_key, #processors_cache_keys, #valid_processor_metadata_value?, #validate_processor_result!
Methods included from Utils
#concat_javascript_sources, #dfs, #dfs_paths, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #normalize_extension, #string_end_with_semicolon?
Instance Method Details
#compose_transformers(transformers, types) ⇒ Object
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.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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) ⇒ Object
Internal: Expand accept type list to include possible transformed types.
parsed_accepts - Array of accept q values
Examples
([['application/javascript', 1.0]])
# => [['application/javascript', 1.0], ['text/coffeescript', 0.8]]
Returns an expanded Array of q values.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sprockets/transformers.rb', line 74 def (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) ⇒ 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.
35 36 37 38 39 40 |
# 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) ⇒ 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.
60 61 62 |
# 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 ⇒ Object
Public: Two level mapping of a source mime type to a target mime type.
environment.transformers
# => { 'text/coffeescript' => {
'application/javascript' => ConvertCoffeeScriptToJavaScript
}
}
17 18 19 |
# File 'lib/sprockets/transformers.rb', line 17 def transformers config[:transformers] end |