active_validation 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/active_validation.gemspec +1 -1
  3. data/lib/active_validation/validators/alpha_numeric_validator.rb +2 -2
  4. data/lib/active_validation/validators/alpha_validator.rb +2 -2
  5. data/lib/active_validation/validators/base64_validator.rb +1 -1
  6. data/lib/active_validation/validators/boolean_validator.rb +3 -3
  7. data/lib/active_validation/validators/coordinate_validator.rb +5 -5
  8. data/lib/active_validation/validators/credit_card_validator.rb +23 -58
  9. data/lib/active_validation/validators/currency_validator.rb +1 -1
  10. data/lib/active_validation/validators/cusip_validator.rb +3 -3
  11. data/lib/active_validation/validators/email_validator.rb +3 -3
  12. data/lib/active_validation/validators/equality_validator.rb +3 -3
  13. data/lib/active_validation/validators/hex_validator.rb +1 -1
  14. data/lib/active_validation/validators/imei_validator.rb +4 -4
  15. data/lib/active_validation/validators/ip_validator.rb +1 -1
  16. data/lib/active_validation/validators/isbn_validator.rb +5 -5
  17. data/lib/active_validation/validators/isin_validator.rb +2 -2
  18. data/lib/active_validation/validators/mac_address_validator.rb +4 -7
  19. data/lib/active_validation/validators/name_validator.rb +1 -1
  20. data/lib/active_validation/validators/password_validator.rb +1 -1
  21. data/lib/active_validation/validators/phone_validator.rb +1 -1
  22. data/lib/active_validation/validators/sedol_validator.rb +4 -6
  23. data/lib/active_validation/validators/slug_validator.rb +1 -1
  24. data/lib/active_validation/validators/ssn_validator.rb +1 -1
  25. data/lib/active_validation/validators/tracking_number_validator.rb +11 -11
  26. data/lib/active_validation/validators/url_validator.rb +8 -8
  27. data/lib/active_validation/validators/username_validator.rb +1 -1
  28. data/lib/active_validation/validators/uuid_validator.rb +2 -2
  29. data/lib/active_validation/version.rb +1 -1
  30. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b3c37a0ea5dafc85f13adb8ef2aedf5e7aa9056
4
- data.tar.gz: 872e9ca16eb9787e673deb3df8f4ae4e467d5c17
3
+ metadata.gz: ad382cdab288f4c64ab7197a45cd470d31f6c7d6
4
+ data.tar.gz: d34760c5275f365fff05f108eacbdb6d6ec3b89a
5
5
  SHA512:
6
- metadata.gz: 5b58170b66edd9b572ec7a79b1af3eabb303f5daa93f778b3ff07e274f3269fabbd51e18dacebf4d90c23bbff50e025beedbbb7d44a8262853802d4e159c1516
7
- data.tar.gz: cd2b3b55ffe6993f903d0becf597852c4843d03501281d88a205f800088d5fad8b88b79d307473ce2f5f08f2602d420b89ed173fe312b64cf699f90c59569498
6
+ metadata.gz: d4de98fb656e899a85e0dc074cdd24f8f3a53ea6285464816a61a3b597d7609c58d48e2893ef2391b1dddd834f6e00cb303224bceaa58e82a995b15d8d7ce34b
7
+ data.tar.gz: 3eb5cafa8ad190ada20854ec1143a5d7bee81698abeb5cf4f211b532ef242c0c2a7c267a3a4a23ab0fb601e329c5c6b3273492b4f72c862760be83bdafde2bc4
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "coveralls"
28
28
  spec.add_development_dependency "rake"
29
29
  spec.add_development_dependency "rspec"
30
- spec.add_development_dependency "shoulda-matchers"
30
+ spec.add_development_dependency "shoulda"
31
31
  end
@@ -2,7 +2,7 @@ class AlphaNumericValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.alpha_numeric'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.alpha_numeric'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -11,7 +11,7 @@ class AlphaNumericValidator < ActiveModel::EachValidator
11
11
  def valid_format?(value, options)
