portuguese_validators 0.1.1 → 0.1.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: dda9a0ba1218650749627b149ac72a33a2db99a6
4
- data.tar.gz: cddc577ffa4606e7298f2b770082559ca77ca8c6
3
+ metadata.gz: ed26e48bd865b704a3d69135c056e5a7f38665f6
4
+ data.tar.gz: 04050615af5d667610996670df921f42ba070cac
5
5
  SHA512:
6
- metadata.gz: 3741c81cce9cba804019218667329a609694220e37ef51731975eb01a8e2bbe5c4da775acbd225444c8e624803410c0d3f914ad1f77f007b55fed9452555d3f4
7
- data.tar.gz: 40da33c44934c9a0cf6c8eaaa218d86c6ab69c04ec12f2a7e21e53eeac5abf808071ce965f6b5710925210ca08f1b77fccdd9d31f5e461911c7f328765a6ad74
6
+ metadata.gz: 196737f9a4080e6fa85775141f5b4114c0f9a7820390d665c1a736bd55b7114f9bb856cf488fb1fc71aa8aa4b0bf78de899aad8c9b3d2c5ac044f5ada1ae5af1
7
+ data.tar.gz: 2c66bf4476f365d63ed05279b87bb226a6727b5dcc734b5ea7a908a416eb658df97c6efefb060f8d31a39677401db8c1249782a18a3f00dce40125ba45537a5d
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
1
  --colour
2
- --format s -c
2
+ --format documentation
@@ -8,9 +8,8 @@ module PortugueseValidators
8
8
  BLACKLIST = %w(000000000)
9
9
 
10
10
  def validate_each(record, attribute, value) # :nodoc:
11
- unless is_valid?(value)
12
- record.errors[attribute] << (options[:message] || 'is not a valid BI')
13
- end
11
+ return if value.blank?
12
+ record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(value)
14
13
  end
15
14
 
16
15
  # Returns true if the number is a valid BI or false otherwise.
@@ -41,4 +40,4 @@ module PortugueseValidators
41
40
  number.match(/^\d{9}$/) ? true : false
42
41
  end
43
42
  end
44
- end
43
+ end
@@ -4,9 +4,8 @@ module PortugueseValidators
4
4
  # The number is always composed by 21 where the last two form the control number.
5
5
  class PortugueseNibValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, value)
7
- unless is_valid?(value)
8
- record.errors[attribute] << (options[:message] || 'is not a valid NIB')
9
- end
7
+ return if value.blank?
8
+ record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(value)
10
9
  end
11
10
 
12
11
  def is_valid?(number)
@@ -36,4 +35,4 @@ module PortugueseValidators
36
35
  number.match(/^\d{21}$/) ? true : false
37
36
  end
38
37
  end
39
- end
38
+ end
@@ -5,9 +5,8 @@ module PortugueseValidators
5
5
  # last digit is the control digit.
6
6
  class PortugueseNifValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
- unless is_valid?(value)
9
- record.errors[attribute] << (options[:message] || 'is not a valid NIF')
10
- end
8
+ return if value.blank?
9
+ record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(value)
11
10
  end
12
11
 
13
12
  def is_valid?(number)
@@ -38,4 +37,4 @@ module PortugueseValidators
38
37
  number.match(/^\d{9}$/) && number.match(/^[1256789]/) ? true : false
39
38
  end
40
39
  end
41
- end
40
+ end
@@ -13,8 +13,8 @@ module PortugueseValidators
13
13
  def validate_each(record, attribute, value)
14
14
  return if value.blank?
15
15
 
16
- number = value.gsub(" ", "")
17
- record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(number)
16
+ phone_number = value.to_s.gsub(" ", "")
17
+ record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(phone_number)
18
18
  end
19
19
 
20
20
  def is_valid?(number)
@@ -33,5 +33,4 @@ module PortugueseValidators
33
33
  number.match(/^((00|\+)(\d{3}))?\d{9}$/) ? true : false
34
34
  end
35
35
  end
36
-
37
36
  end
