Method: ProcessIt::Context#evaluate

Defined in:
lib/processit/context.rb

#evaluate(path, options = {}) ⇒ Object

Reads ‘path` and runs processors on the file.

This allows you to capture the result of an asset and include it directly in another.

<%= evaluate "bar.js" %>


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/processit/context.rb', line 173

def evaluate(path, options = {})
  pathname   = resolve(path)
  attributes = environment.attributes_for(pathname)
  processors = options[:processors] || attributes.processors

  if options[:data]
    result = options[:data]
  else
    if environment.respond_to?(:default_external_encoding)
      mime_type = environment.mime_types(pathname.extname)
      encoding  = environment.encoding_for_mime_type(mime_type)
      result    = Sprockets::Utils.read_unicode(pathname, encoding)
    else
      result = Sprockets::Utils.read_unicode(pathname)
    end
  end

  processors.each do |processor|

    begin
      template = processor.new(pathname.to_s) { result }
      result = template.render(self, {})
    rescue Exception => e
      annotate_exception! e
      raise
    end
  end

  result
end