csv2hash 0.3.0 → 0.4.0
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/Gemfile.lock +1 -1
- data/README.md +31 -8
- data/ReleaseNotes.md +8 -0
- data/lib/csv2hash/structure_validator.rb +1 -1
- data/lib/csv2hash/validator.rb +1 -1
- data/lib/csv2hash/version.rb +1 -1
- data/lib/csv2hash.rb +10 -3
- data/spec/csv2hash/parser/collection_spec.rb +1 -1
- data/spec/csv2hash/parser/mapping_spec.rb +1 -1
- data/spec/csv2hash/structure_validator_spec.rb +9 -3
- data/spec/csv2hash/validator/collection_spec.rb +12 -3
- data/spec/csv2hash/validator/mapping_spec.rb +10 -2
- 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: 388884c9854027a36c9f704d77f8cd31d2fe8aba
|
4
|
+
data.tar.gz: ffb86ac1d924c8cb6041aec965c42edf19b1226d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa1f10d91cb064ae715673074dc4bfd8e095c79cee29efa8cee1db95556a2ac909bd7f3337024c8121f5972fe87d9911a3a921566195859862b5d0f4c832880b
|
7
|
+
data.tar.gz: 710c7f822206b531de694f316ab2f2a5d0c76afe83a0d44f92816054f519b20e9fd32d70b5da0865481c6770f0f2319519e52203c5fc8d0ada551abd565ebf15
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -227,10 +227,10 @@ You can define the number of rows to skip in the header of the CSV.
|
|
227
227
|
Pasrer can take several parameters like that:
|
228
228
|
|
229
229
|
```
|
230
|
-
definition, file_path_or_data,
|
230
|
+
definition, file_path_or_data, ignore_blank_line=false
|
231
231
|
```
|
232
232
|
|
233
|
-
in file_path_or_data
|
233
|
+
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.
|
234
234
|
|
235
235
|
### Response
|
236
236
|
|
@@ -253,16 +253,20 @@ data or errors are Array, but errors can be formatted on csv format with .to_csv
|
|
253
253
|
|
254
254
|
## Exception or Not !
|
255
255
|
|
256
|
-
You can choose into 2 different modes of parsing, either **
|
256
|
+
You can choose into 2 different modes of parsing, either **break_on_failure mode** for raise exception in first breaking rules or **csv mode** for get csv original data + errors throwing into added columns.
|
257
257
|
|
258
|
-
### On **
|
258
|
+
### On **BREAK_ON_FAILURE MODE**
|
259
259
|
|
260
|
-
|
260
|
+
You need call ```.parse()``` with bang ```!```
|
261
|
+
|
262
|
+
### On **CSV MODE**
|
263
|
+
|
264
|
+
You need call `.parse()` return `data_wrapper` if `.parse()` is invalid, you can code your own behavior:
|
261
265
|
|
262
266
|
in your code
|
263
267
|
|
264
268
|
```
|
265
|
-
parser = Csv2hash::Main.new(definition, file_path_or_data,
|
269
|
+
parser = Csv2hash::Main.new(definition, file_path_or_data, ignore_blank_line=false).new
|
266
270
|
response = parser.parse
|
267
271
|
return response if response.valid?
|
268
272
|
# Whatever
|
@@ -348,6 +352,25 @@ Csv data
|
|
348
352
|
```
|
349
353
|
[ [ 'Foo' ] ]
|
350
354
|
```
|
355
|
+
# Upgrading from 0.3 to 0.4
|
356
|
+
|
357
|
+
Signature of ```Csv2hash::Main#new``` has changed too
|
358
|
+
|
359
|
+
Prior to 0.3 :
|
360
|
+
|
361
|
+
```
|
362
|
+
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
363
|
+
```
|
364
|
+
|
365
|
+
call ```.parse!``` for same result
|
366
|
+
|
367
|
+
Starting from 0.3 :
|
368
|
+
|
369
|
+
```
|
370
|
+
Csv2Hash::Main.new(definition, file_path_or_data, ignore_blank_line=false)
|
371
|
+
```
|
372
|
+
|
373
|
+
call ```.parse``` for same result
|
351
374
|
|
352
375
|
# Upgrading from 0.2 to 0.3
|
353
376
|
|
@@ -357,13 +380,13 @@ Signature of ```Csv2hash::Main#new``` has changed too
|
|
357
380
|
Prior to 0.3 :
|
358
381
|
|
359
382
|
```
|
360
|
-
Csv2Hash.new(definition, file_path,
|
383
|
+
Csv2Hash.new(definition, file_path, break_on_failure=true, data_source=nil, ignore_blank_line=false)
|
361
384
|
```
|
362
385
|
|
363
386
|
Starting from 0.3 :
|
364
387
|
|
365
388
|
```
|
366
|
-
Csv2Hash::Main.new(definition, file_path_or_data,
|
389
|
+
Csv2Hash::Main.new(definition, file_path_or_data, break_on_failure=true, ignore_blank_line=false)
|
367
390
|
```
|
368
391
|
|
369
392
|
# Upgrading from 0.1 to 0.2
|
data/ReleaseNotes.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
### VERSION 0.4.0
|
2
|
+
|
3
|
+
* backwards incompatible changes
|
4
|
+
* The signature of Csv2hash::Main#new has changed.
|
5
|
+
|
6
|
+
* refactoring
|
7
|
+
* remove params ``` break_on_failure ``` to benefit of ``` Csv2hash::Main#parse! ``` of ``` Csv2hash::Main#parse ```
|
8
|
+
|
1
9
|
### VERSION 0.3.0
|
2
10
|
|
3
11
|
* backwards incompatible changes
|
data/lib/csv2hash/validator.rb
CHANGED
data/lib/csv2hash/version.rb
CHANGED
data/lib/csv2hash.rb
CHANGED
@@ -24,13 +24,13 @@ module Csv2hash
|
|
24
24
|
class Main
|
25
25
|
include Csv2hash::StructureValidator
|
26
26
|
|
27
|
-
attr_accessor :definition, :file_path_or_data, :data, :notifier, :
|
27
|
+
attr_accessor :definition, :file_path_or_data, :data, :notifier, :break_on_failure, :errors, :ignore_blank_line
|
28
28
|
|
29
|
-
def initialize definition, file_path_or_data,
|
29
|
+
def initialize definition, file_path_or_data, ignore_blank_line=false
|
30
30
|
self.definition, self.file_path_or_data = definition, file_path_or_data
|
31
31
|
@data_source = data_source
|
32
32
|
dynamic_lib_loading 'Parser'
|
33
|
-
self.
|
33
|
+
self.break_on_failure, self.errors = false, []
|
34
34
|
dynamic_lib_loading 'Validator'
|
35
35
|
self.notifier = Notifier.new
|
36
36
|
self.ignore_blank_line = ignore_blank_line
|
@@ -47,6 +47,13 @@ module Csv2hash
|
|
47
47
|
rescue; end
|
48
48
|
end
|
49
49
|
|
50
|
+
def parse!
|
51
|
+
self.break_on_failure = true
|
52
|
+
parse
|
53
|
+
ensure
|
54
|
+
self.break_on_failure = false
|
55
|
+
end
|
56
|
+
|
50
57
|
def parse
|
51
58
|
load_data_source
|
52
59
|
|
@@ -9,7 +9,7 @@ describe Csv2hash::Parser::Collection do
|
|
9
9
|
let(:data_source) { [ [ 'John Doe' ], [ 'Jane Doe' ] ] }
|
10
10
|
|
11
11
|
subject do
|
12
|
-
Csv2hash::Main.new(definition, data_source,
|
12
|
+
Csv2hash::Main.new(definition, data_source, ignore_blank_line=false)
|
13
13
|
end
|
14
14
|
|
15
15
|
context 'regular way' do
|
@@ -9,7 +9,7 @@ describe Csv2hash::Parser::Mapping do
|
|
9
9
|
let(:data_source) { [ [ 'John Doe' ] ] }
|
10
10
|
|
11
11
|
subject do
|
12
|
-
Csv2hash::Main.new(definition, data_source,
|
12
|
+
Csv2hash::Main.new(definition, data_source, ignore_blank_line=false)
|
13
13
|
end
|
14
14
|
|
15
15
|
context 'regular way' do
|
@@ -4,7 +4,6 @@ describe Csv2hash::StructureValidator do
|
|
4
4
|
|
5
5
|
let(:rules) { [ { position: [0,0], key: 'name' } ] }
|
6
6
|
let(:options) { {} }
|
7
|
-
let(:exception_mode) { true }
|
8
7
|
let(:definition) do
|
9
8
|
Csv2hash::Definition.new(rules, Csv2hash::Definition::MAPPING, options).tap do |definition|
|
10
9
|
definition.validate!
|
@@ -13,12 +12,11 @@ describe Csv2hash::StructureValidator do
|
|
13
12
|
end
|
14
13
|
|
15
14
|
subject do
|
16
|
-
Csv2hash::Main.new(definition, data_source,
|
15
|
+
Csv2hash::Main.new(definition, data_source, ignore_blank_line=false)
|
17
16
|
end
|
18
17
|
|
19
18
|
context 'the csv with errors' do
|
20
19
|
let(:options){ { structure_rules: { 'MaxColumns' => 2 } } }
|
21
|
-
let(:exception_mode) { false }
|
22
20
|
before { subject.parse }
|
23
21
|
let(:data_source) do
|
24
22
|
[
|
@@ -37,6 +35,10 @@ describe Csv2hash::StructureValidator do
|
|
37
35
|
context '#MaxColumns' do
|
38
36
|
let(:options){ { structure_rules: { 'MaxColumns' => 2 } } }
|
39
37
|
|
38
|
+
before do
|
39
|
+
allow(subject).to receive(:break_on_failure) { true }
|
40
|
+
end
|
41
|
+
|
40
42
|
context 'valid data' do
|
41
43
|
let(:data_source) do
|
42
44
|
[
|
@@ -60,6 +62,10 @@ describe Csv2hash::StructureValidator do
|
|
60
62
|
context '#MinColumns' do
|
61
63
|
let(:options){ { structure_rules: { 'MinColumns' => 2 } } }
|
62
64
|
|
65
|
+
before do
|
66
|
+
allow(subject).to receive(:break_on_failure) { true }
|
67
|
+
end
|
68
|
+
|
63
69
|
context 'valid data' do
|
64
70
|
let(:data_source) do
|
65
71
|
[
|
@@ -10,11 +10,16 @@ describe Csv2hash::Validator::Collection do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
subject do
|
13
|
-
Csv2hash::Main.new(definition, data_source,
|
13
|
+
Csv2hash::Main.new(definition, data_source, ignore_blank_line=false)
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
allow(subject).to receive(:break_on_failure) { true }
|
14
18
|
end
|
15
19
|
|
16
20
|
context 'with valid data' do
|
17
21
|
let(:data_source) { [ [ 'John Doe' ] ]}
|
22
|
+
|
18
23
|
it { expect { subject.validate_data! }.to_not raise_error }
|
19
24
|
context 'with header' do
|
20
25
|
let(:options) { { header_size: 1 } }
|
@@ -28,7 +33,7 @@ describe Csv2hash::Validator::Collection do
|
|
28
33
|
before { subject.ignore_blank_line = true }
|
29
34
|
it { expect { subject.validate_data! }.to_not raise_error }
|
30
35
|
context 'csv mode' do
|
31
|
-
before { subject.
|
36
|
+
before { subject.break_on_failure = false }
|
32
37
|
its(:errors) { should be_empty }
|
33
38
|
end
|
34
39
|
end
|
@@ -45,7 +50,11 @@ describe Csv2hash::Validator::Collection do
|
|
45
50
|
|
46
51
|
context 'wihtout exception' do
|
47
52
|
let(:data_source) { [ [ ] ]}
|
48
|
-
|
53
|
+
|
54
|
+
before do
|
55
|
+
allow(subject).to receive(:break_on_failure) { false }
|
56
|
+
end
|
57
|
+
|
49
58
|
it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" }
|
50
59
|
|
51
60
|
context 'errors should be filled' do
|
@@ -10,7 +10,11 @@ describe Csv2hash::Validator::Mapping do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
subject do
|
13
|
-
Csv2hash::Main.new(definition, data_source,
|
13
|
+
Csv2hash::Main.new(definition, data_source, ignore_blank_line=false)
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
allow(subject).to receive(:break_on_failure) { true }
|
14
18
|
end
|
15
19
|
|
16
20
|
context 'with valid data' do
|
@@ -25,7 +29,11 @@ describe Csv2hash::Validator::Mapping do
|
|
25
29
|
|
26
30
|
context 'wihtout exception' do
|
27
31
|
let(:data_source) { [ [ ] ] }
|
28
|
-
|
32
|
+
|
33
|
+
before do
|
34
|
+
allow(subject).to receive(:break_on_failure) { false }
|
35
|
+
end
|
36
|
+
|
29
37
|
it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" }
|
30
38
|
|
31
39
|
context 'errors should be filled' do
|