module RocketIO class Controller # if no engine set, templates will be rendered using ERB engine. # any engine supported by [Tilt](github.com/rtomayko/tilt) can be used. # to set engine use symbolized constant name, e.g. :Slim, :Haml # engine name is Case Sensitive and there should be a Tilt::{ENGINE}Template class defined # e.g. `engine :Slim` will look for Tilt::SlimTemplate # and `engine RDiscount` will look for Tilt::RDiscountTemplate # # @note if a block given it will be executed at instance level # and result used for engine. To have any options passed at engine initialization # the block should return an array having engine as first element # and options as consequent elements. # # @note if given block returns no engine, inherited engine will be used # # @example use static engine # engine :Slim # # @example use static engine with options # engine :Slim, pretty_print: true # # @example use dynamically set engine # engine do # some_condition ? :SomeEngine : AnotherEngine # end # # @example use dynamically set engine with options # engine do # some_condition ? [:SomeEngine, :opt1, :opt2] : AnotherEngine # end # # @example Search will use ERB when requested by a bot and Slim otherwise # # class BaseController < RocketIO # engine :Slim # end # # class Search < BaseController # engine do # if request.user_agent =~ /i'm a bot/ # # requested by a bot, using ERB # :ERB # end # # requested by a user, returning no engine for inherited engine to be used # end # end # # @param engine engine name. # @param *engine_options any arguments to be passed at engine initialization # @param block to be executed at instance level # def self.engine engine = nil, *engine_options, &block @__engine__ = block || [RocketIO.engine_class(engine), engine_options.freeze].freeze define_engine_methods end def self.define_engine_methods source = self return unless engine = source.instance_variable_get(:@__engine__) if Proc === engine selfengine = allocate.engine private_api << define_method(:__rocketio_engine__, &engine) private_api << define_method(:engine) { engine, *engine_options = __rocketio_engine__ return selfengine unless engine [RocketIO.engine_class(engine), engine_options.freeze].freeze } else private_api << define_method(:engine) {engine} end end def engine; RocketIO::DEFAULT_ENGINE end end end