Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extracted semi colon troll

  • Loading branch information...
commit 4a1677b846241facf0770d0d08e1166a6655ae2e 1 parent 629eddf
@nolman nolman authored
View
1  lib/troll.rb
@@ -1,2 +1,3 @@
+require 'troll/semi_colon_troll'
require 'troll/collector'
require 'troll/troller'
View
24 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
View
25 lib/troll/troller.rb
@@ -4,11 +4,15 @@ 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!
@@ -16,26 +20,7 @@ def 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
View
36 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
View
32 spec/troller_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.