Sha256: f7b1818a9b562247768c273af5b581f10a50d3ac68877b84b23cf556aa1f19fb

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module Csb
  class Template
    attr_accessor :utf8_bom, :filename, :streaming, :items, :cols, :csv_options

    def initialize(utf8_bom:, streaming:, csv_options:)
      @utf8_bom = utf8_bom
      @streaming = streaming
      @csv_options = csv_options
      @cols = Cols.new
      @items = []
    end

    def build
      streaming ? build_enumerator : build_string
    end

    def streaming?
      !!streaming
    end

    private

    def build_string
      builder = Builder.new(utf8_bom: utf8_bom, items: items, csv_options: csv_options)
      builder.cols.copy!(cols)
      builder.build
    end

    def build_enumerator
      Enumerator.new do |y|
        begin
          builder = Builder.new(y, utf8_bom: utf8_bom, items: items, csv_options: csv_options)
          builder.cols.copy!(cols)
          builder.build
        rescue => error
          if Csb.configuration.after_streaming_error.respond_to?(:call)
            Csb.configuration.after_streaming_error.call(error)
          end
          raise error
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
csb-0.10.0 lib/csb/template.rb
csb-0.9.0 lib/csb/template.rb
csb-0.8.0 lib/csb/template.rb