Class: ValidatesGuatemalaIdentity::GuatemalaIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_guatemala_identity/guatemala_identity.rb

Constant Summary collapse

REGEX =
/\A([0-9]{8})([0-9])([0-9]{2})([0-9]{2})\z/i.freeze

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ GuatemalaIdentity

Returns a new instance of GuatemalaIdentity.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/validates_guatemala_identity/guatemala_identity.rb', line 7

def initialize(value)
  @value = value.to_s

  return unless @value.present?

  @value.match(REGEX)
  @number = Regexp.last_match(1)
  @checksum = Regexp.last_match(2).to_i
  @birth_state_code = Regexp.last_match(3).to_i
  @birth_city_code = Regexp.last_match(4).to_i
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/validates_guatemala_identity/guatemala_identity.rb', line 19

def valid?
  return true if @value.blank?
  return false if @number.nil?
  return false if @birth_state_code.zero? || @birth_state_code > biggest_state_code
  return false if @birth_city_code.zero? || @birth_city_code > biggest_city_code_for(@birth_state_code)

  checksum_matches?
end