Class: CsvPiper::Piper

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_piper/piper.rb

Overview

Catch exceptions on a per processor/row basis (allow other rows to continue processing)?

Constant Summary collapse

HEADER_LINE_INDEX =
1
FIRST_DATA_LINE_INDEX =
2
CSV_HEADER_OPTIONS =
{ headers: true, return_headers: true, skip_blanks: true, skip_lines: /^(\s*,)*$/ }

Instance Method Summary collapse

Constructor Details

#initialize(io_stream:, pre_processors: [], processors: [], csv_options: {}, required_headers: []) ⇒ Piper

Returns a new instance of Piper.



10
11
12
13
14
15
16
17
# File 'lib/csv_piper/piper.rb', line 10

def initialize(io_stream:, pre_processors: [], processors: [], csv_options: {}, required_headers: [])
  @pre_processors = pre_processors
  @processors = processors
  @required_headers = required_headers
  @csv_options = csv_options.merge(CSV_HEADER_OPTIONS)
  @csv_options = @csv_options.merge(skip_lines: "^(\s*#{@csv_options[:col_sep]})*$") if @csv_options[:col_sep]
  @io = io_stream
end

Instance Method Details

#has_required_headers?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/csv_piper/piper.rb', line 19

def has_required_headers?
  missing_headers.empty?
end

#missing_headersObject



23
24
25
26
# File 'lib/csv_piper/piper.rb', line 23

def missing_headers
  headers = csv.headers
  required_headers.reject { |header| headers.include?(header) }
end

#processObject



28
29
30
31
32
33
34
35
# File 'lib/csv_piper/piper.rb', line 28

def process
  validate_process_configuration!
  validate_headers!

  process_csv_body

  self
end