Module: Kwartz::Defun
- Defined in:
- lib/kwartz/defun.rb
Overview
generate compiled code. see defun() method for usage.
Class Method Summary collapse
- .build_defun_code(basename, code, lang, properties) ⇒ Object
- .build_php_code(basename, php_code, properties) ⇒ Object
- .build_ruby_code(basename, ruby_code, properties) ⇒ Object
- .compile_into_lang_code(output, lang, properties) ⇒ Object
-
.defun(basename, output, lang, properties = {}) ⇒ Object
generate compiled code.
Class Method Details
.build_defun_code(basename, code, lang, properties) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kwartz/defun.rb', line 63 def build_defun_code(basename, code, lang, properties) case lang when 'ruby', 'eruby', 'rails', 'erubis', 'pierubis' return build_ruby_code(basename, code, properties) when 'php' return build_php_code(basename, code, properties) else raise "'#{lang}': not supported language." end end |
.build_php_code(basename, php_code, properties) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kwartz/defun.rb', line 103 def build_php_code(basename, php_code, properties) basename = basename.gsub(/[^\w]/, '_') s = <<END <?php function print_view_#{basename}($context) { explode($context); ?>#{php_code}<?php } function expand_view_#{basename}($context) { ob_start(); print_view_#{basename}($context); $output = ob_get_clean(); ob_end_clean(); return $output; } ?> END return s end |
.build_ruby_code(basename, ruby_code, properties) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/kwartz/defun.rb', line 75 def build_ruby_code(basename, ruby_code, properties) basename = basename.gsub(/[^\w]/, '_') module_name = properties.key?(:module) ? properties[:module] : 'View' method_verb = properties[:verb] || 'expand' method_name = properties[:method] || "#{method_verb}_#{basename}" s = '' s << "module #{module_name}\n" if module_name s << "\n" s << " (@@proc_table ||= {})['#{basename}'] = proc do\n" s << ruby_code << (!ruby_code.empty? && ruby_code[-1] != ?\n ? "\n" : '') s << " end#proc\n" s << "\n" s << " module_function\n" if module_name s << " def #{method_name}(context={})\n" s << " if context.is_a?(Hash)\n" s << " hash = context\n" s << " context = Object.new\n" s << " hash.each { |key, val| context.instance_variable_set(\"@\#{key}\", val) }\n" s << " end\n" s << " proc_obj = @@proc_table['#{basename}']\n" s << " context.instance_eval(&proc_obj)\n" s << " end\n" s << "\n" s << "end\n" if module_name return s end |
.compile_into_lang_code(output, lang, properties) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kwartz/defun.rb', line 41 def compile_into_lang_code(output, lang, properties) case lang when 'ruby' return output when 'eruby', 'rails' require 'erb' trim_mode = properties[:trim_mode] || (lang == 'eruby' ? 1 : '-') return ERB.new(output, nil, trim_mode).src when 'erubis' require 'erubis' return Erubis::Eruby.new(nil, properties).convert(output) when 'pierubis' require 'erubis' return Erubis::PI::Eruby.new(nil, properties).convert(output) when 'php' return output else raise "'#{lang}': not supported language." end end |
.defun(basename, output, lang, properties = {}) ⇒ Object
generate compiled code
ex.
args = %w[-l eruby -p ex.plogic ex.html]
eruby_src = Kwartz::Main.new(args).execute()
properties = { :trim_mode=>'>' }
code = Kwartz::Defun.defun('ex', eruby_src, 'eruby', properties)
print code
command-line properties:
--module=name : module name (default 'View')
--method=name : method name (default 'expand_xxx')
34 35 36 37 38 |
# File 'lib/kwartz/defun.rb', line 34 def defun(basename, output, lang, properties={}) lang_code = compile_into_lang_code(output, lang, properties) s = build_defun_code(basename, lang_code, lang, properties) return s end |