Class: Sycsvpro::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/sycsvpro/extractor.rb

Overview

Extracts rows and columns from a csv file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Extractor

Creates a new extractor



20
21
22
23
24
25
# File 'lib/sycsvpro/extractor.rb', line 20

def initialize(options={})
  @in_file  = options[:infile]
  @out_file = options[:outfile]
  @row_filter = RowFilter.new(options[:rows], df: options[:df])
  @col_filter = ColumnFilter.new(options[:cols], df: options[:df])
end

Instance Attribute Details

#col_filterObject (readonly)

filter that is used for columns



17
18
19
# File 'lib/sycsvpro/extractor.rb', line 17

def col_filter
  @col_filter
end

#in_fileObject (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_fileObject (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_filterObject (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

#executeObject

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