class Uy::Rut
Constants
- FACTORS
Attributes
rut[RW]
Public Class Methods
new(rut=nil)
click to toggle source
# File lib/uy/rut.rb, line 7 def initialize(rut=nil) @rut = rut || Rut.generate end
Protected Class Methods
calculate_check_digit(rut)
click to toggle source
# File lib/uy/rut.rb, line 29 def self.calculate_check_digit(rut) rut = rut.length == 12 ? rut.chop : rut checksum = rut .chars .to_a .map(&:to_i) .zip(FACTORS) .map {|d| d.reduce(&:*)} .reduce(:+) check_digit = 11 - (checksum % 11) check_digit = 0 if check_digit == 11 fail if check_digit == 10 check_digit end
generate()
click to toggle source
# File lib/uy/rut.rb, line 45 def self.generate rut = sprintf('%02d', rand(1..21)) 6.times { rut += rand(1..9).to_s } rut += '00' rut += rand(0..9).to_s begin rut += calculate_check_digit(rut).to_s rescue # bad checksum, retry return Rut.generate end rut end
Public Instance Methods
is_valid?()
click to toggle source
# 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 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
Also aliased as: valid?