Class: HistoricoAtivos::CarregaHistorico

Inherits:
Object
  • Object
show all
Defined in:
lib/historico_ativos/carrega_historico.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser_header, parser_trailer, parser_ativo) ⇒ CarregaHistorico

Returns a new instance of CarregaHistorico.



6
7
8
9
10
# File 'lib/historico_ativos/carrega_historico.rb', line 6

def initialize(parser_header, parser_trailer, parser_ativo)
  @parser_header = parser_header
  @parser_trailer = parser_trailer
  @parser_ativo = parser_ativo
end

Instance Attribute Details

#parser_ativoObject

Returns the value of attribute parser_ativo.



4
5
6
# File 'lib/historico_ativos/carrega_historico.rb', line 4

def parser_ativo
  @parser_ativo
end

#parser_headerObject

Returns the value of attribute parser_header.



4
5
6
# File 'lib/historico_ativos/carrega_historico.rb', line 4

def parser_header
  @parser_header
end

#parser_trailerObject

Returns the value of attribute parser_trailer.



4
5
6
# File 'lib/historico_ativos/carrega_historico.rb', line 4

def parser_trailer
  @parser_trailer
end

Instance Method Details

#load(filepath) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/historico_ativos/carrega_historico.rb', line 12

def load(filepath)
  return false unless File.exists?(filepath)
  
  file = File.open(filepath, "r")

  historico = Historico.new

  file.each { |line|
    historico.import_header @parser_header.parse(line) if line.start_with?("00")
    historico.ativos << @parser_ativo.parse(line) if line.start_with?("01")
    historico.import_trailer @parser_trailer.parse(line) if line.start_with?("99")
  }

  historico
end

#persist(historico) ⇒ Object



28
29
30
# File 'lib/historico_ativos/carrega_historico.rb', line 28

def persist(historico)
  historico.save
end