lib/sec_id/base.rb in sec_id-1.1.0 vs lib/sec_id/base.rb in sec_id-2.0.0

- old
+ new

@@ -22,11 +22,11 @@ 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, '*' => 36, '@' => 37, '#' => 38 }.freeze - attr_reader :identifier, :check_digit + attr_reader :full_number, :identifier, :check_digit def self.valid?(id) new(id).valid? end @@ -45,19 +45,22 @@ def initialize(_sec_id_number) raise NotImplementedError end def valid? - raise NotImplementedError + return false unless valid_format? + + check_digit == calculate_check_digit end def valid_format? - raise NotImplementedError + identifier ? true : false end def restore! - raise NotImplementedError + @check_digit = calculate_check_digit + @full_number = to_s end def calculate_check_digit raise NotImplementedError end @@ -71,18 +74,27 @@ def digitized_identifier raise NotImplementedError end + def parse(sec_id_number) + @full_number = sec_id_number.to_s.strip.upcase + @full_number.match(self.class::ID_REGEX) || {} + end + def char_to_digits(char) CHAR_TO_DIGITS.fetch(char) end def char_to_digit(char) CHAR_TO_DIGIT.fetch(char) end def mod_10(sum) (10 - (sum % 10)) % 10 + end + + def div_10_mod_10(number) + (number / 10) + (number % 10) end end end