Class: Cnab240::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/cnab240/arquivo/builder.rb

Constant Summary collapse

HEADER_ARQUIVO =
'0'
HEADER_LOTE =
'1'
INICIAIS_LOTE =
'2'
DETALHE =
'3'
FINAIS_LOTE =
'4'
TRAILER_LOTE =
'5'
TRAILER_ARQUIVO =
'9'
RANGE_TIPO_REGISTRO =
7..7
RANGE_LAYOUT_ARQUIVO =
163..165
RANGE_HEADER_LOTE =
13..15
RANGE_TIPO_OPERACAO_DETALHE =
13..13

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ Builder

Returns a new instance of Builder.



19
20
21
# File 'lib/cnab240/arquivo/builder.rb', line 19

def initialize(io = nil)
  load(io) unless io.nil?
end

Instance Attribute Details

#arquivosObject

Returns the value of attribute arquivos.



17
18
19
# File 'lib/cnab240/arquivo/builder.rb', line 17

def arquivos
  @arquivos
end

Instance Method Details

#load(io) ⇒ Object



23
24
25
26
27
28
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/cnab240/arquivo/builder.rb', line 23

def load(io)
  @arquivos = []
  line_number = 0
  io.each_line do |line|
    check_line_encoding line, ++line_number
    case line[RANGE_TIPO_REGISTRO]

      when HEADER_ARQUIVO
        find_header_arquivo line, line_number

      when HEADER_LOTE
        find_header_lote line, line_number

      when INICIAIS_LOTE
        raise NotImplementedError.new("Tipo de registro nao suportado")

      when DETALHE
        find_segmento line, line_number

      when FINAIS_LOTE
        raise NotImplementedError.new("Tipo de registro not implemented")

      when TRAILER_LOTE
        arquivos.last.lotes.last.trailer.read(line)

      when TRAILER_ARQUIVO
        arquivos.last.trailer.read(line)

      else
        raise "Invalid tipo de registro: #{line[RANGE_TIPO_REGISTRO]} at line #{line_number} \n\t Line: [#{line}]"
    end
  end
  arquivos
end