Class: Template
- Inherits:
-
Object
- Object
- Template
- Defined in:
- lib/template.rb
Constant Summary collapse
- TEMPLATE_DIR =
"template/"
Class Method Summary collapse
-
.get(src_filename, _binding, binary_version = 1.0) ⇒ Object
テンプレートを元にデータを作成.
- .invalid_templace_version? ⇒ Boolean
-
.target_binary_version(version) ⇒ Object
書かれているテンプレートがどのバージョンのテンプレートを設定.
-
.write(src_filename, dest_filepath, _binding, overwrite = false) ⇒ Object
テンプレートを元にファイルを作成.
Class Method Details
.get(src_filename, _binding, binary_version = 1.0) ⇒ Object
テンプレートを元にデータを作成
テンプレートファイルの検索順位
-
root_dir/template
-
script_dir/template
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/template.rb', line 42 def self.get(src_filename, _binding, binary_version = 1.0) @@binary_version = binary_version @@src_filename = src_filename [Narou.get_root_dir, Narou.get_script_dir].each do |dir| path = File.join(dir, TEMPLATE_DIR, src_filename + ".erb") next unless File.exists?(path) src = open(path, "r:BOM|UTF-8") { |fp| fp.read } result = ERB.new(src, nil, "-").result(_binding) return result end nil end |
.invalid_templace_version? ⇒ Boolean
55 56 57 |
# File 'lib/template.rb', line 55 def self.invalid_templace_version? @@src_version < @@binary_version end |
.target_binary_version(version) ⇒ Object
書かれているテンプレートがどのバージョンのテンプレートを設定
62 63 64 65 66 67 68 |
# File 'lib/template.rb', line 62 def self.target_binary_version(version) @@src_version = version if invalid_templace_version? error "テンプレートのバージョンが古いので意図しない動作をする可能性があります\n" + "(#{@@src_filename}.erb ver #{version.to_f} < #{@@binary_version.to_f})" end end |
.write(src_filename, dest_filepath, _binding, overwrite = false) ⇒ Object
テンプレートを元にファイルを作成
src_filename 読み込みたいテンプレートファイル名(.erb は省略する) dest_filepath 保存先ファイルパス。ディレクトリならファイル名はsrcと同じ名前で保存する _binding 変数とか設定したいスコープの binding 変数を渡す overwrite 上書きするか
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/template.rb', line 20 def self.write(src_filename, dest_filepath, _binding, overwrite = false) if File.directory?(dest_filepath) dest_filepath = File.join(dest_filepath, src_filename) end unless overwrite return if File.exists?(dest_filepath) end result = get(src_filename, _binding) or return nil if Helper.os_windows? File.write(dest_filepath, result) else File.binwrite(dest_filepath, result.lstrip) end end |