Class: SPED2SQL::Conversor

Inherits:
Pipeline::Base show all
Includes:
Layout
Defined in:
lib/sped2sql/conversor.rb

Constant Summary

Constants included from Layout

Layout::TEMPLATE_PATH

Instance Attribute Summary collapse

Attributes inherited from Pipeline::Base

#tasks

Instance Method Summary collapse

Methods inherited from Pipeline::Base

#<<, #execute

Constructor Details

#initialize(fonte, template, options = {}) ⇒ Conversor

Returns a new instance of Conversor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sped2sql/conversor.rb', line 8

def initialize(fonte, template, options = {})
  @fonte      = fonte
  @template   = template.is_a?(Symbol) ? Mapa.arquivo_template(template) : template
  @saida      = []
  @memoria    = Hash.new { |k, v| k[v] = [] }
  @options    = options

  valida_arquivo(@fonte)
  valida_arquivo(@template)

  tasks = if options[:tasks].is_a?(Array)
            options[:tasks]
          elsif options[:tasks] == :vazio
            []
          else
            [Pipeline::NormalizaSQL, Pipeline::AddHash]
          end

  super(tasks)
end

Instance Attribute Details

#fonteObject (readonly)

Returns the value of attribute fonte.



6
7
8
# File 'lib/sped2sql/conversor.rb', line 6

def fonte
  @fonte
end

#memoriaObject (readonly)

Returns the value of attribute memoria.



6
7
8
# File 'lib/sped2sql/conversor.rb', line 6

def memoria
  @memoria
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/sped2sql/conversor.rb', line 6

def options
  @options
end

#saidaObject (readonly)

Returns the value of attribute saida.



6
7
8
# File 'lib/sped2sql/conversor.rb', line 6

def saida
  @saida
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/sped2sql/conversor.rb', line 6

def template
  @template
end

Instance Method Details

#converter!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sped2sql/conversor.rb', line 29

def converter!
  mapa  = Mapa.carrega!(@template)
  dados = IO.read(fonte, encoding: 'ISO-8859-1').gsub("'", '"')

  CSV.parse(dados, col_sep: '|', quote_char: "'") do |row|
    # pula linha se o registro nao existe no mapa
    next unless mapa.has_key?(row[1])

    # O primeiro e o ultimo item de uma linha no SPED sempre eh nulo
    linha = row.clone[1..-2]

    # Executa o pipe
    pipe = execute({ original: linha,
                     final:    linha,
                     mapa:     mapa,
                     memoria:  @memoria,
                     saida:    @saida,
                     options:  @options })

    @saida << pipe[:final]
    @memoria[linha.first] << pipe[:final]

    # Para um arquivo completo do SPED, 9999 eh o ultimo registro.
    # termina a leitura do arquivo no registro 9999 evitando ler
    # linhas em branco ou assinatura digital
    break if linha[0].to_i == 9999
  end
end

#to_sqlObject



58
59
60
# File 'lib/sped2sql/conversor.rb', line 58

def to_sql
  SQL::Parser.to_sql(@saida, @options[:db] || {})
end