Sha256: 9474e014685d2d33498456b4a70fc91574d1b0e9c6689e78d4cb7b7f6e5db56c

Contents?: true

Size: 1.3 KB

Versions: 7

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

7 entries across 7 versions & 1 rubygems

Version Path
gooddata-0.6.10 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.9 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.8 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.7 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.6 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.5 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.4 lib/gooddata/helpers/csv_helper.rb