Sha256: 8d14ef746f79428204a9c812cffa5c0c909ef73de862115a796010a79d09a888

Contents?: true

Size: 992 Bytes

Versions: 6

Compression:

Stored size: 992 Bytes

Contents

module SengiriYaml
  require "yaml"
  require "fileutils"

  class Writer
    # divide yaml file
    # @param src_file [String]
    # @param dst_dir  [String]
    # @return [Array<String>] divided yaml filenames
    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sengiri_yaml-1.0.0 lib/sengiri_yaml/writer.rb
sengiri_yaml-1.0.0.rc1 lib/sengiri_yaml/writer.rb
sengiri_yaml-0.0.4 lib/sengiri_yaml/writer.rb
sengiri_yaml-0.0.3 lib/sengiri_yaml/writer.rb
sengiri_yaml-0.0.2 lib/sengiri_yaml/writer.rb
sengiri_yaml-0.0.1 lib/sengiri_yaml/writer.rb