smarter_csv 1.11.0 → 1.11.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 996d8195dc01722461990cb51dbc2b65d4cf2edf93eff2c1d4c5359614911b92
4
- data.tar.gz: 0f193b450b4cfb97a1f11bbeacbc659b5220fa545751533a225347349943bc9a
3
+ metadata.gz: 4a650fbccc5bf703199c6771da75ed95c87c9e3763e176b688a2daaa1b0c4669
4
+ data.tar.gz: 89cfff5bb92ee8faaeea7a644b61e63a7e47c61f30faefc515b602808eec0989
5
5
  SHA512:
6
- metadata.gz: 1a8141ccf75d5f6edf5ffc6f4b719f36dabfc826903b69804cfda09988f43fce24678e7e44fe53e0228a3077858fa870b920bf019b9dd996c53d9024364767ca
7
- data.tar.gz: 25daf165cb30decb0f7cd5703758dde5879a12398e6826fb8e0def3efb135b33ab8ef2bf26149d0bd887d42d73a19f9af8ab5e7ea1aee737e139a3acf5da6a4f
6
+ metadata.gz: 70ea1888be23a467d15001086935117252a30860ee5a418e8b61b61bfb19a03377391ffb28589db412b0aac4293ac029a047b268d0fa6836ec60f2319cabf102
7
+ data.tar.gz: 49d7d1d1c4e258611168056a0b8b7433dfedcf37eeac25c22c0aade552a878361d019b86156cbadcacc5996a43cae11d9d55990a0ef9c4a599cf447019cf8d7f
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
 
2
2
  # SmarterCSV 1.x Change Log
3
3
 
4
+ ## 1.11.2 (2024-07-05)
5
+ * fixing missing errors definition
6
+
7
+ ## 1.11.1 (2024-07-05) (YANKED)
8
+ * improved behavior of Writer class
9
+ * added SmarterCSV.generate shortcut for CSV writing
10
+
4
11
  ## 1.11.0 (2024-07-02)
