csv2hash 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +30 -1
- data/UPGRADE.md +8 -0
- data/lib/csv2hash/definition.rb +0 -3
- data/lib/csv2hash/discover.rb +16 -0
- data/lib/csv2hash/parser/mapping.rb +23 -19
- data/lib/csv2hash/validator.rb +6 -2
- data/lib/csv2hash/version.rb +1 -1
- data/spec/csv2hash/parser/mapping_spec.rb +31 -6
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cff06b049e0b90d0627bced7665e5212667a128
|
4
|
+
data.tar.gz: 6aaf21e5a862915914813ee0d65495b04f041fc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1951755e2eda195942d6abaacaff5b2bf859a3e46e5f36160640dc3035ff1f9e81e903f991fb81e8b8cc29b3d4046766183b8f983fe244e774b05e1381c8d934
|
7
|
+
data.tar.gz: f7ab7b90d43cbec0a3379ec8461b9a41f45c3152d90c3e8106bd01275a53d98ed81b68cdf0bd3581ada17e97d4c75febf98491aebeb23fbaf5036ad766023772
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -121,7 +121,6 @@ Consider the following CSV:
|
|
121
121
|
| First Name | John | yes |
|
122
122
|
| Last Name | Doe | yes |
|
123
123
|
|
124
|
-
|
125
124
|
Precise position validation sample:
|
126
125
|
|
127
126
|
```
|
@@ -154,6 +153,36 @@ Precise position validation sample:
|
|
154
153
|
end
|
155
154
|
```
|
156
155
|
|
156
|
+
### Auto discover position
|
157
|
+
|
158
|
+
This is a special feature for finding the Y index of row where you data start. For instance you have this following data :
|
159
|
+
|
160
|
+
|---------------|---------------|------|-----|
|
161
|
+
| Nickname | jo | | |
|
162
|
+
| First Name | John | | |
|
163
|
+
| Last Name | Doe | | |
|
164
|
+
| | | | |
|
165
|
+
| Employment | CEO | | |
|
166
|
+
| Post | Doe | | |
|
167
|
+
| | | | |
|
168
|
+
| | Personal info | Age | 26 |
|
169
|
+
| | Sex | Male | |
|
170
|
+
| | | | |
|
171
|
+
|
172
|
+
You want extract `Employment` information and `Personal info` but we do not know if extra information will not come and break our index. This feature can be useful is this case.
|
173
|
+
|
174
|
+
You must change Y position (rows) by the column index and regex, the parser will search on this column the index row of this regex, here our rule :
|
175
|
+
|
176
|
+
```
|
177
|
+
cell position: [4,1], key: 'employment'
|
178
|
+
```
|
179
|
+
|
180
|
+
became
|
181
|
+
|
182
|
+
```
|
183
|
+
cell position: [[0, /Employment/],1], key: 'employment'
|
184
|
+
```
|
185
|
+
|
157
186
|
### [COLLECTION] Validation of a collection (Regular CSV)
|
158
187
|
|
159
188
|
Consider the following CSV:
|
data/UPGRADE.md
CHANGED
data/lib/csv2hash/definition.rb
CHANGED
@@ -38,7 +38,6 @@ module Csv2hash
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def validate!
|
41
|
-
# binding.pry
|
42
41
|
unless TYPES.include?(@type)
|
43
42
|
raise "not suitable type, please use '#{MAPPING}' or '#{COLLECTION}'"
|
44
43
|
end
|
@@ -47,7 +46,6 @@ module Csv2hash
|
|
47
46
|
end
|
48
47
|
|
49
48
|
def default!
|
50
|
-
# binding.pry
|
51
49
|
cells.each do |cell|
|
52
50
|
cell.rules.fetch(:position)
|
53
51
|
|
@@ -66,7 +64,6 @@ module Csv2hash
|
|
66
64
|
cell.rules.merge! allow_blank: false unless cell.rules.has_key? :allow_blank
|
67
65
|
cell.rules.merge! extra_validator: nil unless cell.rules.has_key? :extra_validator
|
68
66
|
end
|
69
|
-
# binding.pry
|
70
67
|
end
|
71
68
|
|
72
69
|
private
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Csv2hash
|
2
|
+
module Discover
|
3
|
+
|
4
|
+
def find_positions!
|
5
|
+
definition.cells.each do |cell|
|
6
|
+
y, x = cell.rules.fetch :position
|
7
|
+
if y.is_a?(Array)
|
8
|
+
column, matcher = y
|
9
|
+
y = data_source.index { |entries| entries[column] =~ matcher }
|
10
|
+
cell.rules[:position] = [y, x]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -1,27 +1,31 @@
|
|
1
|
-
module Csv2hash
|
2
|
-
|
1
|
+
module Csv2hash
|
2
|
+
module Parser
|
3
|
+
module Mapping
|
4
|
+
include Parser
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def fill!
|
7
|
+
self.data = {}.tap do |data_computed|
|
8
|
+
data_computed[:data] ||= []
|
9
|
+
data_computed[:data] << {}.tap do |data_parsed|
|
10
|
+
fill_it data_parsed, data_source
|
11
|
+
end
|
12
|
+
end
|
9
13
|
end
|
10
|
-
end
|
11
|
-
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
def fill_it parsed_data, source_data
|
16
|
+
definition.cells.each do |cell|
|
17
|
+
if cell.rules.fetch :mappable
|
18
|
+
y, x = cell.rules.fetch :position
|
19
|
+
if (nested = cell.rules.fetch :nested)
|
20
|
+
parsed_data[nested] ||= {}
|
21
|
+
parsed_data[nested][cell.rules.fetch(:key)] = source_data[y][x]
|
22
|
+
else
|
23
|
+
parsed_data[cell.rules.fetch(:key)] = source_data[y][x]
|
24
|
+
end
|
25
|
+
end
|
22
26
|
end
|
23
27
|
end
|
28
|
+
|
24
29
|
end
|
25
30
|
end
|
26
|
-
|
27
31
|
end
|
data/lib/csv2hash/validator.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
require_relative 'discover'
|
2
|
+
|
1
3
|
module Csv2hash
|
2
4
|
module Validator
|
5
|
+
include Discover
|
3
6
|
|
4
7
|
def validate_rules y=nil
|
5
|
-
|
8
|
+
find_positions!
|
9
|
+
|
6
10
|
definition.cells.each do |cell|
|
7
11
|
_y, x = position cell.rules.fetch(:position)
|
8
12
|
begin
|
@@ -22,7 +26,7 @@ module Csv2hash
|
|
22
26
|
value = data_source[y][x] rescue nil
|
23
27
|
begin
|
24
28
|
raise unless value unless cell.rules.fetch :allow_blank
|
25
|
-
if (extra_validator = cell.rules.fetch :extra_validator) && extra_validator.kind_of?(
|
29
|
+
if (extra_validator = cell.rules.fetch :extra_validator) && extra_validator.kind_of?(ExtraValidator)
|
26
30
|
raise unless extra_validator.valid? cell.rules, value
|
27
31
|
else
|
28
32
|
if value && (values = cell.rules.fetch :values)
|
data/lib/csv2hash/version.rb
CHANGED
@@ -6,11 +6,26 @@ module Csv2hash
|
|
6
6
|
let(:definition) do
|
7
7
|
Main.generate_definition :foo do
|
8
8
|
set_type { Definition::MAPPING }
|
9
|
-
mapping
|
9
|
+
mapping do
|
10
|
+
cell position: [1,1], key: 'first_name'
|
11
|
+
cell position: [2,1], key: 'last_name'
|
12
|
+
end
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
13
|
-
let(:data_source)
|
16
|
+
let(:data_source) do
|
17
|
+
[ ['Nickname', 'jo', nil, nil, nil],
|
18
|
+
['FirstName', 'John', nil, nil, nil],
|
19
|
+
['LastName', 'Doe', nil, nil, nil],
|
20
|
+
[nil, nil, nil, nil, nil],
|
21
|
+
['Employment', 'CEO', nil, nil, nil],
|
22
|
+
['Post', 'Doe', nil, nil, nil],
|
23
|
+
[nil, nil, nil, nil, nil],
|
24
|
+
[nil, 'Personal info', 'Age', 26, nil],
|
25
|
+
[nil, 'Sex', 'Male', nil, nil],
|
26
|
+
[nil, nil, nil, nil, nil]
|
27
|
+
]
|
28
|
+
end
|
14
29
|
|
15
30
|
subject do
|
16
31
|
Main.new(definition, data_source, ignore_blank_line: false)
|
@@ -21,18 +36,28 @@ module Csv2hash
|
|
21
36
|
it {
|
22
37
|
expect(subject.tap do |csv2hash|
|
23
38
|
csv2hash.parse!
|
24
|
-
end.data).to eql({ data: [
|
39
|
+
end.data).to eql({ data: [{ 'first_name' => 'John', 'last_name' => 'Doe' }]})
|
25
40
|
}
|
26
41
|
end
|
27
42
|
|
28
43
|
context 'with nested' do
|
29
|
-
let(:data_source) { [ [ 'John Doe', 22 ] ] }
|
30
44
|
before do
|
31
|
-
definition.cells << Cell.new({ position: [
|
45
|
+
definition.cells << Cell.new({ position: [7,3], key: 'age', nested: 'infos' })
|
46
|
+
end
|
47
|
+
it {
|
48
|
+
expect(subject.tap { |c| c.parse! }.data).to eql(
|
49
|
+
{ data: [ { 'first_name' => 'John', 'last_name' => 'Doe', 'infos' => { 'age' => 26 } } ] }
|
50
|
+
)
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'discover' do
|
55
|
+
before do
|
56
|
+
definition.cells << Cell.new({ position: [[1,/Sex/],2], key: 'sex'})
|
32
57
|
end
|
33
58
|
it {
|
34
59
|
expect(subject.tap { |c| c.parse! }.data).to eql(
|
35
|
-
{ data: [ { '
|
60
|
+
{ data: [ { 'first_name' => 'John', 'last_name' => 'Doe', 'sex' => 'Male' } ] }
|
36
61
|
)
|
37
62
|
}
|
38
63
|
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.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel AZEMAR
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/csv2hash/csv_array.rb
|
113
113
|
- lib/csv2hash/data_wrapper.rb
|
114
114
|
- lib/csv2hash/definition.rb
|
115
|
+
- lib/csv2hash/discover.rb
|
115
116
|
- lib/csv2hash/expectation.rb
|
116
117
|
- lib/csv2hash/extra_validator.rb
|
117
118
|
- lib/csv2hash/notifier.rb
|