Sha256: b849c7758afc24e92b4ae942283b31d3a9ae6e70d805c971abf092e50e5290cf

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

#--
# Copyright 2006 Suraj N. Kurapati
# See the file named LICENSE for details.

require 'erb_content'

class ErbProxy
  attr_reader :handlers

  def initialize
    @handlers = {}
  end

  # Adds a new handler that can be invoked from a ERB template. The arguments
  # passed to the handler are:
  #
  # 1. buffer containing the evaluated results of the ERB template thus far
  #
  # 2. content that was passed to the handler from the ERB template
  #
  # 3. variable number of method arguments passed from the ERB template
  #
  def add_handler aName, &aHandler # :yields: buffer, content, *args
    @handlers[aName] = aHandler

    # XXX: define_method does not accept a block until Ruby 1.9
    instance_eval %{
      def #{aName} *args, &block
        raise ArgumentError unless block_given?

        args.unshift(*ERB.buffer_and_content(&block))
        @handlers[#{aName.inspect}].call(*args)
      end
    }
  end

  # Evaluates the given ERB template. Used to dynamically include one template
  # within another.
  def import aErbFile
    ERB.new(File.read(aErbFile)).result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-vpi-16.0.0 doc/lib/erb_proxy.rb
ruby-vpi-16.0.1 doc/lib/erb_proxy.rb