Class: Temple::Engine

Inherits:
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, prepend, remove, replace

Methods included from Mixins::Options

included

Constructor Details

#initialize(opts = {}) ⇒ Engine

Returns a new instance of Engine.



45
46
47
48
# File 'lib/temple/engine.rb', line 45

def initialize(opts = {})
  super
  @chain = self.class.chain.dup
end

Instance Attribute Details

#chainObject (readonly)



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

def chain
  @chain
end

Class Method Details

.chainObject



41
42
43
# File 'lib/temple/engine.rb', line 41

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

Instance Method Details

#call(input) ⇒ Object



50
51
52
# File 'lib/temple/engine.rb', line 50

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