Class: Kokonfig::DataFile

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

Instance Method Summary collapse

Constructor Details

#initialize(data_file_path, data_dir, templates_dir) ⇒ DataFile

Returns a new instance of DataFile.



7
8
9
10
11
# File 'lib/kokonfig/data_file.rb', line 7

def initialize(data_file_path, data_dir, templates_dir)
  @path = data_file_path
  @data_dir = data_dir
  @templates_dir = templates_dir
end

Instance Method Details

#base_result_fileObject



37
38
39
# File 'lib/kokonfig/data_file.rb', line 37

def base_result_file
  @base_result_file or @base_result_file = File.basename(@path, ".yml")
end

#base_result_relative_pathObject



41
42
43
# File 'lib/kokonfig/data_file.rb', line 41

def base_result_relative_path
  @base_result_relative_path or @base_result_relative_path = Pathname.new(@path).relative_path_from(@data_dir).dirname
end

#generate_files(output_dir) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kokonfig/data_file.rb', line 45

def generate_files(output_dir)
  template = Kokonfig::Template.from_file(self.template_path)

  data_per_version = YAML.load_file(@path)      
  data_per_version.each do |version, data|
    data = data.merge({version: version})
    rendered_data = template.apply(data)

    FileUtils.mkdir_p(File.join(output_dir, self.base_result_relative_path))

    output_path = File.join(output_dir, self.result_file(version))
    File.open(output_path, 'w+') do |result_file|
      result_file.write(rendered_data)
    end
  end
end

#result_file(version) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kokonfig/data_file.rb', line 25

def result_file(version)
  path_parts = []
  path_parts << base_result_relative_path if base_result_relative_path.to_s != "."

  result_base_file_extension = File.extname(base_result_file)
  result_base_file_name = File.basename(base_result_file, result_base_file_extension)
  result_file_name = "#{result_base_file_name}.#{version}#{result_base_file_extension}"
  path_parts << result_file_name

  File.join(*path_parts)
end

#template_pathObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kokonfig/data_file.rb', line 13

def template_path
  path_parts = []
  path_parts << @templates_dir

  path_parts << base_result_relative_path if base_result_relative_path.to_s != "."

  template_file_name = "#{base_result_file}.erb"
  path_parts << template_file_name

  File.join(*path_parts)
end