csv2hash 0.7.1 → 0.7.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: 78efc416ea706262ed45cd1732953a91ab59c3ac
4
- data.tar.gz: 045440b7c850178eedeb7e0d54c8797a374a7aea
3
+ metadata.gz: 1d373a399e6b36122e3188d8ba7078b8f3c7de1f
4
+ data.tar.gz: 537b8221207eacd8c6f8294b7e192a52a9bc85ee
5
5
  SHA512:
6
- metadata.gz: 9db537dbfddb6ef012bd23acdfb2faedb15c20b4471cb66457d0156d15773991a828c875f5d17f0232fe79e178b009e9eba7f48f879dd8497a6881a0c3c8d507
7
- data.tar.gz: 33a571b8d8e0217abf628c73c83c6b6264705d0407b5bd0d217410c836a711f330af66f256321141ef6a5007355e4263fdf139d7c42b13b8a37107464eacf36f
6
+ metadata.gz: ab5e7774d96a99805fec0357004a63970c4162f7773a0f5d31e5334ad0b43a2a96e8559ebc0a433368640ca6ae8f7072af429254f9ff396f926056534a52fd9f
7
+ data.tar.gz: d86e84ed44b3d9ef11750a33fdff532fdf2ae4a98f44ba3eae4e9b722e31d928d3bd4830d4fcdd961342a2cc03d24cadd70cbc18fb0dd3b75fdceda99faf40f4
data/.travis.yml CHANGED
@@ -1,4 +1,9 @@
1
1
  language: ruby
2
+ script: bundle exec rspec
2
3
  rvm:
