Sha256: 034783aece98c29b4f6a83f72875bfec9201d910e71f307f3fc70ebca7f022f1

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# Copyright (c) 2020 Jerome Arbez-Gindre
# frozen_string_literal: true

require('csv')
require('defmastership/csv_formatter_header')
require('defmastership/csv_formatter_body')

module DefMastership
  # to export a CSV file
  class CSVFormatter
    COLUMN_LIST = %w[fixed labels eref iref attributes].freeze
    private_constant :COLUMN_LIST

    def initialize(doc)
      @doc = doc
      @header_formatter = CSVFormatterHeader.new(@doc)
      @body_formatter = CSVFormatterBody.new(@doc)
    end

    def export_to(output_file)
      CSV.open(output_file, 'w:ISO-8859-1') do |csv|
        csv << header
        @doc.definitions.each { |definition| csv << body(definition) }
      end
    end

    def header
      header_line =
        COLUMN_LIST.map do |part|
          @header_formatter.public_send("#{part}_header".to_sym)
        end
      header_line.reduce(:+)
    end

    def body(definition)
      body_line =
        COLUMN_LIST.map do |part|
          @body_formatter.public_send("#{part}_body".to_sym, definition)
        end
      body_line.reduce(:+)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
defmastership-1.0.4 lib/defmastership/csv_formatter.rb