Class: ValidatesRfc::Rfc

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_rfc/rfc.rb

Constant Summary collapse

COMPANY_REGEX =
/\A([A-ZÑ&]{3})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze
PERSON_REGEX =
/\A([A-ZÑ&]{4})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze
REGEX =
/\A([A-ZÑ&]{3,4})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze

Instance Method Summary collapse

Constructor Details

#initialize(value, type = :both) ⇒ Rfc

Returns a new instance of Rfc.



9
10
11
12
# File 'lib/validates_rfc/rfc.rb', line 9

def initialize(value, type = :both)
  @value = value
  @type = type
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/validates_rfc/rfc.rb', line 14

def valid?
  return true if @value.blank?

  case @type
  when :both then @value.match(REGEX)
  when :person then @value.match(PERSON_REGEX)
  when :company then @value.match(COMPANY_REGEX)
  end

  Regexp.last_match(0).present? && valid_date?(Regexp.last_match(2))
end