Class: Cnab240::Arquivo::Arquivo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versao = 'V86') ⇒ Arquivo

Returns a new instance of Arquivo.



8
9
10
11
12
13
# File 'lib/cnab240/arquivo/arquivo.rb', line 8

def initialize(versao = 'V86')
  @versao = versao
  @header = eval("Cnab240::#{versao}::Arquivo::Header").new
  @trailer = eval("Cnab240::#{versao}::Arquivo::Trailer").new
  @lotes = []
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/cnab240/arquivo/arquivo.rb', line 3

def header
  @header
end

#lotesObject

Returns the value of attribute lotes.



4
5
6
# File 'lib/cnab240/arquivo/arquivo.rb', line 4

def lotes
  @lotes
end

#trailerObject

Returns the value of attribute trailer.



5
6
7
# File 'lib/cnab240/arquivo/arquivo.rb', line 5

def trailer
  @trailer
end

#versaoObject

Returns the value of attribute versao.



6
7
8
# File 'lib/cnab240/arquivo/arquivo.rb', line 6

def versao
  @versao
end

Class Method Details

.load(io) ⇒ Array<Cnab240::Arquivo::Arquivo>

Loads contents from an object that implements the IO interface into Cnab240::Arquivo::Arquivo instances.

Examples:

Load contents from a file.

Cnab240::Arquivo::Arquivo.load_from_file(File.open('/tmp/cnab240.txt', 'r'))

Parameters:

  • file (IO)

    The I/O interface from which the contents will be read.

Returns:

Since:

  • 0.0.20



75
76
77
# File 'lib/cnab240/arquivo/arquivo.rb', line 75

def self.load(io)
  Cnab240::Builder.new(io).arquivos
end

.load_from_file(filename) ⇒ Array<Cnab240::Arquivo::Arquivo>

Loads contents from a File into Cnab240::Arquivo::Arquivo instances.

Examples:

Load contents from a file.

Cnab240::Arquivo::Arquivo.load_from_file('/tmp/cnab240.txt')

Parameters:

  • filename (String)

    The file name from which the contents will be read.

Returns:

Since:

  • 0.0.20



61
62
63
# File 'lib/cnab240/arquivo/arquivo.rb', line 61

def self.load_from_file(filename)
  load(File.open(filename, 'r'))
end

Instance Method Details

#<<(lote) ⇒ Object



28
29
30
31
# File 'lib/cnab240/arquivo/arquivo.rb', line 28

def <<(lote)
  lote.arquivo = self
  @lotes << lote
end

#auto_fillObject



79
80
81
82
83
84
85
# File 'lib/cnab240/arquivo/arquivo.rb', line 79

def auto_fill
  trailer.totais_qtde_lotes = lotes.length.to_s
  qtd_registros = lotes.length * 2 # header e trailer de cada lote
  lotes.each { |l| qtd_registros += l.segmentos.length } # segmentos
  qtd_registros += 2 # header e trailer de arquivo
  trailer.totais_qtde_registros = qtd_registros.to_s
end

#linhasObject



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

def linhas
  auto_fill if Cnab240.auto_fill_enabled
  a = []
  a << @header.linha
  @lotes.each do |lote|
    lote.linhas.each do |linha|
      a << linha
    end
  end
  a << @trailer.linha
  a
end

#save_to_file(filename) ⇒ Integer

Writes Cnab240::Arquivo::Arquivo contents into a File, overwriting its current contents.

Examples:

Save contents to a file.

Cnab240::Arquivo::Arquivo.new.save_to_file('/tmp/cnab240.txt')

Parameters:

  • filename (String)

    The file name to which the contents will be saved to.

Returns:

  • (Integer)

    The number of bytes written to the file.

Since:

  • 0.0.20



47
48
49
# File 'lib/cnab240/arquivo/arquivo.rb', line 47

def save_to_file(filename)
  File.open(filename, 'w') { |f| f.write(string) }
end

#stringObject



33
34
35
# File 'lib/cnab240/arquivo/arquivo.rb', line 33

def string
  linhas.join("\n") << "\n"
end