Sha256: 6fc22cb452f81f5d400689ae302844964cad8e7f7b11b8ede3fabdcceb324984

Contents?: true

Size: 734 Bytes

Versions: 1

Compression:

Stored size: 734 Bytes

Contents

require 'csv'

module Kugiru
  class Builder
    UTF8_BOM = "\xEF\xBB\xBF".freeze

    attr_reader :output, :utf8_bom, :cols, :data

    def initialize(output = '', cols:, data:, utf8_bom: false)
      @output = output
      @utf8_bom = utf8_bom
      @cols = cols
      @data = data
    end

    def build
      output << UTF8_BOM if utf8_bom
      output << CSV.generate_line(cols.keys)
      data.each do |row|
        values = cols.values.map { |pr| pr.call(row) }
        output << CSV.generate_line(values)
      end
      output
    end

    def self.build(**args)
      self.new(args).build
    end

    def self.build_enumerator(**args)
      Enumerator.new do |y|
        self.new(y, args).build
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kugiru-0.1.0 lib/kugiru/builder.rb