csv2hash 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -21
- data/ReleaseNotes.md +4 -0
- data/csv2hash.gemspec +1 -1
- data/lib/csv2hash/structure_validator.rb +4 -4
- data/lib/csv2hash/version.rb +1 -1
- data/spec/csv2hash/structure_validator_spec.rb +20 -1
- data/spec/csv2hash/validator_spec.rb +0 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ab8b867d5257761c23fb310ad45eab25d5eebe9
|
4
|
+
data.tar.gz: ffec55a91cb1ccd853e641b65f965cc0a51c0350
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 801f045f211e3dffaafad4c27dffd922132b40be5bc98b2013fafa676f7c3a7584bfad7f5c39ce96dc34a7ace3033cfd54f5505812d61e58ba0c20b6b956228c
|
7
|
+
data.tar.gz: d446c104f67af25a884fabfa3784b2baaaeaf559de8cb8d901c55acef7762966a4aa1ff80b637cfbda25e18c75bcc725b29fd3a1b97212ef9d3b7719ae628b78
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -35,19 +35,19 @@ You should declare a definition for you CSV, and then define for each cell what
|
|
35
35
|
|
36
36
|
Example :
|
37
37
|
|
38
|
-
|
38
|
+
If you want the very first cell, located on the first line and on the first column to be a string with values are either 'yes' either 'no', you can write the following validation rule:
|
39
39
|
|
40
40
|
{ name: 'aswering', type: 'string', values: ['yes', 'no'], position: [0,0] }
|
41
41
|
|
42
|
-
|
42
|
+
:type attribute has 'string' for default value, therefore you can just write this:
|
43
43
|
|
44
44
|
{ name: 'aswering', values: ['yes', 'no'], position: [0,0] }
|
45
45
|
|
46
|
-
You can define
|
46
|
+
You can define you own message but default message is 'undefined :key on :position'
|
47
47
|
|
48
48
|
{ name: 'aswering', values: ['yes', 'no'], position: [0,0], message: 'this value is not supported' }
|
49
49
|
|
50
|
-
You can also define Range
|
50
|
+
You can also define Range of values
|
51
51
|
|
52
52
|
{ name: 'score', values: 0..5, position: [0,0] }
|
53
53
|
|
@@ -57,7 +57,7 @@ The message is parsed:
|
|
57
57
|
|
58
58
|
It produces :
|
59
59
|
|
60
|
-
value of
|
60
|
+
value of answering is not supported, please use one of [yes, no]
|
61
61
|
|
62
62
|
### Default values
|
63
63
|
|
@@ -85,7 +85,7 @@ A definition should be provided. There are 2 types of definitions:
|
|
85
85
|
|
86
86
|
## Samples
|
87
87
|
|
88
|
-
### Validation of
|
88
|
+
### Validation of cells with defined precision
|
89
89
|
|
90
90
|
Consider the following CSV:
|
91
91
|
|
@@ -125,7 +125,7 @@ Precise position validation sample:
|
|
125
125
|
|
126
126
|
end
|
127
127
|
|
128
|
-
### Validation of a collection
|
128
|
+
### Validation of a collection (Regular CSV)
|
129
129
|
|
130
130
|
Consider the following CSV:
|
131
131
|
|
@@ -164,7 +164,6 @@ Collection validation sample:
|
|
164
164
|
|
165
165
|
end
|
166
166
|
|
167
|
-
|
168
167
|
### Structure validation rules
|
169
168
|
|
170
169
|
You may want to validate some structure, like min or max number of columns, definition accepts structure_rules as a key for the third parameter.
|
@@ -193,7 +192,7 @@ class MyParser
|
|
193
192
|
end
|
194
193
|
|
195
194
|
def definition
|
196
|
-
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, structure_rules: {'
|
195
|
+
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, structure_rules: { 'MinColumns' => 2, 'MaxColumns' => 3 })
|
197
196
|
end
|
198
197
|
end
|
199
198
|
|
@@ -202,19 +201,19 @@ end
|
|
202
201
|
|
203
202
|
You can define the number of rows to skip in the header of the CSV.
|
204
203
|
|
205
|
-
Definition.new(rules, type, header_size
|
204
|
+
Definition.new(rules, type, header_size: 0)
|
206
205
|
|
207
206
|
### Parser and configuration
|
208
207
|
|
209
|
-
Pasrer take
|
208
|
+
Pasrer can take several parameters like that:
|
210
209
|
|
211
|
-
|
210
|
+
definition, file_path, exception_mode=true, data_source=nil, ignore_blank_line=false
|
212
211
|
|
213
|
-
you can pass directly Array of data (Array at 2 dimensions) really useful for testing, if you don't care about
|
212
|
+
you can pass directly Array of data (Array at 2 dimensions) really useful for testing, if you don't care about blank lines in your CSV you can ignore them.
|
214
213
|
|
215
214
|
### Response
|
216
215
|
|
217
|
-
The parser return values wrapper into DataWrapper Object, you can call
|
216
|
+
The parser return values wrapper into DataWrapper Object, you can call ```.valid?``` method on this Object and grab either data or errors like that :
|
218
217
|
|
219
218
|
response = parser.parse
|
220
219
|
if response.valid?
|
@@ -229,11 +228,11 @@ data or errors are Array, but errors can be formatted on csv format with .to_csv
|
|
229
228
|
|
230
229
|
## Exception or Not !
|
231
230
|
|
232
|
-
You can
|
231
|
+
You can choose into 2 different modes of parsing, either **exception mode** for raise exception in first breaking rules or **csv mode** for get csv original data + errors throwing into added columns.
|
233
232
|
|
234
233
|
### On **CSV MODE** you can choose different way for manage errors
|
235
234
|
|
236
|
-
`.parse()` return `data_wrapper` if `.parse()` is invalid, you can code your own behavior
|
235
|
+
`.parse()` return `data_wrapper` if `.parse()` is invalid, you can code your own behavior:
|
237
236
|
|
238
237
|
in your code
|
239
238
|
|
@@ -242,7 +241,7 @@ in your code
|
|
242
241
|
return response if response.valid?
|
243
242
|
# Whatever
|
244
243
|
|
245
|
-
In the same time Csv2hash call **notify(response)** method when CSV parsing fail, you can add your own Notifier
|
244
|
+
In the same time Csv2hash call **notify(response)** method when CSV parsing fail, you can add your own Notifier:
|
246
245
|
|
247
246
|
module Csv2hash
|
248
247
|
module Plugins
|
@@ -287,6 +286,7 @@ errors is a Array of Hash
|
|
287
286
|
### Error
|
288
287
|
|
289
288
|
{ y: 1, x: 1, message: 'undefined nikcname on [0, 0]', key: 'nickname', value: nil }
|
289
|
+
|
290
290
|
## Personal Validator Rule
|
291
291
|
|
292
292
|
You can define your own Validator
|
@@ -302,16 +302,15 @@ For downcase validation
|
|
302
302
|
in your rule
|
303
303
|
|
304
304
|
{ position: [0,0], key: 'name', extra_validator: DowncaseValidator.new,
|
305
|
-
message: 'your data should be
|
305
|
+
message: 'your data should be written in lowercase only.' }
|
306
306
|
|
307
307
|
Csv data
|
308
308
|
|
309
309
|
[ [ 'Foo' ] ]
|
310
310
|
|
311
|
+
# Upgrading from 0.1 to 0.2
|
311
312
|
|
312
|
-
|
313
|
-
|
314
|
-
The signature of Definition#initialize changed, as last parameter is a configuration hash, while in versions prior to 0.2 it was an integer (header_size) consider upgrading your code :
|
313
|
+
The signature of Definition#new has changed, the last parameter is a configuration hash, while in versions prior to 0.2 it was an integer (header_size) consider upgrading your code :
|
315
314
|
|
316
315
|
Prior to 0.2 :
|
317
316
|
|
data/ReleaseNotes.md
CHANGED
data/csv2hash.gemspec
CHANGED
@@ -6,14 +6,14 @@ module Csv2hash::StructureValidator
|
|
6
6
|
begin
|
7
7
|
rule_instance(rule, options).validate! data_source
|
8
8
|
rescue => e
|
9
|
-
self.errors << e.message
|
9
|
+
self.errors << { y: nil, x: nil, message: e.message, key: nil }
|
10
10
|
raise if exception_mode
|
11
11
|
end
|
12
12
|
end
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
15
|
+
def rule_instance rule, options
|
16
|
+
Csv2hash::StructureValidator.const_get(rule).new(options)
|
17
17
|
end
|
18
18
|
|
19
19
|
module Validator
|
data/lib/csv2hash/version.rb
CHANGED
@@ -4,6 +4,7 @@ describe Csv2hash::StructureValidator do
|
|
4
4
|
|
5
5
|
let(:rules) { [ { position: [0,0], key: 'name' } ] }
|
6
6
|
let(:options) { {} }
|
7
|
+
let(:exception_mode) { true }
|
7
8
|
let(:definition) do
|
8
9
|
Csv2hash::Definition.new(rules, Csv2hash::Definition::MAPPING, options).tap do |definition|
|
9
10
|
definition.validate!
|
@@ -12,9 +13,27 @@ describe Csv2hash::StructureValidator do
|
|
12
13
|
end
|
13
14
|
|
14
15
|
subject do
|
15
|
-
Csv2hash.new(definition, 'file_path',
|
16
|
+
Csv2hash.new(definition, 'file_path', exception_mode, data_source)
|
16
17
|
end
|
17
18
|
|
19
|
+
context 'the csv with errors' do
|
20
|
+
let(:options){ { structure_rules: { 'MaxColumns' => 2 } } }
|
21
|
+
let(:exception_mode) { false }
|
22
|
+
before { subject.parse }
|
23
|
+
let(:data_source) do
|
24
|
+
[
|
25
|
+
[ 'John', 'Doe' ],
|
26
|
+
[ 'Jane', 'Doe', 'extra field' ]
|
27
|
+
]
|
28
|
+
end
|
29
|
+
|
30
|
+
its(:csv_with_errors) { should be_kind_of CsvArray }
|
31
|
+
it "adds structure error in first cell" do
|
32
|
+
subject.csv_with_errors.first[:message].should eq 'Too many columns (max. 2) on line 1'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
18
37
|
context '#MaxColumns' do
|
19
38
|
let(:options){ { structure_rules: { 'MaxColumns' => 2 } } }
|
20
39
|
|
@@ -9,12 +9,6 @@ describe Csv2hash::Validator do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
subject do
|
13
|
-
Csv2hash.new(definition, 'file_path').tap do |parser|
|
14
|
-
parser.instance_variable_set :@data_source, data_source
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
12
|
describe '#message' do
|
19
13
|
subject { Csv2hash.new double('definition', type: Csv2hash::Definition::COLLECTION), nil }
|
20
14
|
|