Class: Template

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

Defined Under Namespace

Classes: LoadError

Constant Summary collapse

TEMPLATE_DIR =
"template/"
OVERWRITE =
true

Class Method Summary collapse

Class Method Details

.get(src_filename, _binding, binary_version) ⇒ Object

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

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

  1. root_dir/template

  2. script_dir/template

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/template.rb', line 46

def self.get(src_filename, _binding, binary_version)
  @@binary_version = binary_version
  @@src_filename = src_filename
  [Narou.root_dir, Narou.script_dir].each do |dir|
    path = dir.join(TEMPLATE_DIR, src_filename + ".erb")
    next unless path.exist?
    src = Helper::CacheLoader.load(path)
    result = ERB.new(src, nil, "-").result(_binding)
    return result
  end
  raise LoadError, "テンプレートファイルが見つかりません。(#{src_filename}.erb)"
end

.invalid_templace_version?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/template.rb', line 59

def self.invalid_templace_version?
  @@src_version != @@binary_version
end

.target_binary_version(version) ⇒ Object

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

テンプレート内部で使われる変数の変更があった場合に binary_version が上がる (変数の追加ではバージョンは上がらない。現在使われている変数の中身が変わった場合は上る)



69
70
71
72
73
74
75
# File 'lib/template.rb', line 69

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, binary_version, overwrite = false) ⇒ Object

テンプレートを元にファイルを作成

src_filename 読み込みたいテンプレートファイル名(.erb は省略する) dest_filepath 保存先ファイルパス。ディレクトリならファイル名はsrcと同じ名前で保存する _binding 変数とか設定したいスコープの binding 変数を渡す overwrite 上書きするか



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/template.rb', line 24

def self.write(src_filename, dest_filepath, _binding, binary_version, overwrite = false)
  if File.directory?(dest_filepath)
    dest_filepath = File.join(dest_filepath, src_filename)
  end
  unless overwrite
    return if File.exist?(dest_filepath)
  end
  result = get(src_filename, _binding, binary_version) or return nil
  if Helper.os_windows?
    File.write(dest_filepath, result)
  else
    File.binwrite(dest_filepath, result.lstrip)
  end
end