Class: Kwartz::Helper::RailsTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/kwartz/helper/rails.rb

Overview

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.

Constant Summary collapse

@@default_properties =
{ }
@@pdata_suffix =
'.html'
@@plogic_suffix =
'.plogic'
@@lang =

or ‘eruby’, ‘ruby’, ‘erubis’, ‘pierubis’

'rails'
@@use_cache =

true: use cache, false: don’t use cache, nil: use cache only when ENV == ‘production’

nil
@@cache_table =
{}
@@debug =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ RailsTemplate

Returns a new instance of RailsTemplate.



126
127
128
# File 'lib/kwartz/helper/rails.rb', line 126

def initialize(view)
  @view = view
end

Class Method Details

.add_cache(_ruby_code, _filename) ⇒ Object



104
105
106
107
108
# File 'lib/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

.debugObject



117
118
119
# File 'lib/kwartz/helper/rails.rb', line 117

def self.debug
  return @@debug
end

.debug=(flag) ⇒ Object



121
122
123
# File 'lib/kwartz/helper/rails.rb', line 121

def self.debug=(flag)
  @@debug = flag
end

.default_propertiesObject



44
45
46
# File 'lib/kwartz/helper/rails.rb', line 44

def self.default_properties
  return @@default_properties
end

.default_properties=(hash) ⇒ Object



48
49
50
# File 'lib/kwartz/helper/rails.rb', line 48

def self.default_properties=(hash)
  @@default_properties = hash
end

.get_cache(filename) ⇒ Object



110
111
112
# File 'lib/kwartz/helper/rails.rb', line 110

def self.get_cache(filename)
  return @@cache_table[filename]
end

.langObject



77
78
79
# File 'lib/kwartz/helper/rails.rb', line 77

def self.lang
  return @@lang
end

.lang=(lang) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/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

.pdata_suffixObject



55
56
57
# File 'lib/kwartz/helper/rails.rb', line 55

def self.pdata_suffix
  return @@pdata_suffix
end

.pdata_suffix=(suffix) ⇒ Object



59
60
61
# File 'lib/kwartz/helper/rails.rb', line 59

def self.pdata_suffix=(suffix)
  @@pdata_suffix = suffix
end

.plogic_suffixObject



66
67
68
# File 'lib/kwartz/helper/rails.rb', line 66

def self.plogic_suffix
  return @@plogic_suffix
end

.plogic_suffix=(suffix) ⇒ Object



70
71
72
# File 'lib/kwartz/helper/rails.rb', line 70

def self.plogic_suffix=(suffix)
  @@plogic_suffix = suffix
end

.use_cacheObject



93
94
95
# File 'lib/kwartz/helper/rails.rb', line 93

def self.use_cache
  return @@use_cache
end

.use_cache=(flag) ⇒ Object



97
98
99
# File 'lib/kwartz/helper/rails.rb', line 97

def self.use_cache=(flag)
  @@use_cache = flag
end

Instance Method Details

#render(template, assigns) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/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