@@ -1,3 +1,3 @@
1
1
  module PortugueseValidators
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -10,12 +10,12 @@ describe PortugueseBiValidator do
10
10
  after(:all) { Object.send(:remove_const, :User) }
11
11
 
12
12
  describe 'validation' do
13
- it 'returns false if the number is empty' do
14
- expect(User.new(bi: '')).to be_invalid
13
+ it 'returns true if the number is empty' do
14
+ expect(User.new(bi: '')).to be_valid
15
15
  end
16
16
 
17
- it 'returns false if the number is not defined' do
18
- expect(User.new(bi: nil)).to be_invalid
17
+ it 'returns true if the number is not defined' do
18
+ expect(User.new(bi: nil)).to be_valid
19
19
  end
20
20
 
21
21
  context 'given valid BI numbers' do
@@ -10,12 +10,12 @@ describe PortugueseNibValidator do
10
10
  after(:all) { Object.send(:remove_const, :User) }
11
11
 
12
12
  describe 'validation' do
13
- it 'returns false if the number is empty' do
14
- expect(User.new(nib: '')).to be_invalid
13
+ it 'returns true if the number is empty' do
14
+ expect(User.new(nib: '')).to be_valid
15
15
  end
16
16
 
17
- it 'returns false if the number is not defined' do
18
- expect(User.new(nib: nil)).to be_invalid
17
+ it 'returns true if the number is not defined' do
18
+ expect(User.new(nib: nil)).to be_valid
19
19
  end
20
20
 
21
21
  context 'given invalid NIB numbers' do
@@ -10,12 +10,12 @@ describe PortugueseNifValidator do
10
10
  after(:all) { Object.send(:remove_const, :User) }
11
11
 
12
12
  describe 'validation' do
13
- it 'returns false if the number is empty' do
14
- expect(User.new(nif: '')).to be_invalid
13
+ it 'returns true if the number is empty' do
14
+ expect(User.new(nif: '')).to be_valid
15
15
  end
16
16
 
17
- it 'returns false if the number is not defined' do
18
- expect(User.new(nif: nil)).to be_invalid
17
+ it 'returns true if the number is not defined' do
18
+ expect(User.new(nif: nil)).to be_valid
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe PortuguesePhoneValidator do
4
+ before(:all) {
5
+ class User < TestModel
6
+ validates :phone_number, portuguese_phone: true
7
+ end
8
+ }
9
+
10
+ after(:all) { Object.send(:remove_const, :User) }
11
+
12
+ describe 'validation' do
13
+ it 'returns true if the number is empty' do
14
+ expect(User.new(phone_number: '')).to be_valid
15
+ end
16
+
17
+ it 'returns true if the number is not defined' do
18
+ expect(User.new(phone_number: nil)).to be_valid
19
+ end
20
+
21
+ context 'given valid phone numbers' do
22
+ %w(912345678 212345678 00351923456789 +351236123456).each do |phone_number|
23
+ it "returns true for `#{phone_number}'" do
24
+ expect(User.new(phone_number: phone_number)).to be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'given invalid phone numbers' do
30
+ %w(91234567 9123456789 0035192345678 +3519234567890 123456789 00351123456789 qwertyuio
31
+ -12345678).each do |phone_number|
32
+ it "returns false for `#{phone_number}'" do
33
+ expect(User.new(phone_number: phone_number)).to be_invalid
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: portuguese_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Otero
@@ -120,6 +120,7 @@ files:
120
120
  - spec/bi_validator_spec.rb
121
121
  - spec/nib_validator_spec.rb
122
122
  - spec/nif_validator_spec.rb
123
+ - spec/phone_validator_spec.rb
123
124
  - spec/spec_helper.rb
124
125
  homepage: http://github.com/rikas/portuguese_validators/fork
125
126
  licenses:
@@ -149,4 +150,5 @@ test_files:
149
150
  - spec/bi_validator_spec.rb
150
151
  - spec/nib_validator_spec.rb
151
152
  - spec/nif_validator_spec.rb
153
+ - spec/phone_validator_spec.rb
152
154
  - spec/spec_helper.rb