12
12
  strict = options.fetch(:strict, false)
13
13
 
14
- value =~ case options[:case]
14
+ value =~ case options.fetch(:case, :any)
15
15
  when :lower
16
16
  strict ? /^[a-z0-9]+$/ : /^[a-z0-9 ]+$/
17
17
  when :upper
@@ -2,7 +2,7 @@ class AlphaValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.alpha'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.alpha'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -11,7 +11,7 @@ class AlphaValidator < ActiveModel::EachValidator
11
11
  def valid_format?(value, options)
12
12
  strict = options.fetch(:strict, false)
13
13
 
14
- value =~ case options[:case]
14
+ value =~ case options.fetch(:case, :any)
15
15
  when :lower
16
16
  strict ? /^[a-z]+$/ : /^[a-z ]+$/
17
17
  when :upper
@@ -2,7 +2,7 @@ class Base64Validator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.base64'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.base64'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,13 +2,13 @@ class BooleanValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless TRUE_VALUES.include?(value) || FALSE_VALUES.include?(value)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.boolean'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.boolean'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
- FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE']
12
- TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
11
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].freeze
12
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
13
13
 
14
14
  end
@@ -1,20 +1,20 @@
1
1
  class CoordinateValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
- boundary = options.fetch(:boundary, :coordinate)
4
+ boundary = options.fetch(:boundary, :coordinate).to_sym
5
5
  unless BOUNDARIES.include?(boundary)
6
6
  raise ArgumentError,
7
- "Unknown boundary: #{boundary.inspect}. Valid boundaries are: #{BOUNDARIES.map(&:inspect).join(', ')}"
7
+ "Unknown boundary: #{boundary.inspect}. Valid boundaries are: #{BOUNDARIES.map(&:inspect).join(', '.freeze)}"
8
8
  end
9
9
 
10
10
  unless valid?(value, options)
11
- record.errors[attribute] << (options[:message] || I18n.t("active_validation.errors.messages.coordinate.#{boundary}"))
11
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t("active_validation.errors.messages.coordinate.#{boundary}"))
12
12
  end
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- BOUNDARIES = [:coordinate, :latitude, :longitude]
17
+ BOUNDARIES = [:coordinate, :latitude, :longitude].freeze
18
18
 
19
19
  def valid_length?(value)
20
20
  value.present?
@@ -29,7 +29,7 @@ class CoordinateValidator < ActiveModel::EachValidator
29
29
  end
30
30
 
31
31
  def valid_boundary?(value, options)
32
- case options.fetch(:boundary, :coordinate)
32
+ case options.fetch(:boundary, :coordinate).to_sym
33
33
  when :latitude
34
34
  valid_latitude?(value)
35
35
  when :longitude
@@ -2,7 +2,7 @@ class CreditCardValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.credit_card'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.credit_card'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -19,7 +19,7 @@ class CreditCardValidator < ActiveModel::EachValidator
19
19
  solo: [16, 18, 19],
20
20
  unionpay: [16, 17, 18, 19],
21
21
  visa: [16]
22
- }
22
+ }.freeze
23
23
 
24
24
  DEFAULT_PREFIXES = {
25
25
  american_express: ['34', '37'],
@@ -47,7 +47,7 @@ class CreditCardValidator < ActiveModel::EachValidator
47
47
  '622920', '622921', '622922', '622923', '622924', '622925'
48
48
  ],
49
49
  visa: ['4']
50
- }
50
+ }.freeze
51
51
 
52
52
  def valid_format?(value, options)
53
53
  value =~ (options.fetch(:strict, false) ? /^[0-9]+$/ : /^[0-9 -.]+$/)
@@ -56,74 +56,39 @@ class CreditCardValidator < ActiveModel::EachValidator
56
56
  def valid_length?(value, options)
