Sha256: 1056911c2c08c9abc7b416c7ea8e17cef7defcceefdd333b8718ec71d4178ded

Contents?: true

Size: 1.49 KB

Versions: 41

Compression:

Stored size: 1.49 KB

Contents

# encoding: UTF-8
#
# Copyright (c) 2010-2017 GoodData Corporation. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

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

41 entries across 41 versions & 1 rubygems

Version Path
gooddata-2.1.2-java lib/gooddata/helpers/csv_helper.rb
gooddata-2.1.2 lib/gooddata/helpers/csv_helper.rb
gooddata-2.1.1-java lib/gooddata/helpers/csv_helper.rb
gooddata-2.1.1 lib/gooddata/helpers/csv_helper.rb
gooddata-2.1.0-java lib/gooddata/helpers/csv_helper.rb
gooddata-2.1.0 lib/gooddata/helpers/csv_helper.rb
gooddata-2.0.1-java lib/gooddata/helpers/csv_helper.rb
gooddata-2.0.1 lib/gooddata/helpers/csv_helper.rb
gooddata-2.0.0-java lib/gooddata/helpers/csv_helper.rb
gooddata-2.0.0 lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.6-java lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.6 lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.5-java lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.5 lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.4-java lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.4 lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.3-java lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.3 lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.2-java lib/gooddata/helpers/csv_helper.rb
gooddata-1.3.2 lib/gooddata/helpers/csv_helper.rb