Module: Formagio::Utils

Defined in:
lib/formagio/utils.rb,
lib/formagio/utils/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.add_mask_cnpj(value) ⇒ Object



45
46
47
# File 'lib/formagio/utils.rb', line 45

def self.add_mask_cnpj(value)
  value
end

.add_mask_cpf(value) ⇒ Object



15
16
17
# File 'lib/formagio/utils.rb', line 15

def self.add_mask_cpf(value)
  "#{value[0..2]}.#{value[3..5]}.#{value[6..8]}-#{value[9..10]}"
end

.is_cnpj?(doc) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/formagio/utils.rb', line 41

def self.is_cnpj?(doc)
  doc
end

.is_cpf?(cpf = nil) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/formagio/utils.rb', line 19

def self.is_cpf?(cpf=nil)
  return false if cpf.nil?

  black_list = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}
  wvalor = cpf.scan /[0-9]/
  if wvalor.length == 11
    unless black_list.member?(wvalor.join)
      wvalor = wvalor.collect{|x| x.to_i}
      wsoma = 10*wvalor[0]+9*wvalor[1]+8*wvalor[2]+7*wvalor[3]+6*wvalor[4]+5*wvalor[5]+4*wvalor[6]+3*wvalor[7]+2*wvalor[8]
      wsoma = wsoma - (11 * (wsoma/11))
      wresult1 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma
      if wresult1 == wvalor[9]
        wsoma = wvalor[0]*11+wvalor[1]*10+wvalor[2]*9+wvalor[3]*8+wvalor[4]*7+wvalor[5]*6+wvalor[6]*5+wvalor[7]*4+wvalor[8]*3+wvalor[9]*2
        wsoma = wsoma - (11 * (wsoma/11))
        wresult2 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma
        return true if wresult2 == wvalor[10] # CPF validado
      end
    end
  end
  return false # CPF invalidado
end

.is_integer?(value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/formagio/utils.rb', line 6

def self.is_integer?(value)
  val2=value.to_i.to_s
  return (val2==value) ? true : false
end

.so_numero(value) ⇒ Object



11
12
13
# File 'lib/formagio/utils.rb', line 11

def self.so_numero(value)
  value.scan(/[0-9]/).join
end