csv2hash 0.6.0 → 0.6.1
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.
- checksums.yaml +4 -4
- data/{ReleaseNotes.md → CHANGELOG.md} +5 -0
- data/Gemfile.lock +6 -6
- data/README.md +48 -133
- data/UPGRADE.md +94 -0
- data/bin/launch_irb +0 -0
- data/lib/csv2hash/structure_validator/deprecation.rb +21 -0
- data/lib/csv2hash/structure_validator/max_columns.rb +14 -12
- data/lib/csv2hash/structure_validator/min_columns.rb +14 -12
- data/lib/csv2hash/structure_validator.rb +35 -21
- data/lib/csv2hash/version.rb +1 -1
- data/spec/csv2hash/structure_validator_spec.rb +4 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4017d9fec070fb90dc8c400f67f3c78b9fc6f536
|
4
|
+
data.tar.gz: 36ec66a10def761fff7f091aaf61160a8ad4b852
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e77b7043aff1425609cd7844ade388db5dda784a7d6fa1da3ea90d773af69327ea94e9efecb2e796e63e995c850c22e59228fda689a6ba694133a66decd1fb1
|
7
|
+
data.tar.gz: a54b994cc833b8381d38a4623b96b8a2f630ccd1228990320cf6645e401162ad6fcbe45fed6906c3eac79daeda149e4fc391192f1b19e9168e5f33aa178b508e
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
csv2hash (0.6.
|
4
|
+
csv2hash (0.6.1)
|
5
5
|
activesupport (~> 4.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (4.1.
|
10
|
+
activesupport (4.1.5)
|
11
11
|
i18n (~> 0.6, >= 0.6.9)
|
12
12
|
json (~> 1.7, >= 1.7.7)
|
13
13
|
minitest (~> 5.1)
|
@@ -28,10 +28,10 @@ GEM
|
|
28
28
|
json (1.8.1)
|
29
29
|
method_source (0.8.2)
|
30
30
|
mime-types (2.3)
|
31
|
-
minitest (5.4.
|
31
|
+
minitest (5.4.1)
|
32
32
|
multi_json (1.10.1)
|
33
33
|
netrc (0.7.7)
|
34
|
-
pry (0.10.
|
34
|
+
pry (0.10.1)
|
35
35
|
coderay (~> 1.1.0)
|
36
36
|
method_source (~> 0.8.1)
|
37
37
|
slop (~> 3.4)
|
@@ -56,13 +56,13 @@ GEM
|
|
56
56
|
multi_json
|
57
57
|
simplecov-html (~> 0.8.0)
|
58
58
|
simplecov-html (0.8.0)
|
59
|
-
slop (3.
|
59
|
+
slop (3.6.0)
|
60
60
|
term-ansicolor (1.3.0)
|
61
61
|
tins (~> 1.0)
|
62
62
|
thor (0.19.1)
|
63
63
|
thread_safe (0.3.4)
|
64
64
|
tins (1.3.2)
|
65
|
-
tzinfo (1.2.
|
65
|
+
tzinfo (1.2.2)
|
66
66
|
thread_safe (~> 0.1)
|
67
67
|
|
68
68
|
PLATFORMS
|
data/README.md
CHANGED
@@ -26,24 +26,26 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
Parsing is based on rules, you
|
29
|
+
Parsing is based on rules, you should defined rule for each cells
|
30
30
|
|
31
31
|
### DSL
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
33
|
+
```
|
34
|
+
Csv2hash::Main.generate_definition :name do
|
35
|
+
set_type { Definition::MAPPING }
|
36
|
+
set_header_size { 2 } # 0 by default
|
37
|
+
set_structure_rules {{ 'MaxColumns' => 2 }}
|
38
|
+
mapping do
|
39
|
+
cell position: [0,0], key: 'gender'
|
40
|
+
cell position: [1,0], key: 'name'
|
41
|
+
end
|
40
42
|
end
|
41
|
-
|
42
|
-
|
43
|
+
Csv2hash::Main[:name] # Access anywhere
|
44
|
+
```
|
43
45
|
|
44
46
|
### Rules
|
45
47
|
|
46
|
-
You should declared a definition
|
48
|
+
You should declared a definition of your CSV file, and then define for each cell what you would expect.
|
47
49
|
|
48
50
|
Example :
|
49
51
|
|
@@ -53,7 +55,7 @@ If you want the very first cell, located on the first line and on the first colu
|
|
53
55
|
cell name: 'aswering', type: 'string', values: ['yes', 'no'], position: [0,0]
|
54
56
|
```
|
55
57
|
|
56
|
-
|
58
|
+
`:type` attribute has `String` for default value, therefore you can just write this:
|
57
59
|
|
58
60
|
```
|
59
61
|
cell name: 'aswering', values: ['yes', 'no'], position: [0,0]
|
@@ -109,7 +111,7 @@ A definition should be provided. There are 2 types of definitions:
|
|
109
111
|
|
110
112
|
## Samples
|
111
113
|
|
112
|
-
### Validation of cells with defined precision
|
114
|
+
### [MAPPING] Validation of cells with defined precision
|
113
115
|
|
114
116
|
Consider the following CSV:
|
115
117
|
|
@@ -138,21 +140,21 @@ Precise position validation sample:
|
|
138
140
|
private
|
139
141
|
|
140
142
|
def definition
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
143
|
+
Main.generate_definition :my_defintion do
|
144
|
+
set_type { Definition::MAPPING }
|
145
|
+
set_header_size { 1 }
|
146
|
+
mapping do
|
147
|
+
cell position: [2,1], key: 'first_name'
|
148
|
+
cell position: [3,1], key: 'last_name'
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
150
152
|
end
|
151
153
|
|
152
154
|
end
|
153
155
|
```
|
154
156
|
|
155
|
-
### Validation of a collection (Regular CSV)
|
157
|
+
### [COLLECTION] Validation of a collection (Regular CSV)
|
156
158
|
|
157
159
|
Consider the following CSV:
|
158
160
|
|
@@ -182,18 +184,18 @@ Collection validation sample:
|
|
182
184
|
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, header_size: 1)
|
183
185
|
end
|
184
186
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
187
|
+
def definition
|
188
|
+
Main.generate_definition :my_defintion do
|
189
|
+
set_type { Definition::COLLECTION }
|
190
|
+
set_header_size { 1 }
|
191
|
+
mapping do
|
192
|
+
cell position: 0, key: 'nickname'
|
193
|
+
cell position: 1, key: 'first_name'
|
194
|
+
cell position: 2, key: 'last_name'
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
197
199
|
|
198
200
|
end
|
199
201
|
```
|
@@ -201,7 +203,7 @@ Collection validation sample:
|
|
201
203
|
### Structure validation rules
|
202
204
|
|
203
205
|
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.
|
204
|
-
Current validations are:
|
206
|
+
Current validations are: :min_columns, :max_columns
|
205
207
|
|
206
208
|
```
|
207
209
|
class MyParser
|
@@ -222,7 +224,7 @@ Current validations are: MinColumn, MaxColumn
|
|
222
224
|
Main.generate_definition :my_defintion do
|
223
225
|
set_type { Definition::COLLECTION }
|
224
226
|
set_header_size { 1 }
|
225
|
-
set_structure_rules {{
|
227
|
+
set_structure_rules {{ min_columns: 2, max_columns: 3 }}
|
226
228
|
mapping do
|
227
229
|
cell position: 0, key: 'nickname'
|
228
230
|
cell position: 1, key: 'first_name'
|
@@ -250,18 +252,18 @@ Pasrer can take several parameters like that:
|
|
250
252
|
definition, file_path_or_data, ignore_blank_line: false
|
251
253
|
```
|
252
254
|
|
253
|
-
in file_path_or_data attribute you can pass directly an Array of data (Array with 2 dimensions) really useful for testing, if you don't care about blank lines in your CSV you can ignore them.
|
255
|
+
in `file_path_or_data` attribute you can pass directly an `Array` of data (`Array` with 2 dimensions) really useful for testing, if you don't care about blank lines in your CSV you can ignore them.
|
254
256
|
|
255
257
|
### Response
|
256
258
|
|
257
|
-
The parser return values wrapper into DataWrapper Object
|
259
|
+
The parser return values wrapper into `DataWrapper Object`, you can call ```.valid?``` method on this Object and grab either data or errors like that :
|
258
260
|
|
259
261
|
```
|
260
262
|
response = parser.parse
|
261
263
|
if response.valid?
|
262
264
|
response.data
|
263
265
|
else
|
264
|
-
|
266
|
+
response.errors
|
265
267
|
end
|
266
268
|
```
|
267
269
|
|
@@ -273,7 +275,7 @@ data or errors are Array, but errors can be formatted on csv format with .to_csv
|
|
273
275
|
|
274
276
|
## Exception or Not !
|
275
277
|
|
276
|
-
You can choose into 2
|
278
|
+
You can choose into 2 differents modes of parsing, either **break_on_failure mode** for throw an exception when rule fail or **csv mode** for get csv original data + errors throwing into added extra column.
|
277
279
|
|
278
280
|
### On **BREAK_ON_FAILURE MODE**
|
279
281
|
|
@@ -337,7 +339,7 @@ errors is a Array of Hash
|
|
337
339
|
### Rule
|
338
340
|
|
339
341
|
```
|
340
|
-
|
342
|
+
cell position: [1,1], key: 'nickname', allow_blank: false
|
341
343
|
```
|
342
344
|
|
343
345
|
### Error
|
@@ -373,100 +375,13 @@ Csv data
|
|
373
375
|
[ [ 'Foo' ] ]
|
374
376
|
```
|
375
377
|
|
376
|
-
#
|
377
|
-
|
378
|
-
# Upgrading from 0.5 to 0.6
|
379
|
-
|
380
|
-
Introduce DSL
|
381
|
-
|
382
|
-
Prior to 0.6 :
|
383
|
-
|
384
|
-
```
|
385
|
-
rules = [{ position: [0,0], key: 'name' }]
|
386
|
-
Csv2hash::Definition.new(rules, Definition::MAPPING, options={})
|
387
|
-
|
388
|
-
```
|
378
|
+
# Changes
|
389
379
|
|
390
|
-
|
380
|
+
please refere to [CHANGELOG.md](https://github.com/FinalCAD/csv2hash/blob/master/CHANGELOG.md) doc
|
391
381
|
|
392
|
-
|
393
|
-
Csv2hash::Main.generate_definition :foo do
|
394
|
-
set_type { Definition::MAPPING }
|
395
|
-
mapping { cell position: [0,0], key: 'name' }
|
396
|
-
end
|
397
|
-
Csv2hash::Main[:foo] # Access anywhere
|
398
|
-
```
|
399
|
-
|
400
|
-
# Upgrading from 0.4 to 0.5
|
401
|
-
|
402
|
-
Signature of ```Csv2hash::Main#new``` has changed too
|
403
|
-
|
404
|
-
Prior to 0.5 :
|
405
|
-
|
406
|
-
```
|
407
|
-
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line=false)
|
408
|
-
```
|
409
|
-
|
410
|
-
Starting from 0.5 :
|
411
|
-
|
412
|
-
```
|
413
|
-
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line: false)
|
414
|
-
```
|
415
|
-
|
416
|
-
# Upgrading from 0.3 to 0.4
|
417
|
-
|
418
|
-
Signature of ```Csv2hash::Main#new``` has changed too
|
419
|
-
|
420
|
-
Prior to 0.4 :
|
421
|
-
|
422
|
-
```
|
423
|
-
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
424
|
-
```
|
425
|
-
|
426
|
-
call ```.parse!``` for same result
|
427
|
-
|
428
|
-
Starting from 0.4 :
|
429
|
-
|
430
|
-
```
|
431
|
-
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line=false)
|
432
|
-
```
|
433
|
-
|
434
|
-
call ```.parse``` for same result
|
435
|
-
|
436
|
-
# Upgrading from 0.2 to 0.3
|
437
|
-
|
438
|
-
```Csv2hash``` become an ```Module```, ```Csv2hash.new``` no longer works, please use ```Csv2hash::Main.new``` instead.
|
439
|
-
Signature of ```Csv2hash::Main#new``` has changed too
|
440
|
-
|
441
|
-
Prior to 0.3 :
|
442
|
-
|
443
|
-
```
|
444
|
-
Csv2Hash.new(definition, file_path, break_on_failure=true, data_source=nil, ignore_blank_line=false)
|
445
|
-
```
|
446
|
-
|
447
|
-
Starting from 0.3 :
|
448
|
-
|
449
|
-
```
|
450
|
-
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
451
|
-
```
|
452
|
-
|
453
|
-
# Upgrading from 0.1 to 0.2
|
454
|
-
|
455
|
-
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 :
|
456
|
-
|
457
|
-
Prior to 0.2 :
|
458
|
-
|
459
|
-
```
|
460
|
-
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, 1)
|
461
|
-
```
|
462
|
-
|
463
|
-
Starting from 0.2 :
|
464
|
-
|
465
|
-
```
|
466
|
-
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, header_size: 1)
|
467
|
-
```
|
382
|
+
# Upgrading
|
468
383
|
|
469
|
-
|
384
|
+
please refere to [UPGRADE.md](https://github.com/FinalCAD/csv2hash/blob/master/UPGRADE.md) doc
|
470
385
|
|
471
386
|
## Contributing
|
472
387
|
|
data/UPGRADE.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Upgrading
|
2
|
+
|
3
|
+
# Upgrading from 0.5 to 0.6
|
4
|
+
|
5
|
+
Introduce DSL
|
6
|
+
|
7
|
+
Prior to 0.6 :
|
8
|
+
|
9
|
+
```
|
10
|
+
rules = [{ position: [0,0], key: 'name' }]
|
11
|
+
Csv2hash::Definition.new(rules, Definition::MAPPING, options={})
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
Starting from 0.6 :
|
16
|
+
|
17
|
+
```
|
18
|
+
Csv2hash::Main.generate_definition :foo do
|
19
|
+
set_type { Definition::MAPPING }
|
20
|
+
mapping { cell position: [0,0], key: 'name' }
|
21
|
+
end
|
22
|
+
Csv2hash::Main[:foo] # Access anywhere
|
23
|
+
```
|
24
|
+
|
25
|
+
# Upgrading from 0.4 to 0.5
|
26
|
+
|
27
|
+
Signature of ```Csv2hash::Main#new``` has changed too
|
28
|
+
|
29
|
+
Prior to 0.5 :
|
30
|
+
|
31
|
+
```
|
32
|
+
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line=false)
|
33
|
+
```
|
34
|
+
|
35
|
+
Starting from 0.5 :
|
36
|
+
|
37
|
+
```
|
38
|
+
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line: false)
|
39
|
+
```
|
40
|
+
|
41
|
+
# Upgrading from 0.3 to 0.4
|
42
|
+
|
43
|
+
Signature of ```Csv2hash::Main#new``` has changed too
|
44
|
+
|
45
|
+
Prior to 0.4 :
|
46
|
+
|
47
|
+
```
|
48
|
+
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
49
|
+
```
|
50
|
+
|
51
|
+
call ```.parse!``` for same result
|
52
|
+
|
53
|
+
Starting from 0.4 :
|
54
|
+
|
55
|
+
```
|
56
|
+
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line=false)
|
57
|
+
```
|
58
|
+
|
59
|
+
call ```.parse``` for same result
|
60
|
+
|
61
|
+
# Upgrading from 0.2 to 0.3
|
62
|
+
|
63
|
+
```Csv2hash``` become an ```Module```, ```Csv2hash.new``` no longer works, please use ```Csv2hash::Main.new``` instead.
|
64
|
+
Signature of ```Csv2hash::Main#new``` has changed too
|
65
|
+
|
66
|
+
Prior to 0.3 :
|
67
|
+
|
68
|
+
```
|
69
|
+
Csv2Hash.new(definition, file_path, break_on_failure=true, data_source=nil, ignore_blank_line=false)
|
70
|
+
```
|
71
|
+
|
72
|
+
Starting from 0.3 :
|
73
|
+
|
74
|
+
```
|
75
|
+
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
76
|
+
```
|
77
|
+
|
78
|
+
# Upgrading from 0.1 to 0.2
|
79
|
+
|
80
|
+
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 :
|
81
|
+
|
82
|
+
Prior to 0.2 :
|
83
|
+
|
84
|
+
```
|
85
|
+
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, 1)
|
86
|
+
```
|
87
|
+
|
88
|
+
Starting from 0.2 :
|
89
|
+
|
90
|
+
```
|
91
|
+
Csv2Hash::Definition.new(rules, type = Csv2Hash::Definition::COLLECTION, header_size: 1)
|
92
|
+
```
|
93
|
+
|
94
|
+
If no configuration is passed, header_size defaults remains to 0
|
data/bin/launch_irb
CHANGED
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Csv2hash
|
2
|
+
module StructureValidator
|
3
|
+
module Deprecation
|
4
|
+
|
5
|
+
OLD_MAX_COLUMN = 'MaxColumns'.freeze
|
6
|
+
OLD_MIN_COLUMN = 'MinColumns'.freeze
|
7
|
+
OLD_RULES_NAME = [ OLD_MIN_COLUMN, OLD_MAX_COLUMN ]
|
8
|
+
NEW_SYNTAX = { OLD_MIN_COLUMN => 'min_columns', OLD_MAX_COLUMN => 'max_columns' }
|
9
|
+
|
10
|
+
def check_params rule
|
11
|
+
if OLD_RULES_NAME.include? rule
|
12
|
+
warn "[DEPRECATION]: `#{rule}` is deprecated. Please use `#{NEW_SYNTAX[rule]}` instead."
|
13
|
+
NEW_SYNTAX[rule]
|
14
|
+
else
|
15
|
+
rule.to_s
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
|
-
module Csv2hash
|
2
|
-
|
1
|
+
module Csv2hash
|
2
|
+
module StructureValidator
|
3
|
+
class MaxColumns
|
3
4
|
|
4
|
-
|
5
|
+
include Validator
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def initialize max_size
|
8
|
+
@max_size = max_size
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def validate_line line
|
12
|
+
line.size > @max_size
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
def error_message line
|
16
|
+
"Too many columns (max. #{@max_size}) on line #{line}"
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
|
-
end
|
20
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
|
-
module Csv2hash
|
2
|
-
|
1
|
+
module Csv2hash
|
2
|
+
module StructureValidator
|
3
|
+
class MinColumns
|
3
4
|
|
4
|
-
|
5
|
+
include Validator
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def initialize min_size
|
8
|
+
@min_size = min_size
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def validate_line line
|
12
|
+
line.size < @min_size
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
def error_message line
|
16
|
+
"Not enough columns (min. #{@min_size}) on line #{line}"
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
|
-
end
|
20
|
+
end
|
@@ -1,31 +1,45 @@
|
|
1
|
-
|
1
|
+
require 'active_support/core_ext'
|
2
2
|
|
3
|
-
|
4
|
-
class ValidationError < StandardError ; end
|
3
|
+
require_relative 'structure_validator/deprecation'
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
module Csv2hash
|
6
|
+
module StructureValidator
|
7
|
+
include Deprecation
|
8
|
+
|
9
|
+
class ValidationError < StandardError ; end
|
10
|
+
|
11
|
+
MAX_COLUMN = 'max_columns'.freeze
|
12
|
+
MIN_COLUMN = 'min_columns'.freeze
|
13
|
+
RULES_NAME = [ MIN_COLUMN, MAX_COLUMN ]
|
14
|
+
|
15
|
+
def validate_structure!
|
16
|
+
definition.structure_rules.each do |rule, options|
|
17
|
+
begin
|
18
|
+
rule_instance(rule, options).validate! data_source
|
19
|
+
rescue => e
|
20
|
+
self.errors << { y: nil, x: nil, message: e.message, key: nil }
|
21
|
+
raise if break_on_failure
|
22
|
+
end
|
14
23
|
end
|
15
24
|
end
|
16
|
-
end
|
17
25
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
26
|
+
def rule_instance rule, options
|
27
|
+
_rule = check_params rule
|
28
|
+
begin
|
29
|
+
StructureValidator.const_get(_rule.camelize).new(options)
|
30
|
+
rescue NameError => e
|
31
|
+
raise "Structure rule #{rule} unknow, please use one of these #{RULES_NAME}"
|
32
|
+
end
|
33
|
+
end
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
35
|
+
module Validator
|
36
|
+
def validate! source
|
37
|
+
source.index { |line| validate_line line }.tap do |line|
|
38
|
+
raise ValidationError, error_message(line) unless line.nil?
|
39
|
+
end
|
40
|
+
true
|
27
41
|
end
|
28
|
-
true
|
29
42
|
end
|
43
|
+
|
30
44
|
end
|
31
45
|
end
|
data/lib/csv2hash/version.rb
CHANGED
@@ -18,7 +18,7 @@ module Csv2hash
|
|
18
18
|
|
19
19
|
context 'the csv with errors' do
|
20
20
|
before do
|
21
|
-
allow(definition).to receive(:structure_rules) {{
|
21
|
+
allow(definition).to receive(:structure_rules) {{ max_columns: 2 }}
|
22
22
|
subject.parse
|
23
23
|
end
|
24
24
|
let(:data_source) do
|
@@ -30,13 +30,14 @@ module Csv2hash
|
|
30
30
|
|
31
31
|
its(:csv_with_errors) { should be_kind_of CsvArray }
|
32
32
|
it "adds structure error in first cell" do
|
33
|
+
|
33
34
|
expect(subject.csv_with_errors.first[:message]).to eq 'Too many columns (max. 2) on line 1'
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
38
|
context '#MaxColumns' do
|
38
39
|
before do
|
39
|
-
allow(definition).to receive(:structure_rules) {{
|
40
|
+
allow(definition).to receive(:structure_rules) {{ max_columns: 2 }}
|
40
41
|
allow(subject).to receive(:break_on_failure) { true }
|
41
42
|
end
|
42
43
|
|
@@ -62,7 +63,7 @@ module Csv2hash
|
|
62
63
|
|
63
64
|
context '#MinColumns' do
|
64
65
|
before do
|
65
|
-
allow(definition).to receive(:structure_rules) {{
|
66
|
+
allow(definition).to receive(:structure_rules) {{ min_columns: 2 }}
|
66
67
|
allow(subject).to receive(:break_on_failure) { true }
|
67
68
|
end
|
68
69
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv2hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel AZEMAR
|
@@ -92,12 +92,13 @@ files:
|
|
92
92
|
- ".gitignore"
|
93
93
|
- ".rspec"
|
94
94
|
- ".travis.yml"
|
95
|
+
- CHANGELOG.md
|
95
96
|
- Gemfile
|
96
97
|
- Gemfile.lock
|
97
98
|
- LICENSE
|
98
99
|
- README.md
|
99
100
|
- Rakefile
|
100
|
-
-
|
101
|
+
- UPGRADE.md
|
101
102
|
- bin/launch_irb
|
102
103
|
- bin/load_rvm
|
103
104
|
- coverage/.resultset.json.lock
|
@@ -119,6 +120,7 @@ files:
|
|
119
120
|
- lib/csv2hash/parser/mapping.rb
|
120
121
|
- lib/csv2hash/registry.rb
|
121
122
|
- lib/csv2hash/structure_validator.rb
|
123
|
+
- lib/csv2hash/structure_validator/deprecation.rb
|
122
124
|
- lib/csv2hash/structure_validator/max_columns.rb
|
123
125
|
- lib/csv2hash/structure_validator/min_columns.rb
|
124
126
|
- lib/csv2hash/validator.rb
|