57
57
  return(false) unless value.present?
58
58
 
59
- value_size_range = DEFAULT_LENGTHS.values.flatten.uniq.sort
60
- value_size = value.size
59
+ current_card = options.fetch(:card, :all).to_sym
60
+ value_size = value.size
61
61
 
62
- case options[:card]
63
- when :american_express, :amex
64
- DEFAULT_LENGTHS[:american_express].include?(value_size)
65
- when :diners_club
66
- DEFAULT_LENGTHS[:diners_club].include?(value_size)
67
- when :discover
68
- DEFAULT_LENGTHS[:discover].include?(value_size)
69
- when :jcb
70
- DEFAULT_LENGTHS[:jcb].include?(value_size)
71
- when :laser
72
- DEFAULT_LENGTHS[:laser].include?(value_size)
73
- when :maestro
74
- DEFAULT_LENGTHS[:maestro].include?(value_size)
75
- when :mastercard
76
- DEFAULT_LENGTHS[:mastercard].include?(value_size)
77
- when :solo
78
- DEFAULT_LENGTHS[:solo].include?(value_size)
79
- when :unionpay
80
- DEFAULT_LENGTHS[:unionpay].include?(value_size)
81
- when :visa
82
- DEFAULT_LENGTHS[:visa].include?(value_size)
83
- else
62
+ case current_card
63
+ when :amex
64
+ DEFAULT_LENGTHS.fetch(:american_express).include?(value_size)
65
+ when :all
66
+ value_size_range = DEFAULT_LENGTHS.values.flatten.uniq.sort
84
67
  value_size.between?(value_size_range.first, value_size_range.last)
68
+ else
69
+ DEFAULT_LENGTHS.fetch(current_card).include?(value_size)
85
70
  end
86
71
  end
87
72
 
88
73
  def valid_prefix?(value, options)
89
- value_size = value.size
74
+ current_card = options.fetch(:card, :all).to_sym
90
75
 
91
- case options[:card]
92
- when :american_express, :amex
93
- DEFAULT_PREFIXES[:american_express].any? { |prefix| value.start_with?(prefix) }
94
- when :diners_club
95
- DEFAULT_PREFIXES[:diners_club].any? { |prefix| value.start_with?(prefix) }
96
- when :discover
97
- DEFAULT_PREFIXES[:discover].any? { |prefix| value.start_with?(prefix) }
98
- when :jcb
99
- DEFAULT_PREFIXES[:jcb].any? { |prefix| value.start_with?(prefix) }
100
- when :laser
101
- DEFAULT_PREFIXES[:laser].any? { |prefix| value.start_with?(prefix) }
102
- when :maestro
103
- DEFAULT_PREFIXES[:maestro].any? { |prefix| value.start_with?(prefix) }
104
- when :mastercard
105
- DEFAULT_PREFIXES[:mastercard].any? { |prefix| value.start_with?(prefix) }
106
- when :solo
107
- DEFAULT_PREFIXES[:solo].any? { |prefix| value.start_with?(prefix) }
108
- when :unionpay
109
- DEFAULT_PREFIXES[:unionpay].any? { |prefix| value.start_with?(prefix) }
110
- when :visa
111
- DEFAULT_PREFIXES[:visa].any? { |prefix| value.start_with?(prefix) }
112
- else
76
+ case current_card
77
+ when :amex
78
+ DEFAULT_PREFIXES.fetch(:american_express).any? { |p| value.start_with?(p) }
79
+ when :all
113
80
  result = false
114
- DEFAULT_LENGTHS.each do |key, values|
115
- if values.include?(value_size)
116
- result = DEFAULT_PREFIXES[key].any? { |prefix| value.start_with?(prefix) }
117
- break if result
118
- end
119
- break if result
81
+ DEFAULT_LENGTHS.lazy.each do |key, values|
82
+ break if result = DEFAULT_PREFIXES.fetch(key).any? { |p| value.start_with?(p) } if values.include?(value.size)
120
83
  end
