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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 712c36523387610b2faba0dfb934e772a281ee19
4
- data.tar.gz: 9275f05691e92eda1e32f08395bae382282515fd
3
+ metadata.gz: 388884c9854027a36c9f704d77f8cd31d2fe8aba
4
+ data.tar.gz: ffb86ac1d924c8cb6041aec965c42edf19b1226d
5
5
  SHA512:
6
- metadata.gz: f37e0262f38b234f2b0b092f8e9f9a048e43e91dbd732d27e83911097989956567cc9482547ed868bdaf3240d5f149f55024b8721d2d5c5b24365a610ab239c6
7
- data.tar.gz: 5d0bf911880893b7af380c61b9339bd6820a82b862bc2af4214e1453ca1abe7013b85a837a467d1ca8f491c6f0fad7bdc86bca74382e627d20bc8faa189b204b
6
+ metadata.gz: fa1f10d91cb064ae715673074dc4bfd8e095c79cee29efa8cee1db95556a2ac909bd7f3337024c8121f5972fe87d9911a3a921566195859862b5d0f4c832880b
7
+ data.tar.gz: 710c7f822206b531de694f316ab2f2a5d0c76afe83a0d44f92816054f519b20e9fd32d70b5da0865481c6770f0f2319519e52203c5fc8d0ada551abd565ebf15
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv2hash (0.3.0)
4
+ csv2hash (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
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, exception_mode=true, ignore_blank_line=false
230
+ definition, file_path_or_data, ignore_blank_line=false
231
231
  ```
232
232
 
233
- in file_path_or_data attribubte 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.
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 **exception mode** for raise exception in first breaking rules or **csv mode** for get csv original data + errors throwing into added columns.
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 **CSV MODE** you can choose different way for manage errors
258
+ ### On **BREAK_ON_FAILURE MODE**
259
259
 
260
- `.parse()` return `data_wrapper` if `.parse()` is invalid, you can code your own behavior:
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, exception_mode=true, ignore_blank_line=false).new
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, exception_mode=true, data_source=nil, ignore_blank_line=false)
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, exception_mode=true, ignore_blank_line=false)
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
@@ -9,7 +9,7 @@ module Csv2hash::StructureValidator
9
9
  rule_instance(rule, options).validate! data_source
10
10
  rescue => e
11
11
  self.errors << { y: nil, x: nil, message: e.message, key: nil }
12
- raise if exception_mode
12
+ raise if break_on_failure
13
13
  end
14
14
  end
15
15
  end
@@ -8,7 +8,7 @@ module Csv2hash
8
8
  validate_cell (_y||y), x, rule
9
9
  rescue => e
10
10
  self.errors << { y: (_y||y), x: x, message: e.message, key: rule.fetch(:key) }
11
- raise if exception_mode
11
+ raise if break_on_failure
12
12
  end
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module Csv2hash
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
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, :exception_mode, :errors, :ignore_blank_line
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, exception_mode=true, ignore_blank_line=false
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.exception_mode, self.errors = exception_mode, []
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, exception_mode=false, ignore_blank_line=false)
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, exception_mode=false, ignore_blank_line=false)
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, exception_mode, ignore_blank_line=false)
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, exception_mode=true, ignore_blank_line=false)
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.exception_mode = false }
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
- before { subject.exception_mode = false }
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, exception_mode=true, ignore_blank_line=false)
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
- before { subject.exception_mode = false }
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
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel AZEMAR