missing_validators 0.8.3 → 0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8965924e44993f7bfad6dcfd4a63917f544e417
4
- data.tar.gz: 3a114c7b78f19d3384012dcefc5d7656514a5613
3
+ metadata.gz: 7fcaba1ca021815135f1424a4ba05e50bb43bcb8
4
+ data.tar.gz: e4c2defc413858dd7de4f324e5598f338c2289df
5
5
  SHA512:
6
- metadata.gz: 3a6b1635cb5c6980b98cca25389c6f1b1a903197684d20ee4adb1de3e50674c50749d3dab2619cc50d3b31e05e353e44ee3f8fe3fa2dd262617a8e69b0edfaf3
7
- data.tar.gz: 455faa6aa3d3a22b9ed992dc6d2a6bf4e9e67aff8391eca21e3ea86da435d9d0e14777cffe1841ddada8f1112557d5cf4edb999a002340148e270ec3145799d8
6
+ metadata.gz: 91854dee85dce2b8e089afe19dcb22a7c9a0e5a54d61ed09d6e457245a16e721f459d1f5e89943149672679878ba65795aa790de3f5e908e2e27bc05496b0e91
7
+ data.tar.gz: 81e974d943116e0561728a31cb9eff0b88f50961bf59219cf5b94b719f2c2420150782e4c94e2466f20f081ef68c6271a2cc0c8ae51a0172d1566d20ae335a98
@@ -1,11 +1,12 @@
1
1
  en:
2
2
  errors:
3
3
  messages:
4
- inequality: "can't be equal to %{attr}"
5
- email: "isn't a valid email address"
6
- url: "isn't a valid URL"
7
- mac_address: "isn't a valid MAC address"
8
- longitude: "isn't a valid longitude"
4
+ imei: "is not a valid IMEI"
5
+ inequality: "cannot be equal to %{attr}"
6
+ email: "is not a valid email address"
7
+ url: "is not a valid URL"
8
+ mac_address: "is not a valid MAC address"
9
+ longitude: "is not a valid longitude"
9
10
  missing_validators:
10
11
  matchers:
11
12
  ensure_inequality_of:
@@ -11,3 +11,5 @@ require 'missing_validators/validators/mac_address_validator'
11
11
  require 'missing_validators/matchers/ensure_valid_mac_address_format_of' if defined?(RSpec)
12
12
  require 'missing_validators/validators/longitude_validator'
13
13
  require 'missing_validators/validators/latitude_validator'
14
+ require 'missing_validators/validators/imei_validator'
15
+ require 'missing_validators/matchers/ensure_valid_imei_format_of' if defined?(RSpec)
@@ -0,0 +1,20 @@
1
+ RSpec::Matchers.define :ensure_valid_imei_format_of do |attribute|
2
+ match do |model|
3
+ model.send("#{attribute}=", "invalid.imei")
4
+ model.valid?
5
+
6
+ if model.errors.has_key?(attribute)
7
+ model.errors[attribute].include?(I18n.t('errors.messages.imei'))
8
+ end
9
+ end
10
+
11
+ failure_message_for_should do |model|
12
+ I18n.t 'missing_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should',
13
+ model: model.class
14
+ end
15
+
16
+ failure_message_for_should_not do |model|
17
+ I18n.t 'missing_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should_not',
18
+ model: model.class
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ # Allows to check if the value of a specific attribute is a valid IMEI address.
2
+ #
3
+ # @example Validate that the device IMEI address is valid.
4
+ # class Device << ActiveRecord::Base
5
+ # attr_accessor :imei
6
+ # validates :imei, imei: true
7
+ # end
8
+ class ImeiValidator < ActiveModel::EachValidator
9
+ # Checks if an attribute value is a valid IMEI address.
10
+ #
11
+ # @param [Object] record object to validate
12
+ # @param [String] attribute name of the object attribute to validate
13
+ # @param [Object] value attribute value
14
+ def validate_each(record, attribute, value)
15
+ allow_blank = options[:allow_blank] || false
16
+ return if allow_blank && value.blank?
17
+
18
+ unless valid?(value.to_s, options)
19
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.imei'))
20
+ end
21
+ end
22
+
23
+ def self.validate_format(imei_number)
24
+ !!(imei_number =~ /\A[\d\.\:\-\s]+\z/i) # 356843052637512 or 35-6843052-637512 or 35.6843052.637512
25
+ end
26
+
27
+ def self.luhn_valid?(input)
28
+ numbers = input.gsub(/\D/, '').reverse
29
+
30
+ sum, i = 0, 0
31
+
32
+ numbers.each_char do |ch|
33
+ n = ch.to_i
34
+ n *= 2 if i.odd?
35
+ n = 1 + (n - 10) if n >= 10
36
+
37
+ sum += n
38
+ i += 1
39
+ end
40
+
41
+ (sum % 10).zero?
42
+ end
43
+
44
+ private
45
+
46
+ def valid?(imei, options)
47
+ self.class.validate_format(imei) \
48
+ && self.class.luhn_valid?(imei)
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  # Provides a collection of custom validators that are often required in Rails applications.
2
2
  module MissingValidators
3
3
  # Gem version.
