Sha256: 1377f9fde7c08a46a70c7ab61055e9df104d9c545b19f666111b36a8beb42785

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'malt/engines/abstract'

module Malt::Engine

  # Radius Template
  #
  #   http://github.com/jlong/radius/
  #
  class Radius < Abstract

    default :radius

    #
    def render(params, &yld)
      text   = params[:text]
      data   = params[:data]
      format = params[:format]
      case format
      when :html, :xml, nil
        data = make_context(data, &yld)
        opts = engine_options(params)
        parser = ::Radius::Parser.new(data, opts)
        parser.parse(text)
      else
        super(params, &yld)
      end
    end

    private

    # Load Radius library if not already loaded.
    def initialize_engine
      return if defined? ::Radius
      require_library 'radius'
    end

    # Radius templates have a very special data source.
    def make_context(data, &yld)
      case data
      when Hash
        context = make_context_from_hash(data, &yld)
      else
        if data.respond_to(:to_hash)
          data = data.to_hash
          context = make_context_from_hash(data, &yld)
        else
          context = make_context_from_object(data, &yld)
        end
      end
      context
    end

    #
    def make_context_from_object(db, &yld)
      context = Class.new(::Radius::Context).new
      db = make_object(db)
      (class << context; self; end).class_eval do
        define_method :tag_missing do |tag, attr|
          db.__send__(tag) # any way to support attr as args?
        end
      end
      context.define_tag("yield") do
        yld.call
      end
      context
    end

    #
    def make_context_from_hash(data, &yld)
      context = Class.new(::Radius::Context).new
      #data = make_hash(data)
      data.each do |tag, value|
        context.define_tag(tag){ value }
      end
      context.define_tag("yield") do
        yld.call
      end
      context
    end

    #
    def engine_options(params)
      opts = {}
      opts[:tag_prefix] = params[:tag_prefix] || settings[:tag_prefix]
      opts
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
malt-0.1.1 lib/malt/engines/radius.rb