Module: Sprockets::ProcessorUtils
- Extended by:
- ProcessorUtils
- Included in:
- Sprockets, Loader, Processing, ProcessorUtils, Transformers
- Defined in:
- lib/sprockets/processor_utils.rb
Overview
Functional utilities for dealing with Processor functions.
A Processor is a general function that my modify or transform an asset as part of the pipeline. CoffeeScript to JavaScript conversion, Minification or Concatenation are all implemented as seperate Processor steps.
Processors maybe any object that responds to call. So procs or a class that defines a self.call method.
For ergonomics, processors may return a number of shorthand values. Unfortunately, this means that processors can not compose via ordinary function composition. The composition helpers here can help.
Instance Method Summary collapse
-
#call_processor(processor, input) ⇒ Object
Public: Invoke processor.
-
#call_processors(processors, input) ⇒ Object
Public: Invoke list of processors in right to left order.
-
#compose_processors(*processors) ⇒ Object
Public: Compose processors in right to left order.
-
#processor_cache_key(processor) ⇒ Object
Internal: Get processor defined cached key.
-
#processors_cache_keys(processors) ⇒ Object
Internal: Get combined cache keys for set of processors.
Instance Method Details
#call_processor(processor, input) ⇒ Object
Public: Invoke processor.
processor - Processor callables input - Hash of input data to pass to processor
Returns a Hash with :data and other processor metadata key/values.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sprockets/processor_utils.rb', line 69 def call_processor(processor, input) = (input[:metadata] || {}).dup [:data] = input[:data] case result = processor.call({data: "", metadata: {}}.merge(input)) when NilClass when Hash .merge(result) when String .merge(data: result) else raise TypeError, "invalid processor return type: #{result.class}" end end |
#call_processors(processors, input) ⇒ Object
Public: Invoke list of processors in right to left order.
The right to left order processing mirrors standard function composition. Think about:
bundle.call(uglify.call(coffee.call(input)))
processors - Array of processor callables input - Hash of input data to pass to each processor
Returns a Hash with :data and other processor metadata key/values.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sprockets/processor_utils.rb', line 50 def call_processors(processors, input) data = input[:data] || "" = (input[:metadata] || {}).dup processors.reverse_each do |processor| result = call_processor(processor, input.merge(data: data, metadata: )) data = result.delete(:data) .merge!(result) end .merge(data: data) end |
#compose_processors(*processors) ⇒ Object
Public: Compose processors in right to left order.
processors - Array of processors callables
Returns a composed Proc.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sprockets/processor_utils.rb', line 22 def compose_processors(*processors) context = self if processors.length == 1 obj = method(:call_processor).to_proc.curry[processors.first] else obj = method(:call_processors).to_proc.curry[processors] end = (class << obj; self; end) .send(:define_method, :cache_key) do context.processors_cache_keys(processors) end obj end |
#processor_cache_key(processor) ⇒ Object
Internal: Get processor defined cached key.
processor - Processor function
Returns JSON serializable key or nil.
90 91 92 |
# File 'lib/sprockets/processor_utils.rb', line 90 def processor_cache_key(processor) processor.cache_key if processor.respond_to?(:cache_key) end |
#processors_cache_keys(processors) ⇒ Object
Internal: Get combined cache keys for set of processors.
processors - Array of processor functions
Returns Array of JSON serializable keys.
99 100 101 |
# File 'lib/sprockets/processor_utils.rb', line 99 def processors_cache_keys(processors) processors.map { |processor| processor_cache_key(processor) } end |