Class: Csvtool::Application::UseCases::RunRowExtraction
- Inherits:
-
Object
- Object
- Csvtool::Application::UseCases::RunRowExtraction
- Defined in:
- lib/csvtool/application/use_cases/run_row_extraction.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #extract(session:, headers:, on_row: nil) ⇒ Object
-
#initialize(header_reader: Infrastructure::CSV::HeaderReader.new, row_streamer: Infrastructure::CSV::RowStreamer.new, csv_row_file_writer: nil) ⇒ RunRowExtraction
constructor
A new instance of RunRowExtraction.
- #read_headers(file_path:, col_sep:) ⇒ Object
Constructor Details
#initialize(header_reader: Infrastructure::CSV::HeaderReader.new, row_streamer: Infrastructure::CSV::RowStreamer.new, csv_row_file_writer: nil) ⇒ RunRowExtraction
Returns a new instance of RunRowExtraction.
18 19 20 21 22 23 24 25 26 |
# File 'lib/csvtool/application/use_cases/run_row_extraction.rb', line 18 def initialize( header_reader: Infrastructure::CSV::HeaderReader.new, row_streamer: Infrastructure::CSV::RowStreamer.new, csv_row_file_writer: nil ) @header_reader = header_reader @row_streamer = row_streamer @csv_row_file_writer = csv_row_file_writer || Infrastructure::Output::CsvRowFileWriter.new(row_streamer: @row_streamer) end |
Instance Method Details
#extract(session:, headers:, on_row: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/csvtool/application/use_cases/run_row_extraction.rb', line 41 def extract(session:, headers:, on_row: nil) if session.output_destination.file? stats = @csv_row_file_writer.call( output_path: session.output_destination.path, file_path: session.source.path, col_sep: session.source.separator, headers: headers, start_row: session.row_range.start_row, end_row: session.row_range.end_row ) success(stats.merge(output_path: session.output_destination.path)) else stats = @row_streamer.each_in_range( file_path: session.source.path, col_sep: session.source.separator, start_row: session.row_range.start_row, end_row: session.row_range.end_row ) { |fields| on_row.call(fields) if on_row } success(stats) end rescue CSV::MalformedCSVError failure(:could_not_parse_csv) rescue Errno::EACCES, Errno::ENOENT => e if session.output_destination.file? failure(:cannot_write_output_file, path: session.output_destination.path, error_class: e.class) else failure(:cannot_read_file, path: session.source.path) end end |
#read_headers(file_path:, col_sep:) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/csvtool/application/use_cases/run_row_extraction.rb', line 28 def read_headers(file_path:, col_sep:) return failure(:file_not_found, path: file_path) unless File.file?(file_path) headers = @header_reader.call(file_path: file_path, col_sep: col_sep) return failure(:no_headers) if headers.empty? success(headers: headers) rescue CSV::MalformedCSVError failure(:could_not_parse_csv) rescue Errno::EACCES failure(:cannot_read_file, path: file_path) end |