4
- VERSION = "0.8.3"
4
+ VERSION = "0.9"
5
5
  end
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Andrew Gridnev"]
6
6
  gem.email = ["andrew.gridnev@gmail.com"]
7
7
  gem.summary = %q{Adds some handy validators.}
8
- gem.description = %q{Validates email addresses, URLs, MAC addresses and inequality of attributes.}
8
+ gem.description = %q{Validates email addresses, URLs, IMEI, MAC addresses and inequality of attributes.}
9
9
  gem.homepage = "https://github.com/andrewgr/missing_validators/"
10
10
  gem.license = 'MIT'
11
11
 
@@ -16,9 +16,9 @@ Gem::Specification.new do |gem|
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = MissingValidators::VERSION
18
18
 
19
- gem.add_development_dependency 'rspec'
20
- gem.add_development_dependency 'shoulda-matchers'
21
-
22
19
  gem.add_dependency 'activemodel', '> 3.0.0'
23
20
  gem.add_dependency 'activesupport', '> 3.0.0'
21
+
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'shoulda-matchers'
24
24
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  require 'missing_validators'
2
+ require 'shoulda/matchers/active_record'
2
3
  require 'shoulda-matchers'
3
4
 
4
5
  I18n.load_path << File.expand_path("../../config/locales/en.yml", __FILE__)
5
-
6
- RSpec.configure do |config|
7
- config.mock_with :rspec
8
- end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImeiValidator do
4
+ context "IMEI has valid format" do
5
+ let(:klass) do
6
+ Class.new do
7
+ include ActiveModel::Validations
8
+ attr_accessor :imei, :name
9
+ validates :imei, imei: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ it { should ensure_valid_imei_format_of(:imei) }
16
+ it { should_not ensure_valid_imei_format_of(:name) }
17
+
18
+ it { should allow_value(356843052637512).for(:imei) }
19
+ it { should allow_value("356843052637512").for(:imei) }
20
+ it { should allow_value("35-684305-2637512").for(:imei) }
21
+ it { should allow_value("35-684305.263.7512").for(:imei) }
22
+
23
+ context "value too short" do
24
+ it { should_not allow_value("3568430537512").for(:imei) }
25
+ it { should_not allow_value("3").for(:imei) }
26
+ end
27
+
28
+ it "can't be too long" do
29
+ should_not allow_value("35684305263751233").for(:imei)
30
+ end
31
+
32
+ context "checksum doesn't match" do
33
+ it { should_not allow_value("356843052637513").for(:imei) }
34
+ it { should_not allow_value("156843052637512").for(:imei) }
35
+ end
36
+
37
+ it { should_not allow_value("invalid").for(:imei) }
38
+ end
39
+ end
metadata CHANGED
@@ -1,72 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: missing_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: '0.9'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Gridnev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-22 00:00:00.000000000 Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - '>'
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
19
+ version: 3.0.0
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - '>'
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 3.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: shoulda-matchers
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - '>'
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
33
+ version: 3.0.0
34
+ type: :runtime
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: '0'
40
+ version: 3.0.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: activemodel
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>'
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
48
- type: :runtime
47
+ version: '0'
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>'
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: activesupport
56
+ name: shoulda-matchers
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>'
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 3.0.0
62
- type: :runtime
61
+ version: '0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>'
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
- version: 3.0.0
69
- description: Validates email addresses, URLs, MAC addresses and inequality of attributes.
68
+ version: '0'
69
+ description: Validates email addresses, URLs, IMEI, MAC addresses and inequality of
70
+ attributes.
70
71
  email:
71
72
  - andrew.gridnev@gmail.com
72
73
  executables: []
@@ -84,9 +85,11 @@ files:
84
85
  - lib/missing_validators.rb
85
86
  - lib/missing_validators/matchers/ensure_inequality_of_matcher.rb
86
87
  - lib/missing_validators/matchers/ensure_valid_email_format_of.rb
88
+ - lib/missing_validators/matchers/ensure_valid_imei_format_of.rb
87
89
  - lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb
88
90
  - lib/missing_validators/matchers/ensure_valid_url_format_of.rb
89
91
  - lib/missing_validators/validators/email_validator.rb
92
+ - lib/missing_validators/validators/imei_validator.rb
90
93
  - lib/missing_validators/validators/inequality_validator.rb
91
94
  - lib/missing_validators/validators/latitude_validator.rb
92
95
  - lib/missing_validators/validators/longitude_validator.rb
@@ -96,6 +99,7 @@ files:
96
99
  - missing_validators.gemspec
97
100
  - spec/spec_helper.rb
98
101
  - spec/validators/email_validator_spec.rb
102
+ - spec/validators/imei_spec.rb
99
103
  - spec/validators/inequality_validator_spec.rb
100
104
  - spec/validators/latitude_validator_spec.rb
101
105
  - spec/validators/longitude_validator_spec.rb
@@ -128,6 +132,7 @@ summary: Adds some handy validators.
128
132
  test_files:
129
133
  - spec/spec_helper.rb
130
134
  - spec/validators/email_validator_spec.rb
135
+ - spec/validators/imei_spec.rb
131
136
  - spec/validators/inequality_validator_spec.rb
132
137
  - spec/validators/latitude_validator_spec.rb
133
138
  - spec/validators/longitude_validator_spec.rb