121
- return(result)
84
+ result
85
+ else
86
+ DEFAULT_PREFIXES.fetch(current_card).any? { |p| value.start_with?(p) }
122
87
  end
123
88
  end
124
89
 
125
90
  def valid?(value, options)
126
- striped_value = value.gsub(/\D/, '')
91
+ striped_value = value.gsub(/\D/, ''.freeze)
127
92
 
128
93
  valid_format?(value, options) &&
129
94
  valid_length?(striped_value, options) &&
@@ -2,7 +2,7 @@ class CurrencyValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.currency'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.currency'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,18 +2,18 @@ class CusipValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.cusip'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.cusip'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
11
  def valid_checksum?(value)
12
- digits = value.chars.map { |character| character.match(/[A-Z]/) ? (character.ord - 55) : character.to_i }
12
+ digits = value.chars.map { |c| c.match(/[A-Z]/) ? (c.ord - 55) : c.to_i }
13
13
  even_values = digits.values_at(* digits.each_index.select { |i| i.even? })
14
14
  odd_values = digits.values_at(* digits.each_index.select { |i| i.odd? })
15
15
  values = odd_values.map { |i| i * 2 }.zip(even_values).flatten
16
- values = values.inject(0) { |sum, i| sum += (i / 10) + i % 10 }
16
+ values = values.inject(0) { |s, i| s += (i / 10) + i % 10 }
17
17
 
18
18
  ((10 - values) % 10) % 10
19
19
  end
@@ -2,14 +2,14 @@ class EmailValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.email'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.email'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
11
  def valid_domain?(value, options)
12
- options.empty? || options.any? { |domain| value.downcase.end_with?(".#{domain.downcase}") }
12
+ options.empty? || options.any? { |d| value.downcase.end_with?(".#{d.downcase}") }
13
13
  end
14
14
 
15
15
  def valid_format?(value)
@@ -23,7 +23,7 @@ class EmailValidator < ActiveModel::EachValidator
23
23
  def valid?(value, options)
24
24
  valid_length?(value) &&
25
25
  valid_format?(value) &&
26
- valid_domain?(value, [*(options[:domain])])
26
+ valid_domain?(value, [*(options.fetch(:domain, nil))])
27
27
  end
28
28
 
29
29
  end
@@ -11,12 +11,12 @@ class EqualityValidator < ActiveModel::EachValidator
11
11
  operators = OPERATORS.keys
12
12
  unless operators.include?(operator)
13
13
  raise ArgumentError,
14
- "Unknown operator: #{operator.inspect}. Valid operators are: #{operators.map(&:inspect).join(', ')}"
14
+ "Unknown operator: #{operator.inspect}. Valid operators are: #{operators.map(&:inspect).join(', '.freeze)}"
15
15
  end
16
16
  operator = OPERATORS.fetch(operator)
17
17
 
18
18
  unless value.send(operator.to_sym, record.send(to.to_sym))
19
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.equality', attr: to, operator: operator))
19
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.equality'.freeze, attr: to, operator: operator))
20
20
  end
21
21
  end
22
22
 
@@ -29,6 +29,6 @@ class EqualityValidator < ActiveModel::EachValidator
29
29
  greater_than_or_equal_to: :>=,
30
30
  equal_to: :==,
31
31
  not_equal_to: :!=
32
- }
32
+ }.freeze
33
33
 
34
34
  end
@@ -2,7 +2,7 @@ class HexValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.hex'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.hex'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ class ImeiValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.imei'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.imei'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -17,11 +17,11 @@ class ImeiValidator < ActiveModel::EachValidator
17
17
  end
18
18
 
19
19
  def valid_luhn?(value)
20
- number = value.gsub(/\D/, '').reverse
20
+ number = value.gsub(/\D/, ''.freeze).reverse
21
21
 
