Module: Kwatable::CommonHelper
- Defined in:
- lib/kwatable/template/helper/common.rb
Instance Method Summary collapse
-
#apply_subtemplate_for_each_tables(tables, opthash = {}, &block) ⇒ Object
apply subtemplate for each tables.
-
#camel_case(name, flag_all = true) ⇒ Object
convert ‘foo_bar_baz’ into ‘FooBarBaz’.
- #context_var_required(var_name) ⇒ Object
- #each_table(tables, &block) ⇒ Object
-
#encode(str, encoding) ⇒ Object
encode string.
-
#eval_template_with_custom_code(template_filename, context, output_filename, options = {}) ⇒ Object
read user custom code from output filename and apply template with it.
-
#find_file(filename, path_list, find_in_currdir = true) ⇒ Object
find file from path_list.
-
#find_subtemplate(template_filename = @template_filename, template_pathlist = @template_pathlist) ⇒ Object
find subtemplate.
-
#q(obj) ⇒ Object
quote by single-quoation.
-
#user_custom_code(content) ⇒ Object
get user custom code.
Instance Method Details
#apply_subtemplate_for_each_tables(tables, opthash = {}, &block) ⇒ Object
apply subtemplate for each tables
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/kwatable/template/helper/common.rb', line 128 def apply_subtemplate_for_each_tables(tables, opthash={}, &block) subtemplate_filepath = find_subtemplate() subtemplate = File.read(subtemplate_filepath) encoding = @properties[:encoding] || @properties[:charset] context = self output_files = [] opts = { :encoding=>encoding, :template=>subtemplate } each_table(@tables) do |table| output_filename = yield(table) dir = @options[?d] output_filename = "#{dir}/#{output_filename}" if dir @table = table output, action = eval_template_with_custom_code(subtemplate_filepath, context, output_filename, opts) @table = nil output_files << [output_filename, output, action] end return output_files end |
#camel_case(name, flag_all = true) ⇒ Object
convert ‘foo_bar_baz’ into ‘FooBarBaz’
32 33 34 35 36 37 38 39 40 |
# File 'lib/kwatable/template/helper/common.rb', line 32 def camel_case(name, flag_all=true) s = '' name.split('_').each_with_index do |word, i| s << (!flag_all && i == 0 ? word.downcase : word.capitalize) end #s = name.split('_').collect { |w| w.capitalize }.join() #s[0] = s[0].to_s.upcase.chr unless flag_all return s end |
#context_var_required(var_name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kwatable/template/helper/common.rb', line 16 def context_var_required(var_name) value = instance_variable_get(var_name) return if value template_name = File.basename(@template_filename).sub(/\.\w+$/, '') msg = case var_name when '@tables' ; "don't use '-m' option with '%s'." % template_name when '@table' ; "option '-m' is required when using '%s'." % template_name else ; "context value '%s' required but not found." % var_name end err = StandardError.new(msg) err.set_backtrace(caller()) raise err end |
#each_table(tables, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/kwatable/template/helper/common.rb', line 114 def each_table(tables, &block) tables.each do |table| reason = table_check_modeling(table) if reason msg = "*** table '#{table['name']}' skipped. (reason: #{reason})" $stderr.puts msg unless @options[?q] else yield(table) end end end |
#encode(str, encoding) ⇒ Object
encode string
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kwatable/template/helper/common.rb', line 99 def encode(str, encoding) require 'kconv' out_code = case encoding when /\Aeuc-jp\z/i ; Kconv::EUC when /\As(hift[-_])?jis\z/i ; Kconv::SJIS when /\Autf8\z/i ; Kconv::UTF8 when /\Autf16\z/i ; Kconv::UTF16 when /\Aiso-2022-jp\z/i ; Kconv::JIS else ; nil end return Kconv.kconv(str, out_code) end |
#eval_template_with_custom_code(template_filename, context, output_filename, options = {}) ⇒ Object
read user custom code from output filename and apply template with it. return output string and action symbol(:create, :update, or :identical).
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/kwatable/template/helper/common.rb', line 69 def eval_template_with_custom_code(template_filename, context, output_filename, ={}) # options encoding = [:encoding] template = [:template] # get user custom code if test(?f, output_filename) content = File.read(output_filename) custom_code = user_custom_code(content) else content = custom_code = nil end # eval template and get output context.instance_variable_set("@user_custom_code", custom_code) output = Util.eval_template(template_filename, context, template) output = encode(output, encoding) if encoding context.instance_variable_set("@user_custom_code", nil) # detect action action = content.nil? ? :create : (content == output ? :identical : :update) return output, action end |
#find_file(filename, path_list, find_in_currdir = true) ⇒ Object
find file from path_list
50 51 52 |
# File 'lib/kwatable/template/helper/common.rb', line 50 def find_file(filename, path_list, find_in_currdir=true) return Util.find_file(filename, path_list, find_in_currdir) end |
#find_subtemplate(template_filename = @template_filename, template_pathlist = @template_pathlist) ⇒ Object
find subtemplate
56 57 58 59 60 61 62 63 64 |
# File 'lib/kwatable/template/helper/common.rb', line 56 def find_subtemplate(template_filename=@template_filename, template_pathlist=@template_pathlist) basename = File.basename(template_filename) subtemplate_filename = basename.sub(/\.eruby\z/, '.sub.eruby') subtemplate_filepath = Util.find_file(subtemplate_filename, template_pathlist) unless subtemplate_filepath raise "#{basename}: subtemplate `#{subtemplate_filepath}' not found." end return subtemplate_filepath end |
#q(obj) ⇒ Object
quote by single-quoation
44 45 46 |
# File 'lib/kwatable/template/helper/common.rb', line 44 def q(obj) return "'#{obj.to_s.gsub(/['\\]/, '\\\\\1')}'" end |
#user_custom_code(content) ⇒ Object
get user custom code
92 93 94 95 |
# File 'lib/kwatable/template/helper/common.rb', line 92 def user_custom_code(content) pattern = /^.*<user-custom-code>(?:.|\n)*<\/user-custom-code>.*\n/ return content =~ pattern ? $& : nil end |