Skip to content

Commit

Permalink
create Splitter class and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
gipcompany committed Sep 10, 2016
1 parent fb423f9 commit 40a6a4e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 4 deletions.
50 changes: 50 additions & 0 deletions lib/file_manipulator/splitter.rb
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
93 changes: 93 additions & 0 deletions spec/file_manipulator/splitter_spec.rb
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
9 changes: 9 additions & 0 deletions spec/file_manipulator/version_spec.rb
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
4 changes: 0 additions & 4 deletions spec/file_manipulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@
it "has a version number" do
expect(FileManipulator::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
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'
Empty file added tmp/.keep
Empty file.

0 comments on commit 40a6a4e

Please sign in to comment.