22
22
  total = 0
23
- number.chars.each_with_index do |character, i|
24
- result = character.to_i
23
+ number.chars.lazy.each_with_index do |c, i|
24
+ result = c.to_i
25
25
  result *= 2 if i.odd?
26
26
  result = (1 + (result - 10)) if (result >= 10)
27
27
  total += result
@@ -2,7 +2,7 @@ class IpValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.ip'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.ip'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,18 +2,18 @@ class IsbnValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.isbn'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.isbn'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
11
  def valid_format?(value)
12
- value = '' if value.blank?
13
- value = value.gsub(/-| /, '').downcase.chars
12
+ value = ''.freeze if value.blank?
13
+ value = value.gsub(/-| /, ''.freeze).downcase.chars
14
14
 
15
- [10, 13].include?(value.size) &&
16
- value.all? { |character| ['0', '1', '2' , '3', '4', '5', '6', '7', '8', '9', '0', 'x'].include?(character) }
15
+ [10, 13].freeze.include?(value.size) &&
16
+ value.all? { |c| ['0', '1', '2' , '3', '4', '5', '6', '7', '8', '9', '0', 'x'].freeze.include?(c) }
17
17
  end
18
18
 
19
19
  def valid_length?(value)
@@ -2,7 +2,7 @@ class IsinValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.isin'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.isin'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -10,7 +10,7 @@ class IsinValidator < ActiveModel::EachValidator
10
10
 
11
11
  def valid_checksum?(value)
12
12
  characters = value.chars
13
- digits = characters.map { |character| character.match(/[A-Z]/) ? (character.ord - 55) : character.to_i }
13
+ digits = characters.map { |c| c.match(/[A-Z]/) ? (c.ord - 55) : c.to_i }
14
14
  even_values = digits.values_at(* digits.each_index.select { |i| i.even? })
15
15
  odd_values = digits.values_at(* digits.each_index.select { |i| i.odd? })
16
16
 
@@ -2,7 +2,7 @@ class MacAddressValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.mac_address'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.mac_address'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -15,15 +15,12 @@ class MacAddressValidator < ActiveModel::EachValidator
15
15
  /^([\h]{6}):[\h]{6}?$/i,
16
16
  /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i,
17
17
  /^[\h]{12}?$/i
18
- ]
18
+ ].freeze
19
19
 
20
20
  def valid_format?(value)
21
21
  result = false
22
- DEFAULT_FORMATS.each do |pattern|
23
- result = (value =~ pattern)
24
- break if result
25
- end
26
- return(result)
22
+ DEFAULT_FORMATS.lazy.each { |p| break if result = (value =~ p) }
23
+ result
27
24
  end
28
25
 
29
26
  def valid_length?(value)
@@ -2,7 +2,7 @@ class NameValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.name'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.name'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ class PasswordValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.password'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.password'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ class PhoneValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.phone'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.phone'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,20 +2,18 @@ class SedolValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.sedol'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.sedol'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
11
  def valid_checksum?(value)
12
- digits = value.chars.map { |digit| digit.match(/[A-Z]/) ? (digit.ord - 55) : digit.to_i }
13
- weights = [1, 3, 1, 7, 3, 9, 1]
12
+ digits = value.chars.map { |d| d.match(/[A-Z]/) ? (d.ord - 55) : d.to_i }
13
+ weights = [1, 3, 1, 7, 3, 9, 1].freeze
14
14
 
15
15
  total = 0
16
- digits.each_with_index do |digit, i|
17
- total += (weights[i] * digit)
18
- end
16
+ digits.lazy.each_with_index { |d, i| total += (weights[i] * d) }
19
17
 
20
18
  (10 - total % 10) % 10
21
19
  end
@@ -2,7 +2,7 @@ class SlugValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.slug'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.slug'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ class SsnValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.ssn'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.ssn'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.tracking_number'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.tracking_number'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -27,7 +27,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
27
27
  usps20: /^([0-9]{2,2})([0-9]{9,9})([0-9]{8,8})([0-9])$/,
