csv2hash 0.7.2 → 0.7.3
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/.travis.yml +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +13 -4
- data/lib/csv2hash/parser.rb +12 -0
- data/lib/csv2hash/parser/collection.rb +2 -2
- data/lib/csv2hash/parser/mapping.rb +2 -2
- data/lib/csv2hash/version.rb +1 -1
- data/spec/csv2hash/parser_spec.rb +37 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47f29eda9ebb4f6b019424849700862ea05ec24a
|
4
|
+
data.tar.gz: 52e81b5c37ae18cb65922444637033feab9e470c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8353d11495c3b9f3a0d1f5e1daf90604459123f982adadff9022ce63255d21519a17b6b9abb1453cba7f68d007c5a990f970eda0b58c6cc05e7562f4a169c857
|
7
|
+
data.tar.gz: ed96cc24f8ea73e0f862a97a888509235fec77998005f52c500648ec75e6a820367533b2401d1ba43a7f693fdffb8cbf7b9acc07b15cdc31c7855a66b0236092
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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'
|
data/lib/csv2hash/parser.rb
CHANGED
@@ -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
|
data/lib/csv2hash/version.rb
CHANGED
@@ -1,3 +1,39 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
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.
|
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.
|
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
|