Class: Cnab400

Inherits:
Object
  • Object
show all
Defined in:
lib/formatos/cnab400/cnab_400.rb

Constant Summary collapse

SECTION_TYPES =
{header: '0', client: "1", trailler: '9'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Cnab400



Construtor



24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#operacaoObject

Returns the value of attribute operacao.



6
7
8
# File 'lib/formatos/cnab400/cnab_400.rb', line 6

def operacao
  @operacao
end

#versaoObject

Returns the value of attribute versao.



6
7
8
# File 'lib/formatos/cnab400/cnab_400.rb', line 6

def versao
  @versao
end

Instance Method Details

#add_clients(params = {}) ⇒ Object

Seção Client



76
77
78
79
80
81
82
83
84
# 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 = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# 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 = {}) ⇒ Object

Seção Trailler



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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_headerObject



139
140
141
# 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) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# 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) ⇒ Object



127
128
129
# File 'lib/formatos/cnab400/cnab_400.rb', line 127

def get_section section
  self.sections.select {|c| c.is_section?(section) == true }
end

#get_traillerObject



143
144
145
# File 'lib/formatos/cnab400/cnab_400.rb', line 143

def get_trailler
  self.get_section(SECTION_TYPES[:trailler]).first()
end

#has_section(section) ⇒ Object



123
124
125
# File 'lib/formatos/cnab400/cnab_400.rb', line 123

def has_section section
  self.get_section(section).length > 0
end

#is_valid?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/formatos/cnab400/cnab_400.rb', line 131

def is_valid?
  self.sections.select {|b| b.is_valid? == false }.length == 0
end

#sectionsObject



119
120
121
# File 'lib/formatos/cnab400/cnab_400.rb', line 119

def sections
  @sections.to_a
end

#to_sObject



135
136
137
# 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_headerObject



Validações



111
112
113
# 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