28
28
  usps91: /^(?:420\d{5})?(9[1-5](?:[0-9]{19}|[0-9]{23}))([0-9])$/
29
29
  }
30
- }
30
+ }.freeze
31
31
 
32
32
  # DHL
33
33
  DEFAULT_CARRIERS_AND_SERVICES.fetch(:dhl).each do |service, pattern|
@@ -50,7 +50,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
50
50
  sequence, check_digit = formula
51
51
 
52
52
  total = 0
53
- sequence.chars.zip([3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1]).each do |(char1, char2)|
53
+ sequence.chars.zip([3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1].freeze).lazy.each do |(char1, char2)|
54
54
  total += (char1.to_i * char2)
55
55
  end
56
56
 
@@ -59,14 +59,14 @@ class TrackingNumberValidator < ActiveModel::EachValidator
59
59
 
60
60
  DEFAULT_CARRIERS_AND_SERVICES.fetch(:fedex).only(:ground, :ground18, :ground96).each_with_index do |(service, pattern), i|
61
61
  define_method("valid_fedex_#{service}_checksum?") do |value|
62
- return(false) unless value.size == [15, 18, 22].at(i)
62
+ return(false) unless value.size == [15, 18, 22].freeze.at(i)
63
63
 
64
64
  formula = value.scan(pattern).flatten.compact
65
65
  return(false) if formula.empty?
66
66
  sequence, check_digit = formula
67
67
 
68
68
  total = 0
69
- sequence.chars.reverse.each_with_index do |character, i|
69
+ sequence.chars.reverse.lazy.each_with_index do |character, i|
70
70
  result = character.to_i
71
71
  result *= 3 if i.even?
72
72
  total += result
@@ -88,7 +88,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
88
88
  sequence, check_digit = formula
89
89
 
90
90
  total = 0
91
- sequence.chars.reverse.each_with_index do |character, i|
91
+ sequence.chars.reverse.lazy.each_with_index do |character, i|
92
92
  result = character.to_i
93
93
  result *= 3 if i.even?
94
94
  total += result
@@ -103,14 +103,14 @@ class TrackingNumberValidator < ActiveModel::EachValidator
103
103
  DEFAULT_CARRIERS_AND_SERVICES.only(:ontrac, :ups).each_with_index do |(carrier, services), i|
104
104
  services.each do |service, pattern|
105
105
  define_method("valid_#{carrier}_#{service}_checksum?") do |value|
106
- return(false) unless value.size == [15, 18].at(i)
106
+ return(false) unless value.size == [15, 18].freeze.at(i)
107
107
 
108
108
  formula = value.scan(pattern).flatten.compact
109
109
  return(false) if formula.empty?
110
110
  sequence, check_digit = formula
111
111
 
112
112
  total = 0
113
- sequence.chars.each_with_index do |character, i|
113
+ sequence.chars.lazy.each_with_index do |character, i|
114
114
  result = character[/[0-9]/] ? character.to_i : ((character[0].ord - 3) % 10)
115
115
  result *= 2 if i.odd?
116
116
  total += result
@@ -135,7 +135,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
135
135
  check_digit = characters.pop.to_i
136
136
 
137
137
  total = 0
138
- characters.zip([8, 6, 4, 2, 3, 5, 9, 7]).each do |pair|
138
+ characters.zip([8, 6, 4, 2, 3, 5, 9, 7].freeze).lazy.each do |pair|
139
139
  total += (pair[0].to_i * pair[1].to_i)
140
140
  end
141
141
 
@@ -163,7 +163,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
163
163
  check_digit = sequence.last.to_i
164
164
 
165
165
  total = 0
166
- characters.reverse.each_with_index do |character, i|
166
+ characters.reverse.lazy.each_with_index do |character, i|
167
167
  result = character.to_i
