Class: Dixon::Validators::Taiwan
- Inherits:
-
Object
- Object
- Dixon::Validators::Taiwan
- Includes:
- ValidateFormat
- Defined in:
- lib/dixon/validators/taiwan.rb,
lib/dixon/validators/taiwan.rb
Constant Summary collapse
- INVALID_MESSAGE =
'Invalid ID number.'
- LETTER_ISSUED_BY =
{ # Active Letters 'A' => 'Taipei City' , 'B' => 'Taichung City', 'C' => 'Keelung City' , 'D' => 'Tainan City', 'E' => 'Kaohsiung City' , 'F' => 'New Taipei City', 'G' => 'Yilan County' , 'H' => 'Taoyuan County', 'I' => 'Chiayi City' , 'J' => 'Hsinchu County', 'K' => 'Miaoli County' , 'M' => 'Nantou County', 'N' => 'Changhua County' , 'O' => 'Hsinchu City', 'P' => 'Yunlin County' , 'Q' => 'Chiayi County', 'T' => 'Pingtung County' , 'U' => 'Hualien County', 'V' => 'Taitung County' , 'W' => 'Kinmen County', 'X' => 'Penghu County' , 'Z' => 'Lienchiang County', # Letters no longer issued 'L' => 'Taichung County' , 'R' => 'Tainan County' , 'S' => 'Kaohsiung County', 'Y' => 'Yangmingshan Management Bureau' }
- LETTER_NUMBERS =
{ 'A' => '10', 'B' => '11', 'C' => '12', 'D' => '13', 'E' => '14', 'F' => '15', 'G' => '16', 'H' => '17', 'J' => '18', 'K' => '19', 'L' => '20', 'M' => '21', 'N' => '22', 'P' => '23', 'Q' => '24', 'R' => '25', 'S' => '26', 'T' => '27', 'U' => '28', 'V' => '29', 'X' => '30', 'Y' => '31', 'W' => '32', 'Z' => '33', 'I' => '34', 'O' => '35' }
- RULE_NUMBERS =
[1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
Constants included from ValidateFormat
ValidateFormat::TAIWAN_ID_REGEXP
Instance Method Summary collapse
-
#checks(id) ⇒ Object
Check if a given ID is valid.
-
#gender(id) ⇒ Object
Returns gender for given ID.
-
#issued_by(id) ⇒ Object
Returns local agencies who issued your id.
Methods included from ValidateFormat
Instance Method Details
#checks(id) ⇒ Object
Check if a given ID is valid. ‘id` must be a string, case is insensitive. First convert ID into all numbers, then multiply to RULE_NUMBERS element by element. Sum up and take a 10 modulo then check if remainder is 0. If it’s, return true, false otherwise.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dixon/validators/taiwan.rb', line 43 def checks(id) # "F127337575" => [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5] converted = convert id.to_s # [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5] # * [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1] # -------------------------------------------- # sum 1, 45, 8, 14, 43, 15, 12, 21, 10, 7, 5 # => 180 # 180 mod 10, is the remainder equal to zero? If so, return true. # return (mapcar(-> (a,b) { a * b }, converted, RULE_NUMBERS).reduce(:+).divmod 10)[1] == 0 end |
#gender(id) ⇒ Object
Returns gender for given ID. By check the second digit of ID. 1 is male, 2 is female.
59 60 61 62 63 64 65 |
# File 'lib/dixon/validators/taiwan.rb', line 59 def gender(id) case id[1] when '1' then 'male' when '2' then 'female' else puts INVALID_MESSAGE end end |
#issued_by(id) ⇒ Object
Returns local agencies who issued your id.
68 69 70 |
# File 'lib/dixon/validators/taiwan.rb', line 68 def issued_by(id) LETTER_ISSUED_BY[id[0]] end |