Sha256: 2ae22120c7a9cab3722623feb806446f0b8c0781da3247c9834200468d57cd83

Contents?: true

Size: 1000 Bytes

Versions: 7

Compression:

Stored size: 1000 Bytes

Contents

module Kontrol

  # This class renders an ERB template for a set of attributes, which
  # are accessible as instance variables.
  class Template
    include Helpers

    # Initialize this template with an ERB instance.
    def initialize(app)
      @__app__ = app
    end

    def __binding__
      binding
    end

    def self.render(erb, app, file, vars)
      template = Template.new(app)

      for name, value in vars
        template.send :instance_variable_set, "@#{name}", value
      end

      result = erb.result(template.__binding__)

      for name in template.instance_variables
        vars[name[1..-1]] = template.send(:instance_variable_get, name) unless name == '@__app__'
      end

      return result

    rescue => e
      e.backtrace.each do |s|
        s.gsub!('(erb)', file)
      end
      raise e
    end

    def method_missing(id, *args, &block)
      if @__app__.respond_to?(id)
        return @__app__.send(id, *args, &block)
      end
      super
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
georgi-kontrol-0.1.1 lib/kontrol/template.rb
georgi-kontrol-0.1.2 lib/kontrol/template.rb
georgi-kontrol-0.1.3 lib/kontrol/template.rb
georgi-kontrol-0.1.4 lib/kontrol/template.rb
georgi-kontrol-0.1.5 lib/kontrol/template.rb
georgi-kontrol-0.1.6 lib/kontrol/template.rb
georgi-kontrol-0.1 lib/kontrol/template.rb