Class Kwartz::Helper::RailsTemplate
In: kwartz/helper/rails.rb
Parent: Object

helper class to use Kwartz in Rails

How to use Kwartz in Rails:

  1. add the folliwng code in your ‘app/controllers/application.rb’.
      --------------------
      require 'kwartz/helper/rails'
      ActionView::Base.register_template_handler('html', Kwartz::Helper::RailsTemplate)
      #Kwartz::Helper::RailsTemplate.pdata_suffix  = '.html'
      #Kwartz::Helper::RailsTemplate.plogic_suffix = '.plogic'
      #Kwartz::Helper::RailsTemplate.default_properties = { :escape=>true }
      #Kwartz::Helper::RailsTemplate.debug = false
      --------------------
    
  2. restart web server.
  3. put template files ’*.html’ and ’*.plogic’ in ‘app/views/xxx’. layout files (‘app/views/layouts/xxx.{html,plogic}’) is also available.

Methods

Public Class methods

[Source]

# File kwartz/helper/rails.rb, line 104
      def self.add_cache(_ruby_code, _filename)
        _proc_obj = eval "proc do #{_ruby_code} end", binding(), _filename
        @@cache_table[_filename] = _proc_obj
        return _proc_obj
      end

[Source]

# File kwartz/helper/rails.rb, line 117
      def self.debug
        return @@debug
      end

[Source]

# File kwartz/helper/rails.rb, line 121
      def self.debug=(flag)
        @@debug = flag
      end

[Source]

# File kwartz/helper/rails.rb, line 44
      def self.default_properties
        return @@default_properties
      end

[Source]

# File kwartz/helper/rails.rb, line 48
      def self.default_properties=(hash)
        @@default_properties = hash
      end

[Source]

# File kwartz/helper/rails.rb, line 110
      def self.get_cache(filename)
        return @@cache_table[filename]
      end

[Source]

# File kwartz/helper/rails.rb, line 77
      def self.lang
        return @@lang
      end

[Source]

# File kwartz/helper/rails.rb, line 81
      def self.lang=(lang)
        case lang
        when 'rails', 'ruby', 'eruby', 'erubis', 'pierubis'
          @@lang = lang
        else
          raise "'#{lang}': invalid language name."
        end
      end

[Source]

# File kwartz/helper/rails.rb, line 126
      def initialize(view)
        @view = view
      end

[Source]

# File kwartz/helper/rails.rb, line 55
      def self.pdata_suffix
        return @@pdata_suffix
      end

[Source]

# File kwartz/helper/rails.rb, line 59
      def self.pdata_suffix=(suffix)
        @@pdata_suffix = suffix
      end

[Source]

# File kwartz/helper/rails.rb, line 66
      def self.plogic_suffix
        return @@plogic_suffix
      end

[Source]

# File kwartz/helper/rails.rb, line 70
      def self.plogic_suffix=(suffix)
        @@plogic_suffix = suffix
      end

[Source]

# File kwartz/helper/rails.rb, line 93
      def self.use_cache
        return @@use_cache
      end

[Source]

# File kwartz/helper/rails.rb, line 97
      def self.use_cache=(flag)
        @@use_cache = flag
      end

Public Instance methods

[Source]

# File kwartz/helper/rails.rb, line 131
      def render(template, assigns)

        ## reverse engineering
        #$stderr.puts "*** debug: render() called."
        #$stderr.puts "*** debug: RAILS_ENV=#{RAILS_ENV.inspect}"
        #$stderr.puts "*** debug: self.class=#{self.class}"
        #$stderr.puts "*** debug: @view.class=#{@view.class}"
        #$stderr.puts "*** debug: self.__id__=#{self.__id__.inspect}"
        #$stderr.puts "*** debug: @view.class.methods=#{(@view.class.methods - Class.methods).sort.inspect}"
        #$stderr.puts "*** debug: @view.controller.class.methods=#{(@view.controller.class.methods - Class.methods).sort.inspect}"
        #
        #$stderr.puts "*** debug: instance_variables=#{instance_variables.inspect}" #=> [@views]
        #c = @view.controller
        #$stderr.puts "*** debug: @view.controller.instance_variables=#{c.instance_variables.inspect}"
        #$stderr.puts "*** debug: @view.controller.action_name=#{c.action_name.inspect}"
        #$stderr.puts "*** debug: @view.controller.render_layout_basename=#{c.render_layout_basename.inspect}"
        #$stderr.puts "*** debug: @view.controller.render_template_basename=#{c.render_template_basename.inspect}"
        #require 'pp'
        #c = @view.controller
        #$stderr.puts "*** debug: @view.controller.instance_variable_get('@template')="
        #PP.pp(c.instance_variable_get('@template'), $stderr)
        #$stderr.puts "*** debug: @view.controller.methods="
        #PP.pp((c.methods - Object.new.methods).sort, $stderr)
        #$stderr.puts "*** debug: @view.controller.class.methods="
        #PP.pp((c.class.methods - Class.methods).sort, $stderr)

        ## return if @content_for_layout is set
        template_ = @view.controller.instance_variable_get("@template")
        content_for_layout_ = template_.instance_variable_get("@content_for_layout")
        return content_for_layout_ if content_for_layout_

        ## template basename and layout basename
        c = @view.controller
        template_root = c.template_root   # or c.class.template_root
        #template_basename = "#{template_root}/#{c.controller_name}/#{c.action_name}"
        #layout_basename   = "#{template_root}/layouts/#{c.controller_name}"
        template_basename = "#{template_root}/#{c.render_template_basename}"
        layout_basename   = "#{template_root}/#{c.render_layout_basename}"

        ## check timestamps
        convert_flag = true
        cache_filename = template_basename + '.cache'
        if use_cache? && test(?f, cache_filename)
          filenames = [
            template_basename + @@pdata_suffix,
            template_basename + @@plogic_suffix,
            layout_basename   + @@pdata_suffix,
            layout_basename   + @@plogic_suffix,
          ]
          mtime = File.mtime(cache_filename)
          convert_flag = filenames.any? { |filename|
            result = test(?f, filename) && File.mtime(filename) > mtime
          }
        end

        ## convert templates into ruby code, or get cached object
        logger = @view.controller.logger
        msgstr  = "template='#{template_basename}#{@@pdata_suffix}'"    if logger
        logname = "*** #{self.class.name}"                              if logger
        if convert_flag
          logger.info "#{logname}: convert template file: #{msgstr}"    if logger
          ruby_code = convert(template, template_basename, layout_basename)
          File.open(cache_filename, 'w') { |f| f.write(ruby_code) }  # write cache
          proc_obj = self.class.add_cache(ruby_code, cache_filename)
        elsif (proc_obj = self.class.get_cache(cache_filename)).nil?
          logger.info "#{logname}: read cache file: #{msgstr}"          if logger
          ruby_code = File.read(cache_filename)
          proc_obj = self.class.add_cache(ruby_code, cache_filename)
        else
          logger.info "#{logname}: reuse cached proc object: #{msgstr}" if logger
        end

        ## use @view as context object
        @view.__send__(:evaluate_assigns)
        #or @view.instance_eval("evaluate_assigns()")
        context = @view

        ## evaluate ruby code with context object
        if assigns && !assigns.empty?
          #return _evaluate_string(ruby_code, context, assigns)
          return evaluate_proc(proc_obj, context, assigns)
        else
          #return context.instance_eval(ruby_code)
          return context.instance_eval(&proc_obj)
        end
      end

[Validate]