3
- - 2.1.2
4
- script: bundle exec rspec spec
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - ruby-head
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: ruby-head
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ### VERSION 0.7.2
2
+
3
+ * bug fix
4
+ Fix bug and add case sensitive values
5
+ * [fullchanges](https://github.com/FinalCAD/csv2hash/pull/22)
6
+ * [Author Ale Paredes](https://github.com/ale7714)
7
+
8
+ * enhancements
9
+ allow multiple versions of ruby
10
+ * [fullchanges](https://github.com/FinalCAD/csv2hash/pull/23)
11
+ * [Author Steve Chung](https://github.com/s12chung)
12
+
1
13
  ### VERSION 0.7.1
2
14
 
3
15
  * enhancements
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv2hash (0.7.1)
4
+ csv2hash (0.7.2)
5
5
  activesupport (~> 4.1)
6
6
 
7
7
  GEM
@@ -102,7 +102,7 @@ PLATFORMS
102
102
  ruby
103
103
 
104
104
  DEPENDENCIES
105
- bundler (~> 1.6)
105
+ bundler (> 1.3)
106
106
  coveralls
107
107
  csv2hash!
108
108
  generator_spec
data/README.md CHANGED
@@ -119,6 +119,12 @@ cell name: 'aswering', type: 'string', values: ['yes', 'no'], position: [0,0]
119
119
  cell name: 'aswering', values: ['yes', 'no'], position: [0,0]
120
120
  ```
121
121
 
122
+ By default values are case sensitive but if you want the value to be a string that can be either 'yes' or 'no' case insentive ('Yes', 'YES', 'No', 'NO') you can define the following validation rule:
123
+
124
+ ```
125
+ cell name: 'aswering', values: ['yes', 'no'], case_sensitive_values: false, position: [0,0]
126
+ ```
127
+
122
128
  You can define you own message but default message is 'undefined :key on :position'
123
129
 
124
130
  ```
@@ -155,6 +161,7 @@ All remaining keys are optionals:
155
161
  * mappable: true
156
162
  * type: 'string'
157
163
  * values: nil
164
+ * case_sensitive_values: true
158
165
  * nested: nil
159
166
  * allow_blank: false
160
167
  * extra_validator: nil
data/UPGRADE.md CHANGED
@@ -1,41 +1,5 @@
1
1
  # Upgrading
2
2
 
3
- # Upgrading from 0.7.0 to 0.7.1
4
-
5
- nothing
6
-
7
- # Upgrading from 0.6.8 to 0.7.0
8
-
9
- nothing
10
-
11
- # Upgrading from 0.6.7 to 0.6.8
12
-
13
- nothing
14
-
15
- # Upgrading from 0.6.6 to 0.6.7
16
-
17
- nothing
18
-
19
- # Upgrading from 0.6.5 to 0.6.6
20
-
21
- nothing
22
-
23
- # Upgrading from 0.6.4 to 0.6.5
24
-
25
- nothing
26
-
27
- # Upgrading from 0.6.3 to 0.6.4
28
-
29
- nothing
30
-
31
- # Upgrading from 0.6.2 to 0.6.3
32
-
33
- nothing
34
-
35
- # Upgrading from 0.6.1 to 0.6.2
36
-
37
- nothing
38
-
39
3
  # Upgrading from 0.6 to 0.6.1
40
4
 
41
5
  Change Structure validation rules, MinColumn, MaxColumn are replaced by :min_columns, :max_columns
data/csv2hash.gemspec CHANGED
@@ -19,10 +19,8 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency 'activesupport', '~> 4.1'
21
21
 
22
- spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'bundler', '> 1.3'
23
23
  spec.add_development_dependency 'rake', '~> 10.3'
24
24
  spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  spec.add_development_dependency 'its', '~> 0.2'
26
-
27
- spec.required_ruby_version = '~> 2.1'
28
26
  end
@@ -48,21 +48,15 @@ module Csv2hash
48
48
  def default!
49
49
  cells.each do |cell|
50
50
  cell.rules.fetch(:position)
51
-
52
51
  default_position cell
53
- unless cell.rules.has_key? :message
54
- if cell.rules.has_key? :values
55
- cell.rules.merge! message: 'value :found not supported for :key, please use one of :values'
56
- else
57
- cell.rules.merge! message: 'undefined :key on :position'
58
- end
59
- end
60
- cell.rules.merge! mappable: true unless cell.rules.has_key? :mappable
61
- cell.rules.merge! type: 'string' unless cell.rules.has_key? :type
62
- cell.rules.merge! values: nil unless cell.rules.has_key? :values
63
- cell.rules.merge! nested: nil unless cell.rules.has_key? :nested
64
- cell.rules.merge! allow_blank: false unless cell.rules.has_key? :allow_blank
65
- cell.rules.merge! extra_validator: nil unless cell.rules.has_key? :extra_validator
52
+ default_message cell
53
+ cell.rules.merge! mappable: true unless cell.rules.has_key? :mappable
54
+ cell.rules.merge! type: 'string' unless cell.rules.has_key? :type
55
+ cell.rules.merge! values: nil unless cell.rules.has_key? :values
56
+ cell.rules.merge! case_sensitive_values: true unless cell.rules.has_key? :case_sensitive_values
57
+ cell.rules.merge! nested: nil unless cell.rules.has_key? :nested
58
+ cell.rules.merge! allow_blank: false unless cell.rules.has_key? :allow_blank
59
+ cell.rules.merge! extra_validator: nil unless cell.rules.has_key? :extra_validator
66
60
  end
67
61
  end
68
62
 
@@ -79,5 +73,14 @@ module Csv2hash
79
73
  end
80
74
  end
81
75
 
76
+ def default_message cell
77
+ unless cell.rules.has_key? :message
78
+ if cell.rules.has_key? :values
79
+ cell.rules.merge! message: 'value :found not supported for :key, please use one of :values'
80
+ else
81
+ cell.rules.merge! message: 'undefined :key on :position'
82
+ end
83
+ end
84
+ end
82
85
  end
83
86
  end
@@ -28,9 +28,8 @@ module Csv2hash
28
28
  if extra_validator?(cell)
29
29
  verify_extra_validator! cell, value
30
30
  else
31
- if rang? cell, value
32
- values = cell.rules.fetch(:values)
33
- verify_rang! values, value
31
+ if has_valid_values? cell, value
32
+ verify_valid_values! cell, value
34
33
  end
35
34
  end
36
35
  rescue => e
@@ -59,21 +58,27 @@ module Csv2hash
59
58
  end
60
59
 
61
60
  def verify_blank! cell, value
62
- raise unless value unless cell.rules.fetch :allow_blank
61
+ raise unless value.present? || cell.rules.fetch(:allow_blank)
63
62
  end
64
63
 
65
- def rang? cell, value
66
- value && (values = cell.rules.fetch(:values))
64
+ def has_valid_values? cell, value
65
+ value.present? && cell.rules.fetch(:values)
67
66
  end
68
67
 
69
- def verify_rang! values, value
68
+ def verify_valid_values! cell, value
69
+ values = cell.rules.fetch(:values)
70
70
  if values.class == Range
71
71
  raise unless values.include?(value.to_f)
72
72
  else
73
- raise unless values.include?(value)
73
+ case_sensitive_values = cell.rules.fetch(:case_sensitive_values)
74
+ raise unless valid_values_include? values, value, case_sensitive_values
74
75
  end
75
76
  end
76
77
 
78
+ def valid_values_include? values, value, case_sensitive
79
+ case_sensitive ? values.include?(value) : values.any?{ |v| v.casecmp(value)==0 }
80
+ end
81
+
77
82
  def find_or_remove_dynamic_fields_on_mapping!
78
83
  cells = definition.cells.dup
79
84
  # cells without optional and not found dynamic field
@@ -1,3 +1,3 @@
1
1
  module Csv2hash
2
- VERSION = '0.7.1'
2
+ VERSION = '0.7.2'
3
3
  end
@@ -64,6 +64,7 @@ module Csv2hash
64
64
  mappable: true,
65
65
  type: 'string',
66
66
  values: nil,
67
+ case_sensitive_values: true,
67
68
  nested: nil,
68
69
  allow_blank: false,
69
70
  extra_validator: nil })
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  module Csv2hash
4
4
  describe 'Validator' do
5
5
 
6
+ subject do
7
+ Class.new do
8
+ include Validator
9
+ end.new
10
+ end
11
+
6
12
  describe '#message' do
7
13
  let(:cell) { double(:cell, rules: rules) }
8
14
 
9
- subject do
10
- Class.new do
11
- include Validator
12
- end.new
13
- end
14
-
15
15
  context 'string value' do
16
16
  let(:rules) {{ foo: 'bar', message: ':foo are value of foo key', key: 'bar' }}
17
17
 
@@ -37,5 +37,69 @@ module Csv2hash
37
37
  end
38
38
  end
39
39
 
40
+ describe '#validate_cell' do
41
+ let(:x) { 0 }
42
+ let(:y) { 0 }
43
+ let(:allow_blank) { false }
44
+ let(:values) { nil }
45
+ let(:case_sensitive_values) { false }
46
+ let(:data_source) { [ ['John Doe'] ]}
47
+ let(:rules) { { position: [x, y],
48
+ key: 'name',
49
+ message: 'undefined :key on :position',
50
+ mappable: true,
51
+ type: 'string',
52
+ values: values,
53
+ case_sensitive_values: case_sensitive_values,
54
+ nested: nil,
55
+ allow_blank: allow_blank,
56
+ extra_validator: nil }}
57
+ let(:cell) { Cell.new(rules) }
58
+
59
+ before do
60
+ allow(subject).to receive(:break_on_failure) { true }
61
+ allow(subject).to receive(:data_source) { data_source }
62
+ end
63
+
64
+ context 'when default cell' do
65
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to_not raise_error }
66
+ end
67
+
68
+ context 'when position in data source is blank' do
69
+ let(:data_source) { [ [''] ]}
70
+
71
+ context 'and allow_blank is false' do
72
+ let(:allow_blank) { false }
73
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to raise_error 'undefined :name on [0, 0]' }
74
+ end
75
+
76
+ context 'and allow_blank is true' do
77
+ let(:allow_blank) { true }
78
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to_not raise_error }
79
+
80
+ context 'and values are not nil' do
81
+ let(:values) { ['yes', 'no'] }
82
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to_not raise_error }
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'when values are not nil' do
88
+ let(:values) { ['john doe', 'jane doe'] }
89
+ let(:data_source) { [ ['John Doe'] ]}
90
+
91
+ context 'and case_sensitive_values is true' do
92
+ let(:case_sensitive_values) { true }
93
+
94
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to raise_error }
95
+ end
96
+
97
+ context 'and case_sensitive_values is false' do
98
+ let(:case_sensitive_values) { false }
99
+
100
+ it{ expect{ subject.send(:validate_cell, x, y, cell) }.to_not raise_error }
101
+ end
102
+ end
103
+ end
40
104
  end
41
105
  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.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel AZEMAR
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.6'
33
+ version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.6'
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -172,9 +172,9 @@ require_paths:
172
172
  - lib
173
173
  required_ruby_version: !ruby/object:Gem::Requirement
174
174
  requirements:
175
- - - "~>"
175
+ - - ">="
176
176
  - !ruby/object:Gem::Version
177
- version: '2.1'
177
+ version: '0'
178
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  requirements:
180
180
  - - ">="