Class: Parxer::Formatter::Rut

Inherits:
Base
  • Object
show all
Defined in:
lib/parxer/formatters/rut_formatter.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#apply, #initialize

Constructor Details

This class inherits a constructor from Parxer::Formatter::Base

Instance Method Details

#clean_rut(rut) ⇒ Object



11
12
13
# File 'lib/parxer/formatters/rut_formatter.rb', line 11

def clean_rut(rut)
  rut.scan(/(\d|k)/i).flatten.join("").upcase
end

#clean_rut?Boolean

Returns:



28
29
30
# File 'lib/parxer/formatters/rut_formatter.rb', line 28

def clean_rut?
  !!config[:clean]
end

#format_rut(rut) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/parxer/formatters/rut_formatter.rb', line 15

def format_rut(rut)
  last_digit = rut[-1]
  digits = rut[0...-1].split("").reverse
  result = []

  digits.each_with_index do |number, idx|
    result << "." if !idx.zero? && (idx % 3).zero?
    result << number
  end

  result.reverse.join("") + "-" + last_digit
end

#format_value(rut) ⇒ Object



4
5
6
7
8
9
# File 'lib/parxer/formatters/rut_formatter.rb', line 4

def format_value(rut)
  rut = clean_rut(rut)
  return nil if rut.empty?
  return rut if clean_rut?
  format_rut(rut)
end