Class: SPED2SQL::Conversor
- Inherits:
-
Pipeline::Base
- Object
- Pipeline::Base
- SPED2SQL::Conversor
- Includes:
- Layout
- Defined in:
- lib/sped2sql/conversor.rb
Constant Summary
Constants included from Layout
Instance Attribute Summary collapse
-
#fonte ⇒ Object
readonly
Returns the value of attribute fonte.
-
#memoria ⇒ Object
readonly
Returns the value of attribute memoria.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#saida ⇒ Object
readonly
Returns the value of attribute saida.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Attributes inherited from Pipeline::Base
Instance Method Summary collapse
- #converter! ⇒ Object
-
#initialize(fonte, template, options = {}) ⇒ Conversor
constructor
A new instance of Conversor.
- #to_sql ⇒ Object
Methods inherited from Pipeline::Base
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, = {}) @fonte = fonte @template = template.is_a?(Symbol) ? Mapa.arquivo_template(template) : template @saida = [] @memoria = Hash.new { |k, v| k[v] = [] } = valida_arquivo(@fonte) valida_arquivo(@template) tasks = if [:tasks].is_a?(Array) [:tasks] elsif [:tasks] == :vazio [] else [Pipeline::NormalizaSQL, Pipeline::AddHash] end super(tasks) end |
Instance Attribute Details
#fonte ⇒ Object (readonly)
Returns the value of attribute fonte.
6 7 8 |
# File 'lib/sped2sql/conversor.rb', line 6 def fonte @fonte end |
#memoria ⇒ Object (readonly)
Returns the value of attribute memoria.
6 7 8 |
# File 'lib/sped2sql/conversor.rb', line 6 def memoria @memoria end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/sped2sql/conversor.rb', line 6 def end |
#saida ⇒ Object (readonly)
Returns the value of attribute saida.
6 7 8 |
# File 'lib/sped2sql/conversor.rb', line 6 def saida @saida end |
#template ⇒ Object (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: }) @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_sql ⇒ Object
58 59 60 |
# File 'lib/sped2sql/conversor.rb', line 58 def to_sql SQL::Parser.to_sql(@saida, [:db] || {}) end |