From 8413ebc06cff8108f7932e3a5626380b55070548 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:00:46 +0200 Subject: [PATCH 01/12] Converted README to markdown and added explicit syntax highlighting hints for code blocks --- README.rdoc => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.rdoc => README.md (100%) diff --git a/README.rdoc b/README.md similarity index 100% rename from README.rdoc rename to README.md From 438e44d60e8208f52e28b866456ab732f9ee1021 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:01:23 +0200 Subject: [PATCH 02/12] Removed empty config block to silence rspec deprication warning --- spec/spec_helper.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 87ac9b4..676ccab 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,10 +4,6 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'differ' -Spec::Runner.configure do |config| - -end - def diff(*parts) x = Differ::Diff.new x.instance_variable_set(:@raw, parts) From e82551fffc5587b58e80b5772cc04978431020b4 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:01:50 +0200 Subject: [PATCH 03/12] Converted README to markdown and added explicit syntax highlighting hints for code blocks --- README.md | 82 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 02ba355..191c573 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -= Differ +# Differ As streams of text swirled before the young man's eyes, his mind swam with thoughts of many things. They would have to wait, however, as he focussed his @@ -13,75 +13,93 @@ line scripts and web applications. The flexibility comes from the fact that diffs can be built at completely arbitrary levels of granularity (some common ones are built-in), and can be output in a variety of formats. -== Installation +## Installation - sudo gem install differ +```bash +sudo gem install differ +``` == Usage There are a number of ways to use Differ, depending on your situation and needs. - @original = "Epic lolcat fail!" - @current = "Epic wolfman fail!" +```ruby +@original = "Epic lolcat fail!" +@current = "Epic wolfman fail!" +``` You can call the Differ module directly. - require 'differ' +```ruby +require 'differ' +``` There are a number of built-in diff methods to choose from... - @diff = Differ.diff_by_line(@current, @original) - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +```ruby +@diff = Differ.diff_by_line(@current, @original) + # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" - @diff = Differ.diff_by_word(@current, @original) - # => "Epic {"lolcat" >> "wolfman"} fail!" +@diff = Differ.diff_by_word(@current, @original) + # => "Epic {"lolcat" >> "wolfman"} fail!" - @diff = Differ.diff_by_char(@current, @original) - # => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!" +@diff = Differ.diff_by_char(@current, @original) + # => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!" +``` ... or call #diff directly and supply your own boundary string! - @diff = Differ.diff(@current, @original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +```ruby +@diff = Differ.diff(@current, @original) # implicitly by line! + # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" - @diff = Differ.diff(@current, @original, 'i') - # => "Epi{"c lolcat fa" >> "c wolfman fa"}il" +@diff = Differ.diff(@current, @original, 'i') + # => "Epi{"c lolcat fa" >> "c wolfman fa"}il" +``` If you would like something a little more inline... - require 'differ/string' +```ruby +require 'differ/string' - @diff = @current.diff(@original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +@diff = @current.diff(@original) # implicitly by line! + # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +``` ... or a lot more inline... - @diff = (@current - @original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +```ruby +@diff = (@current - @original) # implicitly by line! + # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" - $; = ' ' - @diff = (@current - @original) - # => "Epic {"lolcat" >> "wolfman"} fail!" +$; = ' ' +@diff = (@current - @original) + # => "Epic {"lolcat" >> "wolfman"} fail!" +``` ... we've pretty much got you covered. -=== Output Formatting +### Output Formatting Need a different output format? We've got a few of those too. - Differ.format = :ascii # <- Default - Differ.format = :color - Differ.format = :html +```ruby +Differ.format = :ascii # <- Default +Differ.format = :color +Differ.format = :html - Differ.format = MyCustomFormatModule +Differ.format = MyCustomFormatModule +``` Don't want to change the system-wide default for only a single diff output? Yeah, me either. - @diff = (@current - @original) - @diff.format_as(:color) +```ruby +@diff = (@current - @original) +@diff.format_as(:color) +``` -== Copyright +## Copyright Copyright (c) 2009 Pieter Vande Bruggen. From 30eaaca93234eb37d6a1081afdf1e7e4e90d5771 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:32:52 +0200 Subject: [PATCH 04/12] Changes use of global $; to class var Differ.separator --- README.md | 2 +- lib/differ.rb | 21 ++++++++---- lib/differ/diff.rb | 2 +- lib/differ/string.rb | 2 +- spec/differ/diff_spec.rb | 70 +++++++++++++++++++------------------- spec/differ/string_spec.rb | 14 ++++---- 6 files changed, 60 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 191c573..738be23 100644 --- a/README.md +++ b/README.md @@ -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!" ``` diff --git a/lib/differ.rb b/lib/differ.rb index 63a4078..e25723f 100644 --- a/lib/differ.rb +++ b/lib/differ.rb @@ -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) diff --git a/lib/differ/diff.rb b/lib/differ/diff.rb index 76d3012..2db3e19 100644 --- a/lib/differ/diff.rb +++ b/lib/differ/diff.rb @@ -89,7 +89,7 @@ def raw_array private def sep - "#{$;}" + "#{Differ.separator}" end end end diff --git a/lib/differ/string.rb b/lib/differ/string.rb index f2cf075..1cba4f1 100644 --- a/lib/differ/string.rb +++ b/lib/differ/string.rb @@ -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 diff --git a/spec/differ/diff_spec.rb b/spec/differ/diff_spec.rb index be77486..ea225a0 100644 --- a/spec/differ/diff_spec.rb +++ b/spec/differ/diff_spec.rb @@ -2,7 +2,7 @@ describe Differ::Diff do before(:each) do - $; = nil + Differ.separator = nil @diff = Differ::Diff.new end @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/differ/string_spec.rb b/spec/differ/string_spec.rb index ffc8675..e4aa9a1 100644 --- a/spec/differ/string_spec.rb +++ b/spec/differ/string_spec.rb @@ -7,7 +7,7 @@ end before(:each) do - $; = nil + Differ.separator = nil end describe '#diff' do @@ -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 @@ -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 From 1001522651a2744eba0ecc9212fe72f1212d3c45 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:35:25 +0200 Subject: [PATCH 05/12] Fixes block quote in readme --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 738be23..67f9304 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,7 @@ # Differ - As streams of text swirled before the young man's eyes, his mind swam with - thoughts of many things. They would have to wait, however, as he focussed his - full concentration on the shifting patterns ahead of him. A glint of light - reflecting off a piece of buried code caught his eye and any hope he had was - lost. For the very moment he glanced aside, the landscape became Different. - The young man gave a small sigh and trudged onward in solemn resignation, - fated to wander the desolate codebanks in perpetuity. +> As streams of text swirled before the young man's eyes, his mind swam with thoughts of many things. They would have to wait, however, as he focussed his full concentration on the shifting patterns ahead of him. A glint of light reflecting off a piece of buried code caught his eye and any hope he had was lost. For the very moment he glanced aside, the landscape became Different. +> The young man gave a small sigh and trudged onward in solemn resignation, fated to wander the desolate codebanks in perpetuity. Differ is a flexible, pure-Ruby diff library, suitable for use in both command line scripts and web applications. The flexibility comes from the fact that From fde5a5c610ac1602b3092e83ed4ae3409ea26ba3 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:36:41 +0200 Subject: [PATCH 06/12] Fixes a headline and code comments in readme --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 67f9304..815ed87 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ones are built-in), and can be output in a variety of formats. sudo gem install differ ``` -== Usage +## Usage There are a number of ways to use Differ, depending on your situation and needs. @@ -33,23 +33,23 @@ There are a number of built-in diff methods to choose from... ```ruby @diff = Differ.diff_by_line(@current, @original) - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" @diff = Differ.diff_by_word(@current, @original) - # => "Epic {"lolcat" >> "wolfman"} fail!" +# => "Epic {"lolcat" >> "wolfman"} fail!" @diff = Differ.diff_by_char(@current, @original) - # => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!" +# => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!" ``` ... or call #diff directly and supply your own boundary string! ```ruby @diff = Differ.diff(@current, @original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" @diff = Differ.diff(@current, @original, 'i') - # => "Epi{"c lolcat fa" >> "c wolfman fa"}il" +# => "Epi{"c lolcat fa" >> "c wolfman fa"}il" ``` If you would like something a little more inline... @@ -58,18 +58,18 @@ If you would like something a little more inline... require 'differ/string' @diff = @current.diff(@original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" ``` ... or a lot more inline... ```ruby @diff = (@current - @original) # implicitly by line! - # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" Differ.separator = ' ' @diff = (@current - @original) - # => "Epic {"lolcat" >> "wolfman"} fail!" +# => "Epic {"lolcat" >> "wolfman"} fail!" ``` ... we've pretty much got you covered. From db44ea42601a5c3afe7f9db22d34e47da65379be Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 11:38:28 +0200 Subject: [PATCH 07/12] Adds bundler instructions to install in readme --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 815ed87..4670758 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,20 @@ ones are built-in), and can be output in a variety of formats. ## Installation +Add this to your gemfile if you use bundler + +```ruby +gem 'differ' +``` + +and bundle to install + +```bash +bundle install +``` + +or install it manually + ```bash sudo gem install differ ``` From 5501c33eda3005cb4ded0d4ea6fa30c9c2a38ab8 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 12:04:53 +0200 Subject: [PATCH 08/12] Adds usage example of regexp separator and edits format of readme --- README.md | 72 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 4670758..5a05142 100644 --- a/README.md +++ b/README.md @@ -28,67 +28,82 @@ or install it manually sudo gem install differ ``` -## Usage +## How do I use this thing? -There are a number of ways to use Differ, depending on your situation and needs. +There are a number of ways to use Differ, depending on your situation and needs. Lets examplify: ```ruby +require 'differ' + @original = "Epic lolcat fail!" @current = "Epic wolfman fail!" ``` -You can call the Differ module directly. - -```ruby -require 'differ' -``` - -There are a number of built-in diff methods to choose from... +There are a number of built-in diff_by_* methods to choose from for standard use: ```ruby @diff = Differ.diff_by_line(@current, @original) -# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => {"Epic lolcat fail!" >> "Epic wolfman fail!"} @diff = Differ.diff_by_word(@current, @original) -# => "Epic {"lolcat" >> "wolfman"} fail!" +# => Epic {"lolcat" >> "wolfman"} fail! @diff = Differ.diff_by_char(@current, @original) -# => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!" +# => Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il! ``` -... or call #diff directly and supply your own boundary string! +### Ok, but that doesn't quite cover my case, I have to split by "whatever". + +No problem, you can call diff directly and supply your own boundary string: ```ruby @diff = Differ.diff(@current, @original) # implicitly by line! -# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => {"Epic lolcat fail!" >> "Epic wolfman fail!"} @diff = Differ.diff(@current, @original, 'i') -# => "Epi{"c lolcat fa" >> "c wolfman fa"}il" +# => Epi{"c lolcat fa" >> "c wolfman fa"}il ``` -If you would like something a little more inline... +### Yeah, thats nice. But a simple string might not always cut it... + +Well, you can supply a regex instead of your string if you have to: ```ruby -require 'differ/string' +Differ.diff(@original, @current, /[a-z]i/ +# => E{"c wolfman f" >> "c lolcat f"}l! +``` + +### Ok, ok, but I don't like having to write "Differ" everywhere. +If you would like something a little more inline you can `require 'differ/string'` to get some added inline string magic: + +```ruby @diff = @current.diff(@original) # implicitly by line! -# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +# => {"Epic lolcat fail!" >> "Epic wolfman fail!"} ``` -... or a lot more inline... +Or a lot more inline: ```ruby -@diff = (@current - @original) # implicitly by line! -# => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}" +diff = (@current - @original) # implicitly by line! +# => {"Epic lolcat fail!" >> "Epic wolfman fail!"} -Differ.separator = ' ' -@diff = (@current - @original) -# => "Epic {"lolcat" >> "wolfman"} fail!" +Differ.separator = ' ' # <- Custom string +diff = (@current - @original) +# => Epic {"lolcat" >> "wolfman"} fail! + +Differ.separator = /[a-z]i/ # <- Custom regex without capture group +diff = @original - @current +=> E{"c wolfman f" >> "c lolcat f"}l! + +Differ.separator = /([a-z]i)/ # <- Custom regex with capture group +diff = @original - @current +=> Epi{"c wolfman f" >> "c lolcat f"}ail! ``` -... we've pretty much got you covered. +So we've pretty much got you covered. -### Output Formatting +## Waht about output formatting? Need a different output format? We've got a few of those too. @@ -100,8 +115,9 @@ Differ.format = :html Differ.format = MyCustomFormatModule ``` -Don't want to change the system-wide default for only a single diff output? -Yeah, me either. +### But I don't want to change the system-wide default for only a single diff output! + +Yeah, we either: ```ruby @diff = (@current - @original) From c490e4b0fd5730df3b9ff86a8a3929a40547fd65 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 12:07:47 +0200 Subject: [PATCH 09/12] Slimms down the code examples in the readme --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5a05142..68ff8e9 100644 --- a/README.md +++ b/README.md @@ -42,13 +42,13 @@ require 'differ' There are a number of built-in diff_by_* methods to choose from for standard use: ```ruby -@diff = Differ.diff_by_line(@current, @original) +Differ.diff_by_line(@current, @original) # => {"Epic lolcat fail!" >> "Epic wolfman fail!"} -@diff = Differ.diff_by_word(@current, @original) +Differ.diff_by_word(@current, @original) # => Epic {"lolcat" >> "wolfman"} fail! -@diff = Differ.diff_by_char(@current, @original) +Differ.diff_by_char(@current, @original) # => Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il! ``` @@ -57,10 +57,10 @@ There are a number of built-in diff_by_* methods to choose from for standard use No problem, you can call diff directly and supply your own boundary string: ```ruby -@diff = Differ.diff(@current, @original) # implicitly by line! +Differ.diff(@current, @original) # implicitly by line! # => {"Epic lolcat fail!" >> "Epic wolfman fail!"} -@diff = Differ.diff(@current, @original, 'i') +Differ.diff(@current, @original, 'i') # => Epi{"c lolcat fa" >> "c wolfman fa"}il ``` @@ -78,27 +78,27 @@ Differ.diff(@original, @current, /[a-z]i/ If you would like something a little more inline you can `require 'differ/string'` to get some added inline string magic: ```ruby -@diff = @current.diff(@original) # implicitly by line! +@current.diff(@original) # Implicitly by line by default # => {"Epic lolcat fail!" >> "Epic wolfman fail!"} ``` Or a lot more inline: ```ruby -diff = (@current - @original) # implicitly by line! +@current - @original # Implicitly by line by default # => {"Epic lolcat fail!" >> "Epic wolfman fail!"} -Differ.separator = ' ' # <- Custom string -diff = (@current - @original) +Differ.separator = ' ' # Custom string +@current - @original # => Epic {"lolcat" >> "wolfman"} fail! -Differ.separator = /[a-z]i/ # <- Custom regex without capture group -diff = @original - @current -=> E{"c wolfman f" >> "c lolcat f"}l! +Differ.separator = /[a-z]i/ # Custom regex without capture group +@original - @current +# => E{"c wolfman f" >> "c lolcat f"}l! -Differ.separator = /([a-z]i)/ # <- Custom regex with capture group -diff = @original - @current -=> Epi{"c wolfman f" >> "c lolcat f"}ail! +Differ.separator = /([a-z]i)/ # Custom regex with capture group +@original - @current +# => Epi{"c wolfman f" >> "c lolcat f"}ail! ``` So we've pretty much got you covered. From eb9fe6ebf8e1867d4a148cff70e5b09ded9b698a Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 12:11:06 +0200 Subject: [PATCH 10/12] Added capture group example to regex in readme --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 68ff8e9..94ed4ef 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Differ.diff_by_char(@current, @original) No problem, you can call diff directly and supply your own boundary string: ```ruby -Differ.diff(@current, @original) # implicitly by line! +Differ.diff(@current, @original) # Implicitly by line by default # => {"Epic lolcat fail!" >> "Epic wolfman fail!"} Differ.diff(@current, @original, 'i') @@ -69,10 +69,17 @@ Differ.diff(@current, @original, 'i') Well, you can supply a regex instead of your string if you have to: ```ruby -Differ.diff(@original, @current, /[a-z]i/ +Differ.diff(@original, @current, /[a-z]i/) # => E{"c wolfman f" >> "c lolcat f"}l! ``` +Include a capture group if you want to keep the separator: + +```ruby +Differ.diff(@original, @current, /([a-z]i)/) +# => Epi{"c wolfman f" >> "c lolcat f"}ail! +``` + ### Ok, ok, but I don't like having to write "Differ" everywhere. If you would like something a little more inline you can `require 'differ/string'` to get some added inline string magic: From 356f2e223ae311290e69872509e1ba4d2a1c4e6e Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Mon, 3 Jun 2013 12:44:38 +0200 Subject: [PATCH 11/12] Changed formatters to use call instead of format so you can use procs and lambdas as formatters --- README.md | 19 ++++++++++++++----- lib/differ.rb | 17 ++++++++++------- lib/differ/change.rb | 2 +- lib/differ/diff.rb | 2 +- lib/differ/format/ascii.rb | 2 +- lib/differ/format/color.rb | 2 +- lib/differ/format/html.rb | 2 +- spec/differ/change_spec.rb | 6 +++--- spec/differ/diff_spec.rb | 8 ++++---- spec/differ/format/ascii_spec.rb | 6 +++--- spec/differ/format/color_spec.rb | 6 +++--- spec/differ/format/html_spec.rb | 6 +++--- spec/differ_spec.rb | 4 ++-- 13 files changed, 47 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 94ed4ef..2232dee 100644 --- a/README.md +++ b/README.md @@ -110,25 +110,34 @@ Differ.separator = /([a-z]i)/ # Custom regex with capture group So we've pretty much got you covered. -## Waht about output formatting? +## What about output formatting? -Need a different output format? We've got a few of those too. +Need a different output format? We've got a few of those too: ```ruby -Differ.format = :ascii # <- Default +Differ.format = :ascii # Default Differ.format = :color Differ.format = :html Differ.format = MyCustomFormatModule ``` +The formatter must respond to the call method that takes an instant of the Change class as an argument and returns a string. + ### But I don't want to change the system-wide default for only a single diff output! Yeah, we either: ```ruby -@diff = (@current - @original) -@diff.format_as(:color) +diff = @current - @original +diff.format_as(:color) +``` + +Or with your own formatter: + +```ruby +diff = @current - @original +diff.format_as(->(c){c.to_s}) ``` ## Copyright diff --git a/lib/differ.rb b/lib/differ.rb index e25723f..20eadb9 100644 --- a/lib/differ.rb +++ b/lib/differ.rb @@ -53,13 +53,16 @@ def format end def format_for(f) - case f - when Module then f - when :ascii then Format::Ascii - when :color then Format::Color - when :html then Format::HTML - when nil then nil - else raise "Unknown format type #{f.inspect}" + if f.respond_to? :call + f + else + case f + when :ascii then Format::Ascii + when :color then Format::Color + when :html then Format::HTML + when nil then nil + else raise "Unknown format type #{f.inspect}" + end end end diff --git a/lib/differ/change.rb b/lib/differ/change.rb index e143452..5ea251b 100644 --- a/lib/differ/change.rb +++ b/lib/differ/change.rb @@ -19,7 +19,7 @@ def change? end def to_s - Differ.format.format(self) + Differ.format.call(self) end alias :inspect :to_s diff --git a/lib/differ/diff.rb b/lib/differ/diff.rb index 2db3e19..1ce5b88 100644 --- a/lib/differ/diff.rb +++ b/lib/differ/diff.rb @@ -76,7 +76,7 @@ def format_as(f) @raw.inject('') do |sum, part| part = case part when String then part - when Change then f.format(part) + when Change then f.call(part) end sum << part end diff --git a/lib/differ/format/ascii.rb b/lib/differ/format/ascii.rb index 815af6a..6985d3d 100644 --- a/lib/differ/format/ascii.rb +++ b/lib/differ/format/ascii.rb @@ -2,7 +2,7 @@ module Differ module Format module Ascii class << self - def format(change) + def call(change) (change.change? && as_change(change)) || (change.delete? && as_delete(change)) || (change.insert? && as_insert(change)) || diff --git a/lib/differ/format/color.rb b/lib/differ/format/color.rb index 8ac399a..41bf81e 100644 --- a/lib/differ/format/color.rb +++ b/lib/differ/format/color.rb @@ -2,7 +2,7 @@ module Differ module Format module Color class << self - def format(change) + def call(change) (change.change? && as_change(change)) || (change.delete? && as_delete(change)) || (change.insert? && as_insert(change)) || diff --git a/lib/differ/format/html.rb b/lib/differ/format/html.rb index 770c4cd..8f92250 100644 --- a/lib/differ/format/html.rb +++ b/lib/differ/format/html.rb @@ -2,7 +2,7 @@ module Differ module Format module HTML class << self - def format(change) + def call(change) (change.change? && as_change(change)) || (change.delete? && as_delete(change)) || (change.insert? && as_insert(change)) || diff --git a/spec/differ/change_spec.rb b/spec/differ/change_spec.rb index ca2f057..14b81d7 100644 --- a/spec/differ/change_spec.rb +++ b/spec/differ/change_spec.rb @@ -2,7 +2,7 @@ describe Differ::Change do before(:each) do - @format = Module.new { def self.format(c); end } + @format = Module.new { def self.call(c); end } Differ.format = @format end @@ -20,7 +20,7 @@ end it 'should stringify to ""' do - @format.should_receive(:format).once.and_return('') + @format.should_receive(:call).once.and_return('') @change.to_s.should == '' end end @@ -76,7 +76,7 @@ end it "should stringify via the current format's #format method" do - @format.should_receive(:format).once + @format.should_receive(:call).once Differ::Change.new.to_s end end \ No newline at end of file diff --git a/spec/differ/diff_spec.rb b/spec/differ/diff_spec.rb index ea225a0..14df9ad 100644 --- a/spec/differ/diff_spec.rb +++ b/spec/differ/diff_spec.rb @@ -22,7 +22,7 @@ it 'should delegate insertion changes to Differ#format' do i = +'b' - @format.should_receive(:format).once.with(i).and_return('!') + @format.should_receive(:call).once.with(i).and_return('!') diff('a', i, 'c').to_s.should == 'a!c' end end @@ -30,12 +30,12 @@ describe '#format_as' do before(:each) do @change = +'b' - Differ.format = Module.new { def self.format(c); raise :error; end } - @format = Module.new { def self.format(c); end } + Differ.format = Module.new { def self.call(c); raise :error; end } + @format = Module.new { def self.call(c); end } end it 'should delegate change formatting to the given format' do - @format.should_receive(:format).once.with(@change).and_return('!') + @format.should_receive(:call).once.with(@change).and_return('!') diff('a', @change, 'c').format_as(@format).should == 'a!c' end diff --git a/spec/differ/format/ascii_spec.rb b/spec/differ/format/ascii_spec.rb index 93a9dd6..e34b2bf 100644 --- a/spec/differ/format/ascii_spec.rb +++ b/spec/differ/format/ascii_spec.rb @@ -3,16 +3,16 @@ describe Differ::Format::Ascii do it 'should format inserts well' do @expected = '{+"SAMPLE"}' - Differ::Format::Ascii.format(+'SAMPLE').should == @expected + Differ::Format::Ascii.call(+'SAMPLE').should == @expected end it 'should format deletes well' do @expected = '{-"SAMPLE"}' - Differ::Format::Ascii.format(-'SAMPLE').should == @expected + Differ::Format::Ascii.call(-'SAMPLE').should == @expected end it 'should format changes well' do @expected = '{"THEN" >> "NOW"}' - Differ::Format::Ascii.format('THEN' >> 'NOW').should == @expected + Differ::Format::Ascii.call('THEN' >> 'NOW').should == @expected end end \ No newline at end of file diff --git a/spec/differ/format/color_spec.rb b/spec/differ/format/color_spec.rb index 4cf6223..56abd0a 100644 --- a/spec/differ/format/color_spec.rb +++ b/spec/differ/format/color_spec.rb @@ -3,16 +3,16 @@ describe Differ::Format::Color do it 'should format inserts well' do @expected = "\033[32mSAMPLE\033[0m" - Differ::Format::Color.format(+'SAMPLE').should == @expected + Differ::Format::Color.call(+'SAMPLE').should == @expected end it 'should format deletes well' do @expected = "\033[31mSAMPLE\033[0m" - Differ::Format::Color.format(-'SAMPLE').should == @expected + Differ::Format::Color.call(-'SAMPLE').should == @expected end it 'should format changes well' do @expected = "\033[31mTHEN\033[0m\033[32mNOW\033[0m" - Differ::Format::Color.format('THEN' >> 'NOW').should == @expected + Differ::Format::Color.call('THEN' >> 'NOW').should == @expected end end \ No newline at end of file diff --git a/spec/differ/format/html_spec.rb b/spec/differ/format/html_spec.rb index c3da457..fe33eec 100644 --- a/spec/differ/format/html_spec.rb +++ b/spec/differ/format/html_spec.rb @@ -3,16 +3,16 @@ describe Differ::Format::HTML do it 'should format inserts well' do @expected = 'SAMPLE' - Differ::Format::HTML.format(+'SAMPLE').should == @expected + Differ::Format::HTML.call(+'SAMPLE').should == @expected end it 'should format deletes well' do @expected = 'SAMPLE' - Differ::Format::HTML.format(-'SAMPLE').should == @expected + Differ::Format::HTML.call(-'SAMPLE').should == @expected end it 'should format changes well' do @expected = 'THENNOW' - Differ::Format::HTML.format('THEN' >> 'NOW').should == @expected + Differ::Format::HTML.call('THEN' >> 'NOW').should == @expected end end \ No newline at end of file diff --git a/spec/differ_spec.rb b/spec/differ_spec.rb index cd9087f..32a52e9 100644 --- a/spec/differ_spec.rb +++ b/spec/differ_spec.rb @@ -30,8 +30,8 @@ describe '#format_for' do before(:each) { Differ.format = nil } - it 'should store any module passed to it' do - formatter = Module.new + it 'should store any callable passed to it' do + formatter = ->(c){ c.to_s } Differ.format_for(formatter).should == formatter end From 241e9301bf302428ef99eca8283d8cb02af9eccc Mon Sep 17 00:00:00 2001 From: Jonas Schubert Erlandsson Date: Tue, 4 Jun 2013 08:57:54 +0200 Subject: [PATCH 12/12] Updates gemspec, fixes typo in readme, changes versioning handling and removes Rakefile --- README.md | 2 +- Rakefile | 38 -------------------- VERSION.yml | 4 --- differ.gemspec | 80 ++++++++++--------------------------------- lib/differ/version.rb | 3 ++ 5 files changed, 23 insertions(+), 104 deletions(-) delete mode 100644 Rakefile delete mode 100644 VERSION.yml create mode 100644 lib/differ/version.rb diff --git a/README.md b/README.md index 2232dee..76c552c 100644 --- a/README.md +++ b/README.md @@ -155,4 +155,4 @@ start significantly changing what it does, I'd rather you did! Make it your own little work of art, complete with a stylish flowing signature in the corner. All I really did was give you the canvas. And my blessing. - Know always right from wrong, and let others see your good works. +> Know always right from wrong, and let others see your good works. diff --git a/Rakefile b/Rakefile deleted file mode 100644 index da96b5c..0000000 --- a/Rakefile +++ /dev/null @@ -1,38 +0,0 @@ -require 'rake' - -begin - require 'jeweler' - Jeweler::Tasks.new do |gem| - gem.name = "differ" - gem.summary = "A simple gem for generating string diffs" - gem.email = "pvande@gmail.com" - gem.homepage = "http://github.com/pvande/differ" - gem.authors = [ "Pieter van de Bruggen" ] - end -rescue LoadError - puts "Jeweler not available. Install it with: gem install jeweler" -end - -require 'rake/rdoctask' -Rake::RDocTask.new do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'differ' - rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README*') - rdoc.rdoc_files.include('lib/**/*.rb') -end - -require 'spec/rake/spectask' -Spec::Rake::SpecTask.new(:spec) do |spec| - spec.libs << 'lib' << 'spec' - spec.spec_files = FileList['spec/**/*_spec.rb'] -end - -Spec::Rake::SpecTask.new(:rcov) do |spec| - spec.libs << 'lib' << 'spec' - spec.pattern = 'spec/**/*_spec.rb' - spec.rcov = true -end - - -task :default => :spec diff --git a/VERSION.yml b/VERSION.yml deleted file mode 100644 index 20ba869..0000000 --- a/VERSION.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -:patch: 2 -:major: 0 -:minor: 1 diff --git a/differ.gemspec b/differ.gemspec index d59e0e5..fef0377 100644 --- a/differ.gemspec +++ b/differ.gemspec @@ -1,63 +1,21 @@ -# Generated by jeweler -# DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' -# -*- encoding: utf-8 -*- +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'differ/version' -Gem::Specification.new do |s| - s.name = %q{differ} - s.version = "0.1.2" - - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Pieter van de Bruggen"] - s.date = %q{2011-02-17} - s.email = %q{pvande@gmail.com} - s.extra_rdoc_files = [ - "README.rdoc" - ] - s.files = [ - "README.rdoc", - "Rakefile", - "VERSION.yml", - "differ.gemspec", - "lib/differ.rb", - "lib/differ/change.rb", - "lib/differ/diff.rb", - "lib/differ/format/ascii.rb", - "lib/differ/format/color.rb", - "lib/differ/format/html.rb", - "lib/differ/string.rb", - "spec/differ/change_spec.rb", - "spec/differ/diff_spec.rb", - "spec/differ/format/ascii_spec.rb", - "spec/differ/format/color_spec.rb", - "spec/differ/format/html_spec.rb", - "spec/differ/string_spec.rb", - "spec/differ_spec.rb", - "spec/spec_helper.rb" - ] - s.homepage = %q{http://github.com/pvande/differ} - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.7} - s.summary = %q{A simple gem for generating string diffs} - s.test_files = [ - "spec/differ/change_spec.rb", - "spec/differ/diff_spec.rb", - "spec/differ/format/ascii_spec.rb", - "spec/differ/format/color_spec.rb", - "spec/differ/format/html_spec.rb", - "spec/differ/string_spec.rb", - "spec/differ_spec.rb", - "spec/spec_helper.rb" - ] - - if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 3 - - if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - else - end - else - end -end +Gem::Specification.new do |spec| + spec.name = "differ" + spec.version = Differ::VERSION + spec.authors = ["Pieter van de Bruggen", "Jonas Schubert Erlandsson"] + spec.date = %q{2011-02-17} + spec.email = ["pvande@gmail.com", "jonas.schubert.erlandsson@my-codeworks.com"] + spec.description = "A simple gem for generating string diffs" + spec.summary = "A simple gem for generating string diffs" + spec.homepage = "http://github.com/pvande/differ" + spec.license = "MIT" + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] +end \ No newline at end of file diff --git a/lib/differ/version.rb b/lib/differ/version.rb new file mode 100644 index 0000000..a607755 --- /dev/null +++ b/lib/differ/version.rb @@ -0,0 +1,3 @@ +module Differ + VERSION = "0.1.3" +end