Skip to content

Commit

Permalink
Changes use of global $; to class var Differ.separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Schubert Erlandsson committed Jun 3, 2013
1 parent e82551f commit 30eaaca
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require 'differ/string'
@diff = (@current - @original) # implicitly by line!
# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}"

$; = ' '
Differ.separator = ' '
@diff = (@current - @original)
# => "Epic {"lolcat" >> "wolfman"} fail!"
```
Expand Down
21 changes: 15 additions & 6 deletions lib/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@
module Differ
class << self

def diff(target, source, separator = "\n")
old_sep, $; = $;, separator
def separator=(separator)
@@separator = separator
end

def separator
@@separator
end

def diff(target, source, new_sep = "\n")
old_sep = self.separator
self.separator = new_sep

target = target.split(separator)
source = source.split(separator)
target = target.split(new_sep)
source = source.split(new_sep)

$; = '' if separator.is_a? Regexp
self.separator = '' if new_sep.is_a? Regexp

@diff = Diff.new
advance(target, source) until source.empty? || target.empty?
@diff.insert(*target) || @diff.delete(*source)
return @diff
ensure
$; = old_sep
self.separator = old_sep
end

def diff_by_char(to, from)
Expand Down
2 changes: 1 addition & 1 deletion lib/differ/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def raw_array

private
def sep
"#{$;}"
"#{Differ.separator}"
end
end
end
2 changes: 1 addition & 1 deletion lib/differ/string.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Differ
module StringDiffer
def diff(old)
Differ.diff(self, old, $; || "\n")
Differ.diff(self, old, Differ.separator || "\n")
end
alias :- :diff
end
Expand Down
70 changes: 35 additions & 35 deletions spec/differ/diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Differ::Diff do
before(:each) do
$; = nil
Differ.separator = nil
@diff = Differ::Diff.new
end

Expand All @@ -15,8 +15,8 @@
diff('a', 'b', 'c').to_s.should == 'abc'
end

it 'should concatenate without regard for the $;' do
$; = '*'
it 'should concatenate without regard for the Differ.separator' do
Differ.separator = '*'
diff('a', 'b', 'c').to_s.should == 'abc'
end

Expand Down Expand Up @@ -56,8 +56,8 @@
@diff.should == diff('abcd')
end

it 'should join its arguments with $;' do
$; = '*'
it 'should join its arguments with Differ.separator' do
Differ.separator = '*'
@diff.same(*'a*b*c*d'.split)
@diff.should == diff('a*b*c*d')
end
Expand All @@ -72,8 +72,8 @@
@diff.should == diff('ab')
end

it 'should join to the last result with $;' do
$; = '*'
it 'should join to the last result with Differ.separator' do
Differ.separator = '*'
@diff.same('b')
@diff.should == diff('a*b')
end
Expand All @@ -89,15 +89,15 @@
@diff.should == diff(('z' >> 'd'), 'a')
end

it 'should prepend $; to the result' do
$; = '*'
it 'should prepend Differ.separator to the result' do
Differ.separator = '*'
@diff.same('a')
@diff.should == diff(('z' >> 'd'), '*a')
end

it "should do nothing to a leading $; on the insert" do
it "should do nothing to a leading Differ.separator on the insert" do
@diff = diff('a', ('*-' >> '*+'))
$; = '*'
Differ.separator = '*'
@diff.same('c')
@diff.should == diff('a', ('*-' >> '*+'), '*c')
end
Expand All @@ -113,15 +113,15 @@
@diff.should == diff(-'z', 'a')
end

it 'should append $; to the previous result' do
$; = '*'
it 'should append Differ.separator to the previous result' do
Differ.separator = '*'
@diff.same('a')
@diff.should == diff(-'z*', 'a')
end

it "should relocate a leading $; on the delete to the previous item" do
it "should relocate a leading Differ.separator on the delete to the previous item" do
@diff = diff('a', -'*b')
$; = '*'
Differ.separator = '*'
@diff.same('c')
@diff.should == diff('a*', -'b*', 'c')
end
Expand All @@ -137,15 +137,15 @@
@diff.should == diff(+'z', 'a')
end

it 'should append $; to the previous result' do
$; = '*'
it 'should append Differ.separator to the previous result' do
Differ.separator = '*'
@diff.same('a')
@diff.should == diff(+'z*', 'a')
end

it "should relocate a leading $; on the insert to the previous item" do
it "should relocate a leading Differ.separator on the insert to the previous item" do
@diff = diff('a', +'*b')
$; = '*'
Differ.separator = '*'
@diff.same('c')
@diff.should == diff('a*', +'b*', 'c')
end
Expand All @@ -163,8 +163,8 @@
@diff.should == diff(-'abcd')
end

it 'should join its arguments with $;' do
$; = '*'
it 'should join its arguments with Differ.separator' do
Differ.separator = '*'
@diff.delete(*'a*b*c*d'.split)
@diff.should == diff(-'a*b*c*d')
end
Expand All @@ -180,8 +180,8 @@
@diff.should == diff(-'ab')
end

it 'should join to the last result with $;' do
$; = '*'
it 'should join to the last result with Differ.separator' do
Differ.separator = '*'
@diff.delete('b')
@diff.should == diff(-'a*b')
end
Expand All @@ -197,9 +197,9 @@
@diff.should == diff('b' >> 'a')
end

it "should relocate a leading $; on the insert to the previous item" do
it "should relocate a leading Differ.separator on the insert to the previous item" do
@diff = diff('a', +'*b')
$; = '*'
Differ.separator = '*'
@diff.delete('z')
@diff.should == diff('a*', ('z' >> 'b'))
end
Expand All @@ -216,8 +216,8 @@
@diff.should == diff('a', -'b')
end

it 'should prepend $; to the result' do
$; = '*'
it 'should prepend Differ.separator to the result' do
Differ.separator = '*'
@diff.delete('b')
@diff.should == diff('a', -'*b')
end
Expand All @@ -235,8 +235,8 @@
@diff.should == diff(+'abcd')
end

it 'should join its arguments with $;' do
$; = '*'
it 'should join its arguments with Differ.separator' do
Differ.separator = '*'
@diff.insert(*'a*b*c*d'.split)
@diff.should == diff(+'a*b*c*d')
end
Expand All @@ -252,9 +252,9 @@
@diff.should == diff('b' >> 'a')
end

it "should relocate a leading $; on the delete to the previous item" do
it "should relocate a leading Differ.separator on the delete to the previous item" do
@diff = diff('a', -'*b')
$; = '*'
Differ.separator = '*'
@diff.insert('z')
@diff.should == diff('a*', ('b' >> 'z'))
end
Expand All @@ -270,8 +270,8 @@
@diff.should == diff(+'ab')
end

it 'should join to the last result with $;' do
$; = '*'
it 'should join to the last result with Differ.separator' do
Differ.separator = '*'
@diff.insert('b')
@diff.should == diff(+'a*b')
end
Expand All @@ -288,8 +288,8 @@
@diff.should == diff('a', +'b')
end

it 'should prepend $; to the result' do
$; = '*'
it 'should prepend Differ.separator to the result' do
Differ.separator = '*'
@diff.insert('b')
@diff.should == diff('a', +'*b')
end
Expand Down
14 changes: 7 additions & 7 deletions spec/differ/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

before(:each) do
$; = nil
Differ.separator = nil
end

describe '#diff' do
Expand All @@ -16,9 +16,9 @@
'TO'.diff('FROM')
end

it 'should call Differ#diff with $;' do
$; = 'x'
Differ.should_receive(:diff).with('TO', 'FROM', $;).once
it 'should call Differ#diff with Differ.separator' do
Differ.separator = 'x'
Differ.should_receive(:diff).with('TO', 'FROM', Differ.separator).once
'TO'.diff('FROM')
end
end
Expand All @@ -29,9 +29,9 @@
'TO' - 'FROM'
end

it 'should call Differ#diff with $;' do
$; = 'x'
Differ.should_receive(:diff).with('TO', 'FROM', $;).once
it 'should call Differ#diff with Differ.separator' do
Differ.separator = 'x'
Differ.should_receive(:diff).with('TO', 'FROM', Differ.separator).once
'TO' - 'FROM'
end
end
Expand Down

0 comments on commit 30eaaca

Please sign in to comment.