Class: SecId::Base
- Inherits:
-
Object
- Object
- SecId::Base
- Defined in:
- lib/sec_id/base.rb
Constant Summary collapse
- CHAR_TO_DIGITS =
{ '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => [1, 0], 'B' => [1, 1], 'C' => [1, 2], 'D' => [1, 3], 'E' => [1, 4], 'F' => [1, 5], 'G' => [1, 6], 'H' => [1, 7], 'I' => [1, 8], 'J' => [1, 9], 'K' => [2, 0], 'L' => [2, 1], 'M' => [2, 2], 'N' => [2, 3], 'O' => [2, 4], 'P' => [2, 5], 'Q' => [2, 6], 'R' => [2, 7], 'S' => [2, 8], 'T' => [2, 9], 'U' => [3, 0], 'V' => [3, 1], 'W' => [3, 2], 'X' => [3, 3], 'Y' => [3, 4], 'Z' => [3, 5], '*' => [3, 6], '@' => [3, 7], '#' => [3, 8] }.freeze
- CHAR_TO_DIGIT =
{ '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, '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
Instance Attribute Summary collapse
-
#check_digit ⇒ Object
readonly
Returns the value of attribute check_digit.
-
#full_number ⇒ Object
readonly
Returns the value of attribute full_number.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Class Method Summary collapse
- .check_digit(id) ⇒ Object
- .restore!(id_without_check_digit) ⇒ Object
- .valid?(id) ⇒ Boolean
- .valid_format?(id) ⇒ Boolean
Instance Method Summary collapse
- #calculate_check_digit ⇒ Object
-
#initialize(_sec_id_number) ⇒ Base
constructor
A new instance of Base.
- #restore! ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #valid? ⇒ Boolean
- #valid_format? ⇒ Boolean
Constructor Details
#initialize(_sec_id_number) ⇒ Base
Returns a new instance of Base.
45 46 47 |
# File 'lib/sec_id/base.rb', line 45 def initialize(_sec_id_number) raise NotImplementedError end |
Instance Attribute Details
#check_digit ⇒ Object (readonly)
Returns the value of attribute check_digit.
27 28 29 |
# File 'lib/sec_id/base.rb', line 27 def check_digit @check_digit end |
#full_number ⇒ Object (readonly)
Returns the value of attribute full_number.
27 28 29 |
# File 'lib/sec_id/base.rb', line 27 def full_number @full_number end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
27 28 29 |
# File 'lib/sec_id/base.rb', line 27 def identifier @identifier end |
Class Method Details
.check_digit(id) ⇒ Object
41 42 43 |
# File 'lib/sec_id/base.rb', line 41 def self.check_digit(id) new(id).calculate_check_digit end |
.restore!(id_without_check_digit) ⇒ Object
37 38 39 |
# File 'lib/sec_id/base.rb', line 37 def self.restore!(id_without_check_digit) new(id_without_check_digit).restore! end |
.valid?(id) ⇒ Boolean
29 30 31 |
# File 'lib/sec_id/base.rb', line 29 def self.valid?(id) new(id).valid? end |
.valid_format?(id) ⇒ Boolean
33 34 35 |
# File 'lib/sec_id/base.rb', line 33 def self.valid_format?(id) new(id).valid_format? end |
Instance Method Details
#calculate_check_digit ⇒ Object
64 65 66 |
# File 'lib/sec_id/base.rb', line 64 def calculate_check_digit raise NotImplementedError end |
#restore! ⇒ Object
59 60 61 62 |
# File 'lib/sec_id/base.rb', line 59 def restore! @check_digit = calculate_check_digit @full_number = to_s end |
#to_s ⇒ Object Also known as: to_str
68 69 70 |
# File 'lib/sec_id/base.rb', line 68 def to_s "#{identifier}#{check_digit}" end |
#valid? ⇒ Boolean
49 50 51 52 53 |
# File 'lib/sec_id/base.rb', line 49 def valid? return false unless valid_format? check_digit == calculate_check_digit end |
#valid_format? ⇒ Boolean
55 56 57 |
# File 'lib/sec_id/base.rb', line 55 def valid_format? identifier ? true : false end |