Class: PolishValidators::RegonValidator
- Inherits:
-
Object
- Object
- PolishValidators::RegonValidator
- Defined in:
- lib/polish_validators/regon_validator.rb
Instance Method Summary collapse
-
#initialize(regon) ⇒ RegonValidator
constructor
A new instance of RegonValidator.
- #valid? ⇒ Boolean
Constructor Details
#initialize(regon) ⇒ RegonValidator
Returns a new instance of RegonValidator.
3 4 5 |
# File 'lib/polish_validators/regon_validator.rb', line 3 def initialize(regon) @regon = regon.to_s end |
Instance Method Details
#valid? ⇒ Boolean
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/polish_validators/regon_validator.rb', line 7 def valid? return unless @regon =~ /\A\d{9,14}\Z/ weights8 = [8, 9, 2, 3, 4, 5, 6, 7] weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8] regon = @regon.split(//).collect(&:to_i) checksum = (regon.length == 9 ? weights8 : weights14).reduce(0) { |a, e| a + regon.shift * e } checksum % 11 == regon.shift end |