Class: ETL::Processor::EscapeCsvProcessor
Instance Attribute Summary (collapse)
-
- (Object) charcount
readonly
Returns the value of attribute charcount.
-
- (Object) filters
readonly
Returns the value of attribute filters.
-
- (Object) source_file
readonly
The file to load from.
-
- (Object) target_file
readonly
The file to write to.
-
- (Object) use_temp_file
readonly
whether to use a temporary file or not.
Instance Method Summary (collapse)
-
- (EscapeCsvProcessor) initialize(control, configuration)
constructor
Initialize the processor.
-
- (Object) process
Execute the processor.
Constructor Details
- (EscapeCsvProcessor) initialize(control, configuration)
Initialize the processor.
Configuration options:
-
:source_file: The file to load data from
-
:target_file: The file to write data to
-
:file: short-cut which will set the same value to both source_file and target_file
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 23 def initialize(control, configuration) super if configuration[:file] @use_temp_file = true configuration[:source_file] = configuration[:file] configuration[:target_file] = configuration[:file] + '.tmp' end path = Pathname.new(configuration[:source_file]) @source_file = path.absolute? ? path : Pathname.new(File.dirname(File.(configuration[:source_file]))) + path path = Pathname.new(configuration[:target_file]) @target_file = path.absolute? ? path : Pathname.new(File.dirname(File.(configuration[:target_file]))) + path @filters = configuration[:filters] || [{:replace => '\"', :result => '""'}] @charcount = configuration[:charcount] raise ControlError, "Source file must be specified" if @source_file.nil? raise ControlError, "Target file must be specified" if @target_file.nil? raise ControlError, "Source and target file cannot currently point to the same file" if @source_file == @target_file end |
Instance Attribute Details
- (Object) charcount (readonly)
Returns the value of attribute charcount
15 16 17 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 15 def charcount @charcount end |
- (Object) filters (readonly)
Returns the value of attribute filters
14 15 16 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 14 def filters @filters end |
- (Object) source_file (readonly)
The file to load from
8 9 10 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 8 def source_file @source_file end |
- (Object) target_file (readonly)
The file to write to
10 11 12 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 10 def target_file @target_file end |
- (Object) use_temp_file (readonly)
whether to use a temporary file or not
12 13 14 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 12 def use_temp_file @use_temp_file end |
Instance Method Details
- (Object) process
Execute the processor
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 70 71 72 73 74 |
# File 'lib/etl/processor/escape_csv_processor.rb', line 42 def process reader = File.open(@source_file, 'r') writer = File.open(@target_file, 'w') reader.each_line do |line| reading = line @filters.each do |filter| if (!filter[:replace].nil? && !filter[:result].nil?) result = reading.gsub(Regexp.new(filter[:replace]), filter[:result]) reading = result end end unless @filters.nil? @charcount.each do |count| if (!count[:char].nil? && !count[:count].nil?) c = reading.count count[:char] if c != count[:count] reading = nil end end end unless @charcount.nil? writer.write(reading) unless reading.nil? end reader.close writer.close if use_temp_file FileUtils.rm(source_file) FileUtils.mv(target_file,source_file) end end |