Class: Uy::Rut

Inherits:
Object
  • Object
show all
Defined in:
lib/uy/rut.rb

Constant Summary collapse

FACTORS =
[4,3,2,9,8,7,6,5,4,3,2]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rut = nil) ⇒ Rut

Returns a new instance of Rut.



7
8
9
# File 'lib/uy/rut.rb', line 7

def initialize(rut=nil)
  @rut = rut || Rut.generate
end

Instance Attribute Details

#rutObject

Returns the value of attribute rut.



5
6
7
# File 'lib/uy/rut.rb', line 5

def rut
  @rut
end

Instance Method Details

#is_valid?Boolean Also known as: valid?

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/uy/rut.rb', line 11

def is_valid?
  return false unless @rut =~ /\d{12}/
  return false unless (1..21) === @rut[0..1].to_i
  (2..7).each {|pos| return false unless (1..9) === @rut[pos].to_i}
  return false unless @rut[8..9] == '00'

  begin
    return false unless Rut.calculate_check_digit(@rut) == @rut[11].to_i
  rescue
    return false
  end

  true
end