lib/masticate/base.rb in masticate-0.2 vs lib/masticate/base.rb in masticate-0.2.1
- old
+ new
@@ -28,13 +28,19 @@
@input_count += 1
line && line.chomp
end
def emit(line)
+ @output_count ||= 0
@output_count += 1
begin
- @output.puts line
+ case line
+ when Array
+ @output.puts line.to_csv
+ else
+ @output.puts line
+ end
rescue Errno::EPIPE
# output was closed, e.g. ran piped into `head`
# silently ignore this condition, it's not fatal and doesn't need a warning
end
end
@@ -46,9 +52,25 @@
if opts[:col_sep]
@csv_options[:quote_char] = opts[:quote_char] || "\0"
end
end
- # def crunch(row)
- # # noop
- # end
+ def execute(opts)
+ configure(opts)
+
+ @output_count = 0
+ with_input do |input|
+ while line = get
+ row = CSV.parse_line(line, csv_options)
+ output = crunch(row)
+ emit(output) if output
+ end
+ end
+ crunch(nil) {|row| emit(row)}
+ @output.close if opts[:output]
+
+ {
+ :input_count => input_count,
+ :output_count => @output_count
+ }
+ end
end