-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb423f9
commit 40a6a4e
Showing
6 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module FileManipulator | ||
class Splitter | ||
attr_accessor :file_name, :output_directory, :size | ||
|
||
def initialize(file_name, output_directory, size = 10_485_760) | ||
@file_name = file_name | ||
@output_directory = output_directory | ||
@size = size | ||
end | ||
|
||
def run | ||
index = 0 | ||
|
||
File.open(file_name, 'r') do |input| | ||
until input.eof? | ||
File.open(File.join(output_directory, output_file_name(index)), 'w') do |output| | ||
line = "" | ||
|
||
while output.size <= (size - line.length) && !input.eof? | ||
line = input.readline | ||
output << line | ||
end | ||
end | ||
|
||
index += 1 | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def basename | ||
File.basename(file_name, File.extname(file_name)) | ||
end | ||
|
||
def extname | ||
File.extname(file_name).delete('.') | ||
end | ||
|
||
def number_of_digits | ||
@number_of_digits ||= Math.log10(File.size(file_name).to_f / size).ceil + 1 | ||
end | ||
|
||
def output_file_name(index) | ||
output_file_name = sprintf("#{basename}_%0#{number_of_digits}d", index) | ||
|
||
"#{output_file_name}.#{extname}" unless extname == '' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require "spec_helper" | ||
|
||
module FileManipulator | ||
describe Splitter do | ||
# one line | ||
let(:line) { "123456789\n" } # 10 bytes | ||
|
||
# basename | ||
let(:basename) { "foo" } # 10 bytes | ||
|
||
# extname | ||
let(:extname) { "txt" } # 10 bytes | ||
|
||
# tempfile | ||
let(:tempfile) do | ||
name = File.join(Dir.pwd, 'tmp', "#{basename}.#{extname}") | ||
File.new(name, 'w') | ||
end | ||
|
||
# small_file | ||
let(:small_content) { line } # 10 bytes | ||
let(:small_file) do | ||
tempfile.write(small_content) | ||
tempfile.rewind | ||
tempfile | ||
end | ||
|
||
# large_file | ||
let(:large_content) { line * 1_000 } # 10,000 bytes | ||
let(:large_file) do | ||
tempfile.write(large_content) | ||
tempfile.rewind | ||
tempfile | ||
end | ||
|
||
# output_directory | ||
let(:output_directory) { File.join(Dir.pwd, 'tmp') } | ||
|
||
# splitters | ||
let(:splitter_small_1) { Splitter.new(small_file.path, output_directory, 1) } | ||
let(:splitter_small_2) { Splitter.new(small_file.path, output_directory, 10) } | ||
let(:splitter_large_1) { Splitter.new(large_file.path, output_directory, 10) } | ||
let(:splitter_large_2) { Splitter.new(large_file.path, output_directory, 100) } | ||
|
||
after do | ||
Dir::glob(File.join(output_directory, "*.#{extname}")).each do |_file| | ||
File.delete(_file) | ||
end | ||
end | ||
|
||
it '#new' do | ||
expect(splitter_small_2.file_name).to eq(small_file.path) | ||
expect(splitter_small_2.output_directory).to eq(output_directory) | ||
expect(splitter_small_2.size).to eq(10) | ||
end | ||
|
||
describe '#run' do | ||
it 'small_1' do | ||
splitter_small_1.run | ||
end | ||
|
||
it 'small_2' do | ||
splitter_small_2.run | ||
end | ||
|
||
it 'large_1' do | ||
splitter_large_1.run | ||
end | ||
|
||
it 'large_2' do | ||
splitter_large_2.run | ||
end | ||
end | ||
|
||
it '#basename' do | ||
expect(splitter_small_1.send(:basename)).to eq(basename) | ||
end | ||
|
||
it '#extname' do | ||
expect(splitter_small_1.send(:extname)).to eq(extname) | ||
end | ||
|
||
it '#number_of_digits' do | ||
expect(splitter_small_1.send(:number_of_digits)).to eq(2) | ||
expect(splitter_large_1.send(:number_of_digits)).to eq(4) | ||
end | ||
|
||
it '#output_file_name' do | ||
index = 1 | ||
expect(splitter_small_1.send(:output_file_name, index)).to eq("foo_0#{index}.txt") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "spec_helper" | ||
|
||
module FileManipulator | ||
describe VERSION do | ||
it "has a version number" do | ||
expect(VERSION).not_to be nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) | ||
require "file_manipulator" | ||
require 'pry' |