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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4017d9fec070fb90dc8c400f67f3c78b9fc6f536
4
- data.tar.gz: 36ec66a10def761fff7f091aaf61160a8ad4b852
3
+ metadata.gz: 0cff06b049e0b90d0627bced7665e5212667a128
4
+ data.tar.gz: 6aaf21e5a862915914813ee0d65495b04f041fc1
5
5
  SHA512:
6
- metadata.gz: 9e77b7043aff1425609cd7844ade388db5dda784a7d6fa1da3ea90d773af69327ea94e9efecb2e796e63e995c850c22e59228fda689a6ba694133a66decd1fb1
7
- data.tar.gz: a54b994cc833b8381d38a4623b96b8a2f630ccd1228990320cf6645e401162ad6fcbe45fed6906c3eac79daeda149e4fc391192f1b19e9168e5f33aa178b508e
6
+ metadata.gz: 1951755e2eda195942d6abaacaff5b2bf859a3e46e5f36160640dc3035ff1f9e81e903f991fb81e8b8cc29b3d4046766183b8f983fe244e774b05e1381c8d934
7
+ data.tar.gz: f7ab7b90d43cbec0a3379ec8461b9a41f45c3152d90c3e8106bd01275a53d98ed81b68cdf0bd3581ada17e97d4c75febf98491aebeb23fbaf5036ad766023772
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### VERSION 0.6.2
2
+
3
+ * feature
4
+ * Auto discover, you can add floating position on mapping rules
5
+
1
6
  ### VERSION 0.6.1
2
7
 
3
8
  * deprecations
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv2hash (0.6.1)
4
+ csv2hash (0.6.2)
5
5
  activesupport (~> 4.1)
6
6
 
7
7
  GEM
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
@@ -1,5 +1,13 @@
1
1
  # Upgrading
2
2
 
3
+ # Upgrading from 0.6.1 to 0.6.2
4
+
5
+ nothing
6
+
7
+ # Upgrading from 0.6 to 0.6.1
8
+
9
+ Change Structure validation rules, MinColumn, MaxColumn are replaced by :min_columns, :max_columns
10
+
3
11
  # Upgrading from 0.5 to 0.6
4
12
 
5
13
  Introduce DSL
@@ -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::Parser::Mapping
2
- include Csv2hash::Parser
1
+ module Csv2hash
2
+ module Parser
3
+ module Mapping
4
+ include Parser
3
5
 
4
- def fill!
5
- self.data = {}.tap do |data_computed|
6
- data_computed[:data] ||= []
7
- data_computed[:data] << {}.tap do |data_parsed|
8
- fill_it data_parsed, data_source
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
- def fill_it parsed_data, source_data
14
- definition.cells.each do |cell|
15
- if cell.rules.fetch :mappable
16
- y, x = cell.rules.fetch :position
17
- if (nested = cell.rules.fetch :nested)
18
- parsed_data[nested] ||= {}
19
- parsed_data[nested][cell.rules.fetch(:key)] = source_data[y][x]
20
- else
21
- parsed_data[cell.rules.fetch(:key)] = source_data[y][x]
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
@@ -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
- # binding.pry
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?(Csv2hash::ExtraValidator)
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)
@@ -1,3 +1,3 @@
1
1
  module Csv2hash
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
@@ -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 { cell position: [0,0], key: 'name' }
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) { [ [ 'John Doe' ] ] }
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: [ { 'name' => 'John Doe' } ] })
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: [0,1], key: 'age', nested: 'infos' })
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: [ { 'name' => 'John Doe', 'infos' => { 'age' => 22 } } ] }
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.1
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