smarter_csv 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +8 -3
- data/lib/smarter_csv/smarter_csv.rb +5 -3
- data/lib/smarter_csv/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -136,7 +136,7 @@ The options and the block are optional.
|
|
136
136
|
:key_mapping | nil | a hash which maps headers from the CSV file to keys in the result hash
|
137
137
|
:downcase_header | true | downcase all column headers
|
138
138
|
:strings_as_keys | false | use strings instead of symbols as the keys in the result hashes
|
139
|
-
:
|
139
|
+
:strip_whitespace | true | remove whitespace before/after values and headers
|
140
140
|
:remove_empty_values | true | remove values which have nil or empty strings as values
|
141
141
|
:remove_zero_values | true | remove values which have a numeric value equal to zero / 0
|
142
142
|
:remove_values_matching | nil | removes key/value pairs if value matches given regular expressions. e.g.:
|
@@ -196,10 +196,15 @@ Or install it yourself as:
|
|
196
196
|
|
197
197
|
## Changes
|
198
198
|
|
199
|
-
#### 1.0.
|
199
|
+
#### 1.0.4 (2012-08-17)
|
200
|
+
|
201
|
+
* renamed the following options:
|
202
|
+
* :strip_whitepace_from_values => :strip_whitespace - removes leading/trailing whitespace from headers and values
|
203
|
+
|
204
|
+
#### 1.0.3 (2012-08-16)
|
200
205
|
|
201
206
|
* added the following options:
|
202
|
-
* strip_whitepace_from_values
|
207
|
+
* :strip_whitepace_from_values - removes leading/trailing whitespace from values
|
203
208
|
|
204
209
|
#### 1.0.2 (2012-08-02)
|
205
210
|
|
@@ -5,7 +5,7 @@ module SmarterCSV
|
|
5
5
|
|
6
6
|
def SmarterCSV.process(filename, options={}, &block)
|
7
7
|
default_options = {:col_sep => ',' , :row_sep => $/ , :quote_char => '"',
|
8
|
-
:remove_empty_values => true, :remove_zero_values => false , :remove_values_matching => nil , :remove_empty_hashes => 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
|
11
11
|
}
|
@@ -22,7 +22,9 @@ module SmarterCSV
|
|
22
22
|
# the first line of a CSV file contains the header .. it might be commented out, so we need to read it anyhow
|
23
23
|
header = f.readline.sub(options[:comment_regexp],'').chomp(options[:row_sep])
|
24
24
|
header = header.gsub(options[:strip_chars_from_headers], '') if options[:strip_chars_from_headers]
|
25
|
-
file_headerA = header.split(options[:col_sep]).map{|x| x.gsub(%r/options[:quote_char]/,'')
|
25
|
+
file_headerA = header.split(options[:col_sep]).map{|x| x.gsub(%r/options[:quote_char]/,'')}
|
26
|
+
file_headerA.map!{|x| x.strip} if options[:strip_whitespace]
|
27
|
+
file_headerA.map!{|x| x.gsub(/\s+/,'_')}
|
26
28
|
file_headerA.map!{|x| x.downcase } if options[:downcase_header]
|
27
29
|
file_header_size = file_headerA.size
|
28
30
|
end
|
@@ -68,7 +70,7 @@ module SmarterCSV
|
|
68
70
|
line.chomp! # will use $/ which is set to options[:col_sep]
|
69
71
|
|
70
72
|
dataA = line.split(options[:col_sep])
|
71
|
-
dataA.map!{|x| x.strip} if options[:
|
73
|
+
dataA.map!{|x| x.strip} if options[:strip_whitespace]
|
72
74
|
hash = Hash.zip(headerA,dataA) # from Facets of Ruby library
|
73
75
|
# make sure we delete any key/value pairs from the hash, which the user wanted to delete:
|
74
76
|
hash.delete(nil); hash.delete(''); hash.delete(:"") # delete any hash keys which were mapped to be deleted
|
data/lib/smarter_csv/version.rb
CHANGED