class Malt::Engine::Radius

Radius Template

http://github.com/jlong/radius/

Public Instance Methods

prepare_engine(params={}, &content) click to toggle source
# File lib/malt/engines/radius.rb, line 30
def prepare_engine(params={}, &content)
  scope, locals = parameters(params, :scope, :locals)

  locals ||= {}

  # convert string keys to symbols w/o rewriting the hash
  string_keys = locals.keys.select{ |k| String === k }
  string_keys.each do |k|
    locals[k.to_sym] = locals[k]
    locals.delete(k)
  end

  make_context(scope, locals, &content)
end
render(params={}, &content) click to toggle source
Calls superclass method Malt::Engine::Abstract#render
# File lib/malt/engines/radius.rb, line 14
def render(params={}, &content)
  into, text = parameters(params, :to, :text)

  case into
  when :html, :xml, nil
    context = prepare_engine(params, &content)
    options = engine_options(params)

    parser = ::Radius::Parser.new(context, options)
    parser.parse(text)
  else
    super(params, &content)
  end
end

Private Instance Methods

engine_options(params) click to toggle source
# File lib/malt/engines/radius.rb, line 120
def engine_options(params)
  opts = {}
  opts[:tag_prefix] = params[:tag_prefix] || settings[:tag_prefix] #|| 'r'
  opts
end
make_context(scope, locals, &content) click to toggle source

Radius templates have a very special data source.

# File lib/malt/engines/radius.rb, line 54
def make_context(scope, locals, &content)
  case scope
  when nil
    context = make_context_from_hash(locals, &content)
  when Binding
    context = make_context_from_binding(scope, locals, &content)
  else
    context = make_context_from_object(scope, locals, &content)
  end
  context
end
make_context_from_binding(scope, locals, &content) click to toggle source
# File lib/malt/engines/radius.rb, line 67
def make_context_from_binding(scope, locals, &content)
  context_class = Class.new(::Radius::Context)
  context_class.class_eval do
    define_method :tag_missing do |tag, attr|
      if locals.key?(tag.to_sym)
        locals[tag.to_sym]
      else
        scope.eval(tag)
      end
    end
  end
  context = context_class.new
  context.define_tag("content") do
    content ? content.call : ''
  end
  context
end
make_context_from_hash(locals, &content) click to toggle source
# File lib/malt/engines/radius.rb, line 105
def make_context_from_hash(locals, &content)
  context_class = Class.new(::Radius::Context)
  context_class.class_eval do
    define_method :tag_missing do |tag, attr|
      locals[tag.to_sym]
    end
  end
  context = context_class.new
  context.define_tag("content") do
    content ? content.call : ''
  end
  context
end
make_context_from_object(scope, locals, &content) click to toggle source
# File lib/malt/engines/radius.rb, line 86
def make_context_from_object(scope, locals, &content)
  context_class = Class.new(::Radius::Context)
  context_class.class_eval do
    define_method :tag_missing do |tag, attr|
      if locals.key?(tag.to_sym)
        locals[tag.to_sym]
      else
        scope.__send__(tag) # any way to support attr as args?
      end
    end
  end
  context = context_class.new
  context.define_tag("content") do
    content ? content.call : ''
  end
  context
end
require_engine() click to toggle source

Load Radius library if not already loaded.

# File lib/malt/engines/radius.rb, line 48
def require_engine
  return if defined? ::Radius
  require_library 'radius'
end