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
-
#col_sep ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#discover_headers ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#force_quotes ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#headers ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#map_headers ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#options ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#output_file ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#quote_char ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
-
#row_sep ⇒ Object
readonly
IMPORTANT NOTES: * Data hashes could contain strings or symbols as keys.
Class Method Summary collapse
-
.default_options ⇒ Object
NOTE: this is not called when “parse” methods are tested by themselves.
-
.generate(filename, options = {}, &block) ⇒ Object
Convenience method for generating CSV files:.
-
.process(input, given_options = {}, &block) ⇒ Object
For backwards compatibility:.
Instance Attribute Details
#col_sep ⇒ Object (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_headers ⇒ Object (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_quotes ⇒ Object (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 |
#headers ⇒ Object (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_headers ⇒ Object (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 |
#options ⇒ Object (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 end |
#output_file ⇒ Object (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_char ⇒ Object (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_sep ⇒ Object (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_options ⇒ Object
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. 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, = {}, &block) raise unless block_given? writer = Writer.new(filename, ) 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, )
reader.process # with or without block
67 68 69 70 |
# File 'lib/smarter_csv.rb', line 67 def self.process(input, = {}, &block) reader = Reader.new(input, ) reader.process(&block) end |