Class: Cure::Extract::Extractor

Inherits:
Object
  • Object
show all
Includes:
Configuration, Database, Helpers::FileHelpers, Helpers::PerfHelpers, Log
Defined in:
lib/cure/extract/extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::PerfHelpers

#print_memory_usage, #print_time_spent

Methods included from Helpers::FileHelpers

#clean_dir, #open_file, #read_file, #with_file, #with_temp_dir

Methods included from Configuration

#config, #create_config, #register_config

Methods included from Database

#database_service, #init_database

Methods included from Log

#log_debug, #log_error, #log_info, #log_trace, #log_warn

Constructor Details

#initialize(opts) ⇒ Extractor

Returns a new instance of Extractor.

Parameters:

  • opts (Hash)


28
29
30
# File 'lib/cure/extract/extractor.rb', line 28

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#optsHash (readonly)

Returns opts.

Returns:

  • (Hash)

    opts



25
26
27
# File 'lib/cure/extract/extractor.rb', line 25

def opts
  @opts
end

Instance Method Details

#parse_csv(file, ref_name:) ⇒ Object

Parameters:

  • file (Pathname, String)
    • location of file

  • ref_name (String)
    • name of reference file



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/cure/extract/extractor.rb', line 34

def parse_csv(file, ref_name:)
  nr_processor = named_range_processor(ref_name: ref_name)
  v_processor = variable_processor(ref_name: ref_name)

  sample_rows = config.template.extraction.sample_rows
  row_count = 0

  database_service.with_transaction do
    CSV.foreach(file, liberal_parsing: true) do |row|
      next if sample_rows && row_count >= sample_rows

      nr_processor.process_row(row_count, row)
      v_processor.process_row(row_count, row)
      row_count += 1

      log_info "#{row_count} rows processed [#{Time.now}]" if (row_count % 1_000).zero?
    end

    nr_processor.after_process
  end

  log_info "[#{row_count}] total rows parsed from CSV"
end