168
168
  result *= 3 if i.even?
169
169
  total += result
@@ -186,7 +186,7 @@ class TrackingNumberValidator < ActiveModel::EachValidator
186
186
  check_digit = sequence.last.to_i
187
187
 
188
188
  total = 0
189
- characters.reverse.each_with_index do |character, i|
189
+ characters.reverse.lazy.each_with_index do |character, i|
190
190
  result = character.to_i
191
191
  result *= 3 if i.even?
192
192
  total += result
@@ -5,16 +5,16 @@ class UrlValidator < ActiveModel::EachValidator
5
5
  uri = URI.parse(value.to_s)
6
6
  raise URI::InvalidURIError unless valid?(uri, options)
7
7
  rescue URI::InvalidURIError
8
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.url'))
8
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.url'.freeze))
9
9
  end
10
10
 
11
11
  private
12
12
 
13
- DEFAULT_SCHEMES = [:http, :https]
13
+ DEFAULT_SCHEMES = [:http, :https].freeze
14
14
 
15
15
  def valid_domain?(value, options)
16
16
  value_downcased = value.host.to_s.downcase
17
- options.empty? || options.any? { |domain| value_downcased.end_with?(".#{domain.downcase}") }
17
+ options.empty? || options.any? { |d| value_downcased.end_with?(".#{d.downcase}".freeze) }
18
18
  end
19
19
 
20
20
  def valid_length?(value)
@@ -23,11 +23,11 @@ class UrlValidator < ActiveModel::EachValidator
23
23
 
24
24
  def valid_scheme?(value, options)
25
25
  value_downcased = value.scheme.to_s.downcase
26
- options.empty? || options.any? { |scheme| value_downcased == scheme.to_s.downcase }
26
+ options.empty? || options.any? { |s| value_downcased == s.to_s.downcase }
27
27
  end
28
28
 
29
29
  def valid_root?(value)
30
- ['/', ''].include?(value.path) && value.query.blank? && value.fragment.blank?
30
+ ['/', ''].freeze.include?(value.path) && value.query.blank? && value.fragment.blank?
31
31
  end
32
32
 
33
33
  def valid_uri?(value)
@@ -37,9 +37,9 @@ class UrlValidator < ActiveModel::EachValidator
37
37
  def valid?(value, options)
38
38
  valid_length?(value) &&
39
39
  valid_uri?(value) &&
40
- valid_domain?(value, [*(options[:domain])]) &&
41
- valid_scheme?(value, [*(options[:scheme] || UrlValidator::DEFAULT_SCHEMES)]) &&
42
- (options[:root] ? valid_root?(value) : true)
40
+ valid_domain?(value, [*(options.fetch(:domain, nil))]) &&
41
+ valid_scheme?(value, [*(options.fetch(:scheme, UrlValidator::DEFAULT_SCHEMES))]) &&
42
+ (options.fetch(:root, false) ? valid_root?(value) : true)
43
43
  end
44
44
 
45
45
  end
@@ -2,7 +2,7 @@ class UsernameValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.username'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.username'.freeze))
6
6
  end
7
7
  end
8
8
 
@@ -2,14 +2,14 @@ class UuidValidator < ActiveModel::EachValidator
2
2
 
3
3
  def validate_each(record, attribute, value)
4
4
  unless valid?(value.to_s, options)
5
- record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.uuid'))
5
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.uuid'.freeze))
6
6
  end
7
7
  end
8
8
 
9
9
  private
10
10
 
11
11
  def valid_format?(value, options)
12
- value =~ case options[:version]
12
+ value =~ case options.fetch(:version, 0)
13
13
  when 3
14
14
  /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
15
15
  when 4
@@ -1,3 +1,3 @@
1
1
  module ActiveValidation
2
- VERSION = "2.2.2"
2
+ VERSION = "2.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: shoulda-matchers
112
+ name: shoulda
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="