smarter_csv 1.0.9 → 1.0.10

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
  SHA1:
3
- metadata.gz: 48d8c593c902730176cc44664a1504ae041e984b
4
- data.tar.gz: df04a687fb895502d6cebcc748a931dc060de4a7
3
+ metadata.gz: c8d2d18557f4dbffe6711681fa15d783f6521362
4
+ data.tar.gz: 1a9e8f007ccbd329d77d6f990f77e0073a66026d
5
5
  SHA512:
6
- metadata.gz: 01582b2515dcad6d402ea6130ffafb5cadbbf04bc2bc13d8d57b73573642ad46fa8c5e3ca1b4d3b831a6bf1c46133fcba1149bce1635ad8d643af5aac1a83a86
7
- data.tar.gz: d7c1b27060c6a8b1768b37f2fbad22e62cdc99d8c2deaff28753960a8bf5d31da7609d06bc7d799e3dbc940127672cf28d718362058564d997e4d020c0b01df0
6
+ metadata.gz: 8450de646c3573ec1fc87001dedbbf6ef5b062fd7bf2ab7dc91ef3973821f21dda1e5a8a613f8912faae3e632b9da1d97c29a7708172784dd8c50c075c31a5a9
7
+ data.tar.gz: 77b632311d95dc2e4a4bac5f7a28c6702be0c72fc6686e90c169851ae8a8ff60e962918fdbe48acf8f3d87ac323a37826f72000c59a3c8e60d08a206d65b210c
data/README.md CHANGED
@@ -200,6 +200,9 @@ Or install it yourself as:
200
200
 
201
201
  ## Changes
202
202
 
203
+ #### 1.0.10 (2013-06-26)
204
+ * bugfix : fixed issue #14 - passing options along to CSV.parse (thank to Marcos Zimmermann)
205
+
203
206
  #### 1.0.9 (2013-06-19)
204
207
  * bugfix : fixed issue #13 with negative integers and floats not being correctly converted (thanks to Graham Wetzler)
205
208
 
