Sha256: 6a7d2be688c87f8d852aa957a7915fe909c8e25e3e8148a996b13ee70621f697

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

# encoding: UTF-8

require 'csv'

module GoodData
  module Helpers
    class Csv
      class << self
        # Read data from CSV
        #
        # @param [Hash] opts
        # @option opts [String] :path File to read data from
        # @option opts [Boolean] :header File to read data from
        # @return Array of rows with loaded data
        def read(opts)
          path = opts[:path]
          res = []

          line = 0

          CSV.foreach(path) do |row|
            line += 1
            next if opts[:header] && line == 1

            if block_given?
              data = yield row
            else
              data = row
            end

            res << data if data
          end

          res
        end

        # Write data to CSV
        # @option opts [String] :path File to write data to
        # @option opts [Array] :data Mandatory array of data to write
        # @option opts [String] :header Optional Header row
        def write(opts, &_block)
          path = opts[:path]
          header = opts[:header]
          data = opts[:data]

          CSV.open(path, 'w') do |csv|
            csv << header unless header.nil?
            data.each do |entry|
              res = yield entry
              csv << res if res
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gooddata-0.6.20 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.19 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.18 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.17 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.16 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.15 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.14 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.13 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.12 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.11 lib/gooddata/helpers/csv_helper.rb