Class: Template

Inherits:
Object
  • Object
show all
Defined in:
lib/template.rb

Constant Summary collapse

TEMPLATE_DIR =
"template/"

Class Method Summary collapse

Class Method Details

.get(src_filename, _binding, binary_version = 1.0) ⇒ Object

テンプレートを元にデータを作成

テンプレートファイルの検索順位

  1. root_dir/template

  2. script_dir/template



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/template.rb', line 38

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

Returns:

  • (Boolean)


51
52
53
# File 'lib/template.rb', line 51

def self.invalid_templace_version?
  @@src_version < @@binary_version
end

.target_binary_version(version) ⇒ Object

書かれているテンプレートがどのバージョンのテンプレートを設定



58
59
60
61
62
63
64
# File 'lib/template.rb', line 58

def self.target_binary_version(version)
  @@src_version = version
  if invalid_templace_version?
    warn "テンプレートのバージョンが古いので意図しない動作をする可能性があります\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
# 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
  File.write(dest_filepath, result)
end