@@ -278,6 +281,7 @@ And a special thanks to those who contributed pull requests:
278
281
  * [Pavel](http://github.com/paxa)
279
282
  * [Félix Bellanger](https://github.com/Keeguon)
280
283
  * [Graham Wetzler](https://github.com/grahamwetzler)
284
+ * [Marcos G. Zimmermann](https://github.com/marcosgz)
281
285
 
282
286
 
283
287
  ## Contributing
@@ -5,11 +5,12 @@ module SmarterCSV
5
5
 
6
6
  def SmarterCSV.process(input, options={}, &block) # first parameter: filename or input object with readline method
7
7
  default_options = {:col_sep => ',' , :row_sep => $/ , :quote_char => '"', :force_simple_split => false , :verbose => false ,
8
- :remove_empty_values => true, :remove_zero_values => false , :remove_values_matching => nil , :remove_empty_hashes => true , :strip_whitespace => true,
8
+ :remove_empty_values => true, :remove_zero_values => false , :remove_values_matching => nil , :remove_empty_hashes => true , :strip_whitespace => true,
9
9
  :convert_values_to_numeric => true, :strip_chars_from_headers => nil , :user_provided_headers => nil , :headers_in_file => true,
10
10
  :comment_regexp => /^#/, :chunk_size => nil , :key_mapping_hash => nil , :downcase_header => true, :strings_as_keys => false, :file_encoding => 'utf-8'
11
11
  }
12
12
  options = default_options.merge(options)
13
+ csv_options = options.select{|k,v| [:col_sep, :row_sep, :quote_char].include?(k)} # options.slice(:col_sep, :row_sep, :quote_char)
13
14
  headerA = []
14
15
  result = []
15
16
  old_row_sep = $/
@@ -25,7 +26,7 @@ module SmarterCSV
25
26
  line_count += 1
26
27
  header = header.gsub(options[:strip_chars_from_headers], '') if options[:strip_chars_from_headers]
27
28
  if (header =~ %r{#{options[:quote_char]}}) and (! options[:force_simple_split])
28
- file_headerA = CSV.parse( header ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
29
+ file_headerA = CSV.parse( header, csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
29
30
  else
30
31
  file_headerA = header.split(options[:col_sep])
31
32
  end
@@ -36,11 +37,11 @@ module SmarterCSV
36
37
  file_header_size = file_headerA.size
37
38
  end
38
39
  if options[:user_provided_headers] && options[:user_provided_headers].class == Array && ! options[:user_provided_headers].empty?
39
- # use user-provided headers
40
+ # use user-provided headers
40
41
  headerA = options[:user_provided_headers]
41
42
  if defined?(file_header_size) && ! file_header_size.nil?
42
43
  if headerA.size != file_header_size
43
- raise SmarterCSV::HeaderSizeMismatch , "ERROR [smarter_csv]: :user_provided_headers defines #{headerA.size} headers != CSV-file #{input} has #{file_header_size} headers"
44
+ raise SmarterCSV::HeaderSizeMismatch , "ERROR [smarter_csv]: :user_provided_headers defines #{headerA.size} headers != CSV-file #{input} has #{file_header_size} headers"
44
45
  else
45
46
  # we could print out the mapping of file_headerA to headerA here
46
47
  end
@@ -49,10 +50,10 @@ module SmarterCSV
49
50
  headerA = file_headerA
50
51
  end
51
52
  headerA.map!{|x| x.to_sym } unless options[:strings_as_keys]
52
-
53
- unless options[:user_provided_headers] # wouldn't make sense to re-map user provided headers
53
+
54
+ unless options[:user_provided_headers] # wouldn't make sense to re-map user provided headers
54
55
  key_mappingH = options[:key_mapping]
55
-
56
+
56
57
  # do some key mapping on the keys in the file header
57
58
  # if you want to completely delete a key, then map it to nil or to ''
58
59
  if ! key_mappingH.nil? && key_mappingH.class == Hash && key_mappingH.keys.size > 0
@@ -79,7 +80,7 @@ module SmarterCSV
79
80
  line.chomp! # will use $/ which is set to options[:col_sep]
80
81
 
81
82
  if (line =~ %r{#{options[:quote_char]}}) and (! options[:force_simple_split])
82
- dataA = CSV.parse( line ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
83
+ dataA = CSV.parse( line, csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
83
84
  else
84
85
  dataA = line.split(options[:col_sep])
85
86
  end
@@ -102,7 +103,7 @@ module SmarterCSV
102
103
  when /^[+-]?\d+\.\d+$/
103
104
  hash[k] = v.to_f
104
105
  when /^[+-]?\d+$/
105
- hash[k] = v.to_i
106
+ hash[k] = v.to_i
106
107
  end
107
108
  end
108
109
  end
@@ -110,7 +111,7 @@ module SmarterCSV
110
111
 
111
112
  if use_chunks
112
113
  chunk << hash # append temp result to chunk
113
-
114
+
114
115
  if chunk.size >= chunk_size || f.eof? # if chunk if full, or EOF reached
115
116
  # do something with the chunk
116
117
  if block_given?
@@ -122,7 +123,7 @@ module SmarterCSV
122
123
  chunk = [] # initialize for next chunk of data
123
124
  end
124
125
  # while a chunk is being filled up we don't need to do anything else here
125
-
126
+
126
127
  else # no chunk handling
127
128
  if block_given?
128
129
  yield [hash] # do something with the hash in the block (better to use chunking here)
@@ -1,3 +1,3 @@
1
1
  module SmarterCSV
2
- VERSION = "1.0.9"
2
+ VERSION = "1.0.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - |
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2013-06-19 00:00:00 Z
14
+ date: 2013-06-26 00:00:00 Z
15
15
  dependencies: []
16
16
 
17
17
  description: Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with optional features for processing large files in parallel, embedded comments, unusual field- and record-separators, flexible mapping of CSV-headers to Hash-keys