Module: SmarterCSV

Defined in:
lib/smarter_csv.rb,
lib/smarter_csv/errors.rb,
lib/smarter_csv/parser.rb,
lib/smarter_csv/reader.rb,
lib/smarter_csv/writer.rb,
lib/smarter_csv/file_io.rb,
lib/smarter_csv/headers.rb,
lib/smarter_csv/options.rb,
lib/smarter_csv/version.rb,
lib/smarter_csv/auto_detection.rb,
lib/smarter_csv/header_validations.rb,
lib/smarter_csv/hash_transformations.rb,
lib/smarter_csv/header_transformations.rb

Overview

:nocov:

Defined Under Namespace

Modules: AutoDetection, FileIO, HashTransformations, HeaderTransformations, HeaderValidations, Headers, Options, Parser Classes: DuplicateHeaders, Error, HeaderSizeMismatch, IncorrectOption, InvalidInputData, KeyMappingError, MalformedCSV, MissingKeys, NoColSepDetected, Reader, SmarterCSVException, ValidationError, Writer

Constant Summary collapse

VERSION =
"1.13.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#col_sepObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def col_sep
  @col_sep
end

#discover_headersObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def discover_headers
  @discover_headers
end

#force_quotesObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def force_quotes
  @force_quotes
end

#headersObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def headers
  @headers
end

#map_headersObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def map_headers
  @map_headers
end

#optionsObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def options
  @options
end

#output_fileObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def output_file
  @output_file
end

#quote_charObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def quote_char
  @quote_char
end

#row_sepObject (readonly)

IMPORTANT NOTES:

* Data hashes could contain strings or symbols as keys.
  Make sure to use the correct form when specifying headers manually,
  in combination with the :discover_headers option


38
39
40
# File 'lib/smarter_csv/writer.rb', line 38

def row_sep
  @row_sep
end

Class Method Details

.default_optionsObject

NOTE: this is not called when “parse” methods are tested by themselves

ONLY FOR BACKWARDS-COMPATIBILITY



8
9
10
# File 'lib/smarter_csv/options.rb', line 8

def self.default_options
  Options::DEFAULT_OPTIONS
end

.generate(filename, options = {}, &block) ⇒ Object

Convenience method for generating CSV files:

SmarterCSV.generate(filename, options) do |csv_writer|

MyModel.find_in_batches(batch_size: 100) do |batch|
 batch.pluck(:name, :description, :instructor).each do |record|
    csv_writer << record
  end
end

end

rubocop:disable Lint/UnusedMethodArgument



83
84
85
86
87
88
89
90
# File 'lib/smarter_csv.rb', line 83

def self.generate(filename, options = {}, &block)
  raise unless block_given?

  writer = Writer.new(filename, options)
  yield writer
ensure
  writer.finalize
end

.process(input, given_options = {}, &block) ⇒ Object

For backwards compatibility:

while ‘SmarterCSV.process` works for simple cases, you can’t get access to the internal state any longer. e.g. you need the instance of the Reader to access the original headers

Please use this instead:

reader = SmarterCSV::Reader.new(input, options)
reader.process # with or without block


67
68
69
70
# File 'lib/smarter_csv.rb', line 67

def self.process(input, given_options = {}, &block)
  reader = Reader.new(input, given_options)
  reader.process(&block)
end