Class: FileSplit::FileSplitter
- Inherits:
-
Object
- Object
- FileSplit::FileSplitter
- Defined in:
- lib/file_split/file_splitter.rb
Overview
The Main File Splitter Class
Instance Method Summary collapse
-
#start ⇒ Object
Process File Splitting.
Instance Method Details
#start ⇒ Object
Process File Splitting
Example
>> FileSplitter.start
=> void
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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/file_split/file_splitter.rb', line 13 def start row_count = 0 file_count = 1 headers = [] = [] out = false reader = File.open config[:source], "r" reader.each_line { |line| break if config[:maxlines] > 0 && reader.lineno == config[:maxlines] if row_count == 0 out = File.new(target_name(file_count), "w+") headers.each { |header| out.puts header } end if config[:header] > 0 && reader.lineno <= config[:header] headers.push line end out.puts line row_count += 1 if config[:footer] > 0 .unshift line .delete_at config[:footer] if .length == config[:footer] end if row_count == config[:lines] row_count = 0 file_count += 1 out.close end } reader.close if row_count <= .length File.delete target_name(file_count) file_count -= 1 File.open(target_name(file_count), "a+") do |last_file| [row_count...length].each do |i| last_file.puts [i] end end end if .length > 0 (file_count-1).times do |file_no| out = File.open(target_name(file_no), "a+") .each { || out.puts } out.close end end end |