csv2hash 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d373a399e6b36122e3188d8ba7078b8f3c7de1f
4
- data.tar.gz: 537b8221207eacd8c6f8294b7e192a52a9bc85ee
3
+ metadata.gz: 47f29eda9ebb4f6b019424849700862ea05ec24a
4
+ data.tar.gz: 52e81b5c37ae18cb65922444637033feab9e470c
5
5
  SHA512:
6
- metadata.gz: ab5e7774d96a99805fec0357004a63970c4162f7773a0f5d31e5334ad0b43a2a96e8559ebc0a433368640ca6ae8f7072af429254f9ff396f926056534a52fd9f
7
- data.tar.gz: d86e84ed44b3d9ef11750a33fdff532fdf2ae4a98f44ba3eae4e9b722e31d928d3bd4830d4fcdd961342a2cc03d24cadd70cbc18fb0dd3b75fdceda99faf40f4
6
+ metadata.gz: 8353d11495c3b9f3a0d1f5e1daf90604459123f982adadff9022ce63255d21519a17b6b9abb1453cba7f68d007c5a990f970eda0b58c6cc05e7562f4a169c857
7
+ data.tar.gz: ed96cc24f8ea73e0f862a97a888509235fec77998005f52c500648ec75e6a820367533b2401d1ba43a7f693fdffb8cbf7b9acc07b15cdc31c7855a66b0236092
data/.travis.yml CHANGED
@@ -7,3 +7,4 @@ rvm:
7
7
  matrix:
8
8
  allow_failures:
9
9
  - rvm: ruby-head
10
+ - rvm: 2.1.0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ### VERSION 0.7.3
2
+
3
+ * enhancements
4
+ * add sanitizer
5
+
6
+ * [fullchanges](https://github.com/FinalCAD/csv2hash/pull/24)
7
+ * [Author Joel AZEMAR](https://github.com/joel)
8
+
1
9
  ### VERSION 0.7.2
2
10
 
3
11
  * bug fix
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv2hash (0.7.2)
4
+ csv2hash (0.7.3)
5
5
  activesupport (~> 4.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -85,6 +85,15 @@ end.parse
85
85
  List of options :
86
86
 
87
87
  * `ignore_blank_line: :boolean` # i think is pretty straightforward to understand
88
+ * `sanitizer: :boolean` remove trailing space, example:
89
+
90
+ ```
91
+ ' it is a really good idea!! '
92
+ ```
93
+ become
94
+ ```
95
+ => "it is a really good idea!!"
96
+ ```
88
97
 
89
98
  ### Definition DSL
90
99
 
@@ -305,13 +314,13 @@ This is a special feature for finding a specific column index on header. For exa
305
314
  | Name | Age |
306
315
  |---------------|---------------|
307
316
  | John Doe | 23 |
308
- | Jane Doe | 28 |
309
- | | |
317
+ | Jane Doe | 28 |
318
+ | | |
310
319
  | | |
311
320
  ```
312
321
 
313
- You want to extract `Name` and `Age` for all rows but you want the order of the columns to be able to change.
314
- You change the position to the regex of column index you are looking for. So this how the position
322
+ You want to extract `Name` and `Age` for all rows but you want the order of the columns to be able to change.
323
+ You change the position to the regex of column index you are looking for. So this how the position
315
324
 
316
325
  ```
317
326
  cell position: 0, key: 'name'
@@ -1,4 +1,16 @@
1
1
  module Csv2hash
2
2
  module Parser
3
+
4
+ def treat(content)
5
+ return content unless self.options.fetch(:sanitizer){false}
6
+ sanitize(content)
7
+ end
8
+
9
+ private
10
+
11
+ def sanitize(content)
12
+ return content unless content.is_a?(String)
13
+ content.gsub(/[\s]+/, ' ').strip
14
+ end
3
15
  end
4
16
  end
@@ -24,9 +24,9 @@ module Csv2hash
24
24
  x = cell.rules.fetch :position
25
25
  if (nested = cell.rules.fetch :nested)
26
26
  parsed_data[nested] ||= {}
27
- parsed_data[nested][cell.rules.fetch(:key)] = source_data[x]
27
+ parsed_data[nested][cell.rules.fetch(:key)] = treat(source_data[x])
28
28
  else
29
- parsed_data[cell.rules.fetch(:key)] = source_data[x]
29
+ parsed_data[cell.rules.fetch(:key)] = treat(source_data[x])
30
30
  end
31
31
  end
32
32
  end
@@ -18,9 +18,9 @@ module Csv2hash
18
18
  y, x = cell.rules.fetch :position
19
19
  if (nested = cell.rules.fetch :nested)
20
20
  parsed_data[nested] ||= {}
21
- parsed_data[nested][cell.rules.fetch(:key)] = source_data[y][x]
21
+ parsed_data[nested][cell.rules.fetch(:key)] = treat(source_data[y][x])
22
22
  else
23
- parsed_data[cell.rules.fetch(:key)] = source_data[y][x]
23
+ parsed_data[cell.rules.fetch(:key)] = treat(source_data[y][x])
24
24
  end
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module Csv2hash
2
- VERSION = '0.7.2'
2
+ VERSION = '0.7.3'
3
3
  end
@@ -1,3 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Csv2hash::Parser
3
+ module Csv2hash
4
+ describe Parser do
5
+ let(:definition) do
6
+ Main.generate_definition :foo do
7
+ set_type { Definition::COLLECTION }
8
+ mapping { cell position: 0, key: 'name' }
9
+ end
10
+ end
11
+
12
+ let(:john) { ' John Doe ' }
13
+ let(:jane) { ' Jane Doe ' }
14
+ let(:data_source) { [ [ john ], [ jane ] ] }
15
+ let(:ignore_blank_line) { false }
16
+
17
+ subject do
18
+ Main.new(definition, data_source, ignore_blank_line: ignore_blank_line, sanitizer: sanitizer)
19
+ end
20
+
21
+ context 'regular way' do
22
+ let(:sanitizer) { false }
23
+ it {
24
+ expect(subject.tap do |parser|
25
+ parser.parse!
26
+ end.data).to eql({ data: [ { 'name' => john }, { 'name' => jane } ] })
27
+ }
28
+ end
29
+
30
+ context 'sanitizer way' do
31
+ let(:sanitizer) { true }
32
+ it {
33
+ expect(subject.tap do |parser|
34
+ parser.parse!
35
+ end.data).to eql({ data: [ { 'name' => 'John Doe' }, { 'name' => 'Jane Doe' } ] })
36
+ }
37
+ end
38
+ end
39
+ end
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.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel AZEMAR
@@ -182,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  version: '0'
183
183
  requirements: []
184
184
  rubyforge_project:
185
- rubygems_version: 2.4.2
185
+ rubygems_version: 2.2.2
186
186
  signing_key:
187
187
  specification_version: 4
188
188
  summary: Mapping a CSV to a Ruby Hash