class Cnab400

Constants

SECTION_TYPES

Attributes

operacao[RW]
versao[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 24
def initialize(params = {})
  @sections = []
  @versao = params[:versao] || "V08"
  @operacao = params[:operacao]

  if params[:file] && params[:file].size > 0
    self.process_file params[:file]
  end
end

Public Instance Methods

add_clients(params = {}) click to toggle source

Seção Client

# File lib/formatos/cnab400/cnab_400.rb, line 76
def add_clients(params = {})

  self.valida_existe_header

  section = self.get_new_section(:client)

  section.set_values params
  self.add_section_from_business section
end
create_header(params = {}) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 61
def create_header(params = {})

  if self.get_header.nil?
    section = self.get_new_section(:header)

    section.set_values params

    self.add_section_from_business section

  else
    raise "Header já declarado!"
  end
end
create_trailler(params = {}) click to toggle source

Seção Trailler

# File lib/formatos/cnab400/cnab_400.rb, line 87
def create_trailler(params = {})
  self.valida_existe_header

  if self.get_trailler.nil?
    if self.get_section(SECTION_TYPES[:client]).length > 0
      section = self.get_new_section(:trailler)

      section.set_values params

      section.set_sequencial          self.sections.length + 1

      self.sections << section
    else
      raise "Nenhum valor declarado!"
    end
  else
    raise "Trailler já declarado!"
  end
end
get_header() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 139
def get_header
  self.get_section(SECTION_TYPES[:header]).first()
end
get_new_section(section_type) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 147
def get_new_section section_type
  case section_type
    when :header
      eval("Cnab400::#{@versao}::#{@operacao}::Header").new
    when :client
      eval("Cnab400::#{@versao}::#{@operacao}::Detalhe").new
    when :trailler
      eval("Cnab400::#{@versao}::#{@operacao}::Trailler").new
  end
end
get_section(section) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 127
def get_section section
  self.sections.select {|c| c.is_section?(section) == true }
end
get_trailler() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 143
def get_trailler
  self.get_section(SECTION_TYPES[:trailler]).first()
end
has_section(section) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 123
def has_section section
  self.get_section(section).length > 0
end
is_valid?() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 131
def is_valid?
  self.sections.select {|b| b.is_valid? == false }.length == 0
end
sections() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 119
def sections
  @sections.to_a
end
to_s() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 135
def to_s
  self.sections.map{|a| a.to_s}.join("\r\n")
end
valida_existe_header() click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 111
def valida_existe_header
  raise "Header ainda não declarado" if self.get_header.nil?
end

Protected Instance Methods

add_section(section) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 38
def add_section section

  if section.is_valid?
    self.sections << section
  else
    raise "Seção #{section.get_id} está inválida:
              #{section.errors.inspect}
          #{section.to_s}"
  end

end
add_section_from_business(section) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 50
def add_section_from_business section
  self.add_section section
  self
end
process_file(location) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 162
def process_file location
  file = File.new(location, "r")

  while (line = file.gets)
    process_string line
  end

  file.close
end
process_string(file) click to toggle source
# File lib/formatos/cnab400/cnab_400.rb, line 172
def process_string file
  section_type = file[0]

  section = get_new_section SECTION_TYPES.key(section_type)
  section.process_section(file)

  self.add_section section
end