Module: TemplateEngine
- Included in:
- R6_Template, XamplGenerator::StandardGeneratorTemplates
- Defined in:
- lib/xamplr-gen/simpleTemplate/old/r6.000.rb,
lib/xamplr-gen/simpleTemplate/old/r6.001.rb,
lib/xamplr-gen/simpleTemplate/simple-template.rb
Overview
! /usr/bin/env ruby
Instance Attribute Summary collapse
-
#file_name_to_method ⇒ Object
Returns the value of attribute file_name_to_method.
-
#files ⇒ Object
Returns the value of attribute files.
-
#method_to_file_name ⇒ Object
Returns the value of attribute method_to_file_name.
Instance Method Summary collapse
-
#build_script(template_file_name, method_name) ⇒ Object
def macro(name, &block) Kernel.send(:define_method, name) { |*args| puts block.call(args) } end.
- #compile_scripts(files) ⇒ Object
- #initialize ⇒ Object
- #macro(name, &block) ⇒ Object
Instance Attribute Details
#file_name_to_method ⇒ Object
Returns the value of attribute file_name_to_method.
4 5 6 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.001.rb', line 4 def file_name_to_method @file_name_to_method end |
#files ⇒ Object
Returns the value of attribute files.
3 4 5 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.000.rb', line 3 def files @files end |
#method_to_file_name ⇒ Object
Returns the value of attribute method_to_file_name.
4 5 6 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.001.rb', line 4 def method_to_file_name @method_to_file_name end |
Instance Method Details
#build_script(template_file_name, method_name) ⇒ Object
def macro(name, &block)
Kernel.send(:define_method, name) { |*args|
puts block.call(args)
}
end
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.001.rb', line 17 def build_script(template_file_name, method_name) # Build the definition of a method (called 'method_name') that # will execute the template (in the file 'template_file_name'). There will # be an optional argument that defaults to the empty string (and so this # method will, by default, build a new string representing the result). If # the argument is supplied, it must respond to the "<<" (append) method. # # The result variable is available in the template. To write to the result # from Ruby code, result << sprintf("hello %s", "world") in the template # will get its output where expected. File.open(template_file_name) do | file | r = " def #{method_name}(result=\"\") tmp = "" result << \"\" " while line = file.gets if line[0] == ?| r << " #{line[1..-1]}" else r << " result << \"#{line.chomp.gsub("\"", "\\\"")}\\n\"\n" end end r << " result end " end end |
#compile_scripts(files) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.000.rb', line 43 def compile_scripts() files.each { | script_name | method_name = File::basename(script_name, ".*") the_script = build_script(script_name, method_name) puts the_script instance_eval the_script } end |
#initialize ⇒ Object
6 7 8 9 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.001.rb', line 6 def initialize @method_to_file_name = Hash.new() @file_name_to_method = Hash.new() end |
#macro(name, &block) ⇒ Object
5 6 7 8 9 |
# File 'lib/xamplr-gen/simpleTemplate/old/r6.000.rb', line 5 def macro(name, &block) Kernel.send(:define_method, name) { |*args| puts block.call(args) } end |