Module: ValidateFormat

Included in:
Dixon::Validators::Taiwan
Defined in:
lib/dixon/validators/taiwan.rb

Constant Summary collapse

TAIWAN_ID_REGEXP =

A123456789

/[a-zA-Z][1|2][0-9]+/i

Instance Method Summary collapse

Instance Method Details

#mObject

Add a before_hook for methods that defined in literal symbol array.



141
142
143
144
145
146
147
148
149
150
# File 'lib/dixon/validators/taiwan.rb', line 141

%i(checks gender issued_by convert).each do |m|
  define_method(m) do |id|
    is_it_valid = validate_id_format id
    if is_it_valid == 'Valid ID.'
      super(id)
    else
      return is_it_valid
    end
  end
end

#validate_id_format(id) ⇒ Object

Check if given id’s format is valid. The current ID number has exact 10 digits The first digit is one capital English letter and is followed by nine Arabic numerals. returns

'Empty ID.' for empty string
'Valid ID.' if the format is correct.
otherwise, 'Invalid ID number.'


131
132
133
134
135
136
# File 'lib/dixon/validators/taiwan.rb', line 131

def validate_id_format(id)
  id_str = id.to_s
  return 'Empty ID.' if id_str.empty?
  return 'Valid ID.' if id_str =~ TAIWAN_ID_REGEXP and id_str.size == 10
  return Dixon::Validators::Taiwan::INVALID_MESSAGE
end