5
12
  * added SmarterCSV::Writer to output CSV files ([issue #44](https://github.com/tilo/smarter_csv/issues/44))
6
13
 
data/README.md CHANGED
@@ -3,7 +3,13 @@
3
3
 
4
4
  [![codecov](https://codecov.io/gh/tilo/smarter_csv/branch/main/graph/badge.svg?token=1L7OD80182)](https://codecov.io/gh/tilo/smarter_csv) [![Gem Version](https://badge.fury.io/rb/smarter_csv.svg)](http://badge.fury.io/rb/smarter_csv)
5
5
 
6
- This library provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.
6
+ SmarterCSV provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.
7
+
8
+ SmarterCSV focuses on representing the data for each row as a hash.
9
+
10
+ When reading CSV files, Using an array-of-hashes format makes it much easier to further process the data, or creating database records with it.
11
+
12
+ When writing CSV data to file, it similarly takes arrays of hashes, and converts them to a CSV file.
7
13
 
8
14
  #### BREAKING CHANGES
9
15
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmarterCSV
4
+ class Error < StandardError; end # new code should rescue this instead
5
+ # Reader:
6
+ class SmarterCSVException < Error; end # for backwards compatibility
7
+ class HeaderSizeMismatch < SmarterCSVException; end
8
+ class IncorrectOption < SmarterCSVException; end
9
+ class ValidationError < SmarterCSVException; end
10
+ class DuplicateHeaders < SmarterCSVException; end
11
+ class MissingKeys < SmarterCSVException; end # previously known as MissingHeaders
12
+ class NoColSepDetected < SmarterCSVException; end
13
+ class KeyMappingError < SmarterCSVException; end
14
+ # Writer:
15
+ class InvalidInputData < SmarterCSVException; end
16
+ end
@@ -1,15 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmarterCSV
4
- class SmarterCSVException < StandardError; end
5
- class HeaderSizeMismatch < SmarterCSVException; end
6
- class IncorrectOption < SmarterCSVException; end
7
- class ValidationError < SmarterCSVException; end
8
- class DuplicateHeaders < SmarterCSVException; end
9
- class MissingKeys < SmarterCSVException; end # previously known as MissingHeaders
10
- class NoColSepDetected < SmarterCSVException; end
11
- class KeyMappingError < SmarterCSVException; end
12
-
13
4
  # first parameter: filename or input object which responds to readline method
14
5
  def SmarterCSV.process(input, given_options = {}, &block) # rubocop:disable Lint/UnusedMethodArgument
15
6
  initialize_variables
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmarterCSV
4
- VERSION = "1.11.0"
4
+ VERSION = "1.11.2"
5
5
  end
@@ -35,22 +35,35 @@ module SmarterCSV
35
35
  # Make sure to use the correct form when specifying headers manually,
36
36
  # in combination with the :discover_headers option
37
37
 
38
+ attr_reader :options, :row_sep, :col_sep, :quote_char, :force_quotes, :discover_headers, :headers, :map_headers, :output_file
39
+
38
40
  class Writer
39
41
  def initialize(file_path, options = {})
40
42
  @options = options
41
- @discover_headers = options.has_key?(:discover_headers) ? (options[:discover_headers] == true) : true
42
- @headers = options[:headers] || []
43
43
  @row_sep = options[:row_sep] || "\n" # RFC4180 "\r\n"
44
44
  @col_sep = options[:col_sep] || ','
45
- @quote_char = '"'
45
+ @quote_char = options[:quote_char] || '"'
46
46
  @force_quotes = options[:force_quotes] == true
47
+ @discover_headers = true # defaults to true
48
+ if options.has_key?(:discover_headers)
49
+ # passing in the option overrides the default behavior
50
+ @discover_headers = options[:discover_headers] == true
51
+ else
52
+ # disable discover_headers when headers are given explicitly
53
+ @discover_headers = !(options.has_key?(:map_headers) || options.has_key?(:headers))
54
+ end
55
+ @headers = [] # start with empty headers
56
+ @headers = options[:headers] if options.has_key?(:headers) # unless explicitly given
57
+ @headers = options[:map_headers].keys if options.has_key?(:map_headers) && !options.has_key?(:headers)
47
58
  @map_headers = options[:map_headers] || {}
59
+
48
60
  @output_file = File.open(file_path, 'w+')
49
61
  # hidden state:
50
62
  @temp_file = Tempfile.new('tempfile', '/tmp')
51
63
  @quote_regex = Regexp.union(@col_sep, @row_sep, @quote_char)
52
64
  end
53
65
 
66
+ # this can be called many times in order to append lines to the csv file
54
67
  def <<(data)
55
68
  case data
56
69
  when Hash
@@ -60,7 +73,7 @@ module SmarterCSV
60
73
  when NilClass
61
74
  # ignore
62
75
  else
63
- raise ArgumentError, "Invalid data type: #{data.class}. Must be a Hash or an Array."
76
+ raise InvalidInputData, "Invalid data type: #{data.class}. Must be a Hash or an Array."
64
77
  end
65
78
  end
66
79
 
data/lib/smarter_csv.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "smarter_csv/version"
4
+ require "smarter_csv/errors"
5
+
4
6
  require "smarter_csv/file_io"
5
7
  require "smarter_csv/options_processing"
6
8
  require "smarter_csv/auto_detection"
@@ -9,8 +11,10 @@ require 'smarter_csv/header_transformations'
9
11
  require 'smarter_csv/header_validations'
10
12
  require "smarter_csv/headers"
11
13
  require "smarter_csv/hash_transformations"
14
+
12
15
  require "smarter_csv/parse"
13
16
  require "smarter_csv/writer"
17
+ require "smarter_csv/smarter_csv"
14
18
 
15
19
  # load the C-extension:
16
20
  case RUBY_ENGINE
@@ -49,4 +53,24 @@ else
49
53
  BLOCK_COMMENT
50
54
  end
51
55
  # :nocov:
52
- require "smarter_csv/smarter_csv"
56
+
57
+ module SmarterCSV
58
+ # SmarterCSV.generate(filename, options) do |csv_writer|
59
+ # MyModel.find_in_batches(batch_size: 100) do |batch|
60
+ # batch.pluck(:name, :description, :instructor).each do |record|
61
+ # csv_writer << record
62
+ # end
63
+ # end
64
+ # end
65
+ #
66
+ # rubocop:disable Lint/UnusedMethodArgument
67
+ def self.generate(filename, options = {}, &block)
68
+ raise unless block_given?
69
+
70
+ writer = Writer.new(filename, options)
71
+ yield writer
72
+ ensure
73
+ writer.finalize
74
+ end
75
+ # rubocop:enable Lint/UnusedMethodArgument
76
+ end
data/smarter_csv.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["tilo.sloboda@gmail.com"]
11
11
 
12
12
  spec.summary = "CSV Reading and Writing"
13
- spec.description = "Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with lots of features for processing large files in parallel, embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers to Hash-keys"
13
+ spec.description = "Ruby Gem for convenient reading and writing: importing of CSV Files as Array(s) of Hashes, with lots of features for processing large files in parallel, embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers to Hash-keys"
14
14
  spec.homepage = "https://github.com/tilo/smarter_csv"
15
15
  spec.license = 'MIT'
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tilo Sloboda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-02 00:00:00.000000000 Z
11
+ date: 2024-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -94,9 +94,10 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with
98
- lots of features for processing large files in parallel, embedded comments, unusual
99
- field- and record-separators, flexible mapping of CSV-headers to Hash-keys
97
+ description: 'Ruby Gem for convenient reading and writing: importing of CSV Files
98
+ as Array(s) of Hashes, with lots of features for processing large files in parallel,
99
+ embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers
100
+ to Hash-keys'
100
101
  email:
101
102
  - tilo.sloboda@gmail.com
102
103
  executables: []
@@ -118,6 +119,7 @@ files:
118
119
  - ext/smarter_csv/smarter_csv.c
119
120
  - lib/smarter_csv.rb
120
121
  - lib/smarter_csv/auto_detection.rb
122
+ - lib/smarter_csv/errors.rb
121
123
  - lib/smarter_csv/file_io.rb
122
124
  - lib/smarter_csv/hash_transformations.rb
123
125
  - lib/smarter_csv/header_transformations.rb