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 may 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.

Defined Under Namespace

Classes: CompositeProcessor

Constant Summary collapse

VALID_METADATA_VALUE_TYPES =

Internal: Set of all “simple” value types allowed to be returned in processor metadata.

Set.new([
  String,
  Symbol,
  TrueClass,
  FalseClass,
  NilClass
] + (0.class == Integer ? [Integer] : [Bignum, Fixnum])).freeze
VALID_METADATA_COMPOUND_TYPES =

Internal: Set of all nested compound metadata types that can nest values.

Set.new([
  Array,
  Hash,
  Set
]).freeze
VALID_METADATA_VALUE_TYPES_HASH =

Internal: Hash of all “simple” value types allowed to be returned in processor metadata.

VALID_METADATA_VALUE_TYPES.each_with_object({}) do |type, hash|
  hash[type] = true
end.freeze
VALID_METADATA_COMPOUND_TYPES_HASH =

Internal: Hash of all nested compound metadata types that can nest values.

VALID_METADATA_COMPOUND_TYPES.each_with_object({}) do |type, hash|
  hash[type] = true
end.freeze
VALID_METADATA_TYPES =

Internal: Set of all allowed metadata types.

(VALID_METADATA_VALUE_TYPES + VALID_METADATA_COMPOUND_TYPES).freeze

Instance Method Summary collapse

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sprockets/processor_utils.rb', line 80

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.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sprockets/processor_utils.rb', line 61

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.



46
47
48
# File 'lib/sprockets/processor_utils.rb', line 46

def compose_processors(*processors)
  CompositeProcessor.create processors
end

#processor_cache_key(processor) ⇒ Object

Internal: Get processor defined cached key.

processor - Processor function

Returns JSON serializable key or nil.



101
102
103
# File 'lib/sprockets/processor_utils.rb', line 101

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.



110
111
112
# File 'lib/sprockets/processor_utils.rb', line 110

def processors_cache_keys(processors)
  processors.map { |processor| processor_cache_key(processor) }
end

#valid_processor_metadata_value?(value) ⇒ Boolean

Internal: Validate object is in validate metadata whitelist.

value - Any Object

Returns true if class is in whitelist otherwise false.

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
# File 'lib/sprockets/processor_utils.rb', line 179

def (value)
  if VALID_METADATA_VALUE_TYPES_HASH[value.class]
    true
  elsif VALID_METADATA_COMPOUND_TYPES_HASH[value.class]
    value.all? { |v| (v) }
  else
    false
  end
end

#validate_processor_result!(result) ⇒ Object

Internal: Validate returned result of calling a processor pipeline and raise a friendly user error message.

result - Metadata Hash returned from call_processors

Returns result or raises a TypeError.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sprockets/processor_utils.rb', line 151

def validate_processor_result!(result)
  if !result.instance_of?(Hash)
    raise TypeError, "processor metadata result was expected to be a Hash, but was #{result.class}"
  end

  if !result[:data].instance_of?(String)
    raise TypeError, "processor :data was expected to be a String, but as #{result[:data].class}"
  end

  result.each do |key, value|
    if !key.instance_of?(Symbol)
      raise TypeError, "processor metadata[#{key.inspect}] expected to be a Symbol"
    end

    if !(value)
      raise TypeError, "processor metadata[:#{key}] returned a complex type: #{value.inspect}\n" +
        "Only #{VALID_METADATA_TYPES.to_a.join(", ")} maybe used."
    end
  end

  result
end