Sha256: 5ddaa25f14f958cab7b5588d9d5d7e03a53d3ced5bf974879ce8743b643fb163

Contents?: true

Size: 1.49 KB

Versions: 29

Compression:

Stored size: 1.49 KB

Contents

# encoding: UTF-8
#
# Copyright (c) 2010-2015 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

29 entries across 29 versions & 2 rubygems

Version Path
gooddata-edge-0.6.27.edge lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.49 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.48 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.47 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.46 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.45 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.44 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.43 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.42 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.41 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.40 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.39 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.38 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.37 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.36 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.35 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.34 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.33 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.32 lib/gooddata/helpers/csv_helper.rb
gooddata-0.6.31 lib/gooddata/helpers/csv_helper.rb