Skip to content

Commit

Permalink
extracted semi colon troll
Browse files Browse the repository at this point in the history
  • Loading branch information
nolman committed Sep 17, 2011
1 parent 629eddf commit 4a1677b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 52 deletions.
1 change: 1 addition & 0 deletions lib/troll.rb
@@ -1,2 +1,3 @@
require 'troll/semi_colon_troll'
require 'troll/collector'
require 'troll/troller'
24 changes: 24 additions & 0 deletions lib/troll/semi_colon_troll.rb
@@ -0,0 +1,24 @@
module Troll
class SemiColonTroll
def troll_string(str)
result = ""
str.each_line { |line| result += troll_line(line) }
result
end

private
def troll_line(line)
case line
when /^\s+$/
line
when /^.*;\s*$/
line
when /^.*\n$/
"#{line.chomp};\n"
else
"#{line};"
end
end

end
end
25 changes: 5 additions & 20 deletions lib/troll/troller.rb
Expand Up @@ -4,38 +4,23 @@ class Troller

def initialize(file_path=nil)
self.file = file_path
@trolls = [SemiColonTroll.new]
end

def troll
contents = File.read(self.file)
self.result = troll_string(contents)
@trolls.each do |troll|
contents = troll.troll_string(contents)
end
self.result = contents
end

def troll!
troll
write_file
end

def troll_string(str)
result = ""
str.each_line { |line| result += troll_line(line) }
result
end

private
def troll_line(line)
case line
when /^\s+$/
line
when /^.*;\s*$/
line
when /^.*\n$/
"#{line.chomp};\n"
else
"#{line};"
end
end

def write_file
File.open(output_file, 'w') do |f|
f.puts self.result
Expand Down
36 changes: 36 additions & 0 deletions spec/semi_colon_troll_spec.rb
@@ -0,0 +1,36 @@
require 'spec_helper'
require 'troll'

describe Troll::SemiColonTroll do
describe "#troll_string" do
it "appends a semicolon to a single-line string" do
Troll::SemiColonTroll.new.troll_string("foo").should eql("foo;")
end

it "does not append a semicolon to a single-line string ending in a semi-colon" do
Troll::SemiColonTroll.new.troll_string("foo;").should eql("foo;")
end

it "appends a semicolon to each line of a multi-line string" do
multi_line_string = <<-EOS
class Xzibit
def yo_dawg_i_heard_you_like(&procs)
Proc.new { procs.call }
end
end
EOS
semi_coloned_multi_line_string = <<-EOS
class Xzibit;
def yo_dawg_i_heard_you_like(&procs);
Proc.new { procs.call };
end;
end;
EOS
Troll::SemiColonTroll.new.troll_string(multi_line_string).should eql(semi_coloned_multi_line_string)
end
end
end
32 changes: 0 additions & 32 deletions spec/troller_spec.rb
Expand Up @@ -28,36 +28,4 @@
original.should eql(trolled)
end
end

describe "#troll_string" do
it "appends a semicolon to a single-line string" do
Troll::Troller.new.troll_string("foo").should eql("foo;")
end

it "does not append a semicolon to a single-line string ending in a semi-colon" do
Troll::Troller.new.troll_string("foo;").should eql("foo;")
end

it "appends a semicolon to each line of a multi-line string" do
multi_line_string = <<-EOS
class Xzibit
def yo_dawg_i_heard_you_like(&procs)
Proc.new { procs.call }
end
end
EOS
semi_coloned_multi_line_string = <<-EOS
class Xzibit;
def yo_dawg_i_heard_you_like(&procs);
Proc.new { procs.call };
end;
end;
EOS
Troll::Troller.new.troll_string(multi_line_string).should eql(semi_coloned_multi_line_string)
end
end
end

0 comments on commit 4a1677b

Please sign in to comment.