Class: Temple::Engine

Inherits:
Object
  • Object
show all
Extended by:
Mixins::EngineDSL
Includes:
Mixins::EngineDSL, Mixins::Options
Defined in:
lib/temple/engine.rb

Overview

An engine is simply a chain of compilers (that often includes a parser, some filters and a generator).

class MyEngine < Temple::Engine
  # First run MyParser, passing the :strict option
  use MyParser, :strict

  # Then a custom filter
  use MyFilter

  # Then some general optimizations filters
  filter :MultiFlattener
  filter :StaticMerger
  filter :DynamicInliner

  # Finally the generator
  generator :ArrayBuffer, :buffer
end

class SpecialEngine < MyEngine
  append MyCodeOptimizer
  before :ArrayBuffer, Temple::Filters::Validator
  replace :ArrayBuffer, Temple::Generators::RailsOutputBuffer
end

engine = MyEngine.new(:strict => "For MyParser")
engine.call(something)

Direct Known Subclasses

Temple::ERB::Engine

Instance Attribute Summary (collapse)

Attributes included from Mixins::Options

#options

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Mixins::EngineDSL

after, append, before, filter, generator, prepend, remove, replace

Methods included from Mixins::Options

included

Constructor Details

- (Engine) initialize(o = {})

Returns a new instance of Engine



42
43
44
45
46
# File 'lib/temple/engine.rb', line 42

def initialize(o = {})
  super
  @chain = self.class.chain.dup
  [*options[:chain]].compact.each {|block| block.call(self) }
end

Instance Attribute Details

- (Object) chain (readonly)

Returns the value of attribute chain



36
37
38
# File 'lib/temple/engine.rb', line 36

def chain
  @chain
end

Class Method Details

+ (Object) chain



38
39
40
# File 'lib/temple/engine.rb', line 38

def self.chain
  @chain ||= superclass.respond_to?(:chain) ? superclass.chain.dup : []
end

Instance Method Details

- (Object) call(input)



48
49
50
# File 'lib/temple/engine.rb', line 48

def call(input)
  call_chain.inject(input) {|m, e| e.call(m) }
end