Class: SengiriYaml::Writer

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

Instance Method Summary collapse

Instance Method Details

#divide(src_file, dst_dir) ⇒ Array<String>

divide yaml file

Parameters:

  • src_file (String)
  • dst_dir (String)

Returns:

  • (Array<String>)

    divided yaml filenames



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sengiri_yaml/writer.rb', line 10

def divide(src_file, dst_dir)
  FileUtils.mkdir_p(dst_dir) unless File.exist?(dst_dir)

  src_content = YAML.load_file(src_file)
  filenames = []

  case src_content
  when Hash
    src_content.each do |key, value|
      filename = "#{dst_dir}/#{key}.yml"
      File.open(filename, "wb") do |f|
        f.write({key => value}.to_yaml)
      end
      filenames << filename
    end

  when Array
    src_content.each_with_index do |element, index|
      filename = "#{dst_dir}/#{index}.yml"
      File.open(filename, "wb") do |f|
        f.write([element].to_yaml)
      end
      filenames << filename
    end

  else
    raise "Unknown type"
  end

  filenames
end