Class: Sycsvpro::Extractor
- Inherits:
-
Object
- Object
- Sycsvpro::Extractor
- Defined in:
- lib/sycsvpro/extractor.rb
Overview
Extracts rows and columns from a csv file
Instance Attribute Summary collapse
-
#col_filter ⇒ Object
readonly
filter that is used for columns.
-
#in_file ⇒ Object
readonly
infile contains the data that is operated on.
-
#out_file ⇒ Object
readonly
outfile is the file where the result is written to.
-
#row_filter ⇒ Object
readonly
filter that is used for rows.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the extractor.
-
#initialize(options = {}) ⇒ Extractor
constructor
Creates a new extractor.
Constructor Details
#initialize(options = {}) ⇒ Extractor
Creates a new extractor
20 21 22 23 24 25 |
# File 'lib/sycsvpro/extractor.rb', line 20 def initialize(={}) @in_file = [:infile] @out_file = [:outfile] @row_filter = RowFilter.new([:rows], df: [:df]) @col_filter = ColumnFilter.new([:cols], df: [:df]) end |
Instance Attribute Details
#col_filter ⇒ Object (readonly)
filter that is used for columns
17 18 19 |
# File 'lib/sycsvpro/extractor.rb', line 17 def col_filter @col_filter end |
#in_file ⇒ Object (readonly)
infile contains the data that is operated on
11 12 13 |
# File 'lib/sycsvpro/extractor.rb', line 11 def in_file @in_file end |
#out_file ⇒ Object (readonly)
outfile is the file where the result is written to
13 14 15 |
# File 'lib/sycsvpro/extractor.rb', line 13 def out_file @out_file end |
#row_filter ⇒ Object (readonly)
filter that is used for rows
15 16 17 |
# File 'lib/sycsvpro/extractor.rb', line 15 def row_filter @row_filter end |
Instance Method Details
#execute ⇒ Object
Executes the extractor
28 29 30 31 32 33 34 35 |
# File 'lib/sycsvpro/extractor.rb', line 28 def execute File.open(out_file, 'w') do |o| File.new(in_file, 'r').each_with_index do |line, index| extraction = col_filter.process(row_filter.process(line.chomp, row: index)) o.puts extraction unless extraction.nil? end end end |