sub_diff 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +22 -0
- data/lib/sub_diff/diff.rb +22 -0
- data/lib/sub_diff/diff_collection.rb +38 -0
- data/lib/sub_diff/version.rb +17 -0
- data/lib/sub_diff.rb +39 -0
- data/test/diff_collection_test.rb +42 -0
- data/test/diff_test.rb +42 -0
- data/test/sub_diff_test.rb +85 -0
- data/test/test_helper.rb +7 -0
- metadata +94 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Sean Huber - shuber@huberry.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= sub_diff
|
2
|
+
|
3
|
+
Allows you to apply regular expression replacements to strings while presenting the result in a "diff" like format
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install sub_diff
|
8
|
+
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
<tt>String#gsub_diff</tt> or <tt>String#sub_diff</tt> accept the same arguments as the standard <tt>gsub</tt> and <tt>sub</tt> methods, however the returned object is enumerable and allows you to iterate thru each replacement
|
13
|
+
|
14
|
+
replacement = 'this is a test'.gsub_diff(/(\S*is)/, 'x(\1)')
|
15
|
+
|
16
|
+
puts replacement
|
17
|
+
# => x(this) x(is) a test
|
18
|
+
|
19
|
+
replacement.each { |diff| puts [diff.changed?, diff.value_was.inspect, diff.value.inspect].join(' - ') }
|
20
|
+
# => true - "this" - "x(this)"
|
21
|
+
# => false - " " - " "
|
22
|
+
# => true - "is" - "x(is)"
|
23
|
+
# => false - " a test" - " a test"
|
24
|
+
|
25
|
+
|
26
|
+
== Patches and pull requests
|
27
|
+
|
28
|
+
* Fork the project.
|
29
|
+
* Make your feature addition or bug fix.
|
30
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
31
|
+
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
32
|
+
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the sub_diff gem'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs += ['lib', 'test']
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the sub_diff gem'
|
16
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'sub_diff'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module SubDiff
|
2
|
+
class Diff
|
3
|
+
attr_reader :value, :value_was
|
4
|
+
alias_method :to_s, :value
|
5
|
+
|
6
|
+
def initialize(value, value_was = nil)
|
7
|
+
@value, @value_was = value, value_was || value
|
8
|
+
end
|
9
|
+
|
10
|
+
def changed?
|
11
|
+
@changed ||= value != value_was
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(*args, &block)
|
15
|
+
to_s.send(*args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def respond_to_missing?(method, include_private)
|
19
|
+
to_s.respond_to?(method, include_private)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SubDiff
|
2
|
+
class DiffCollection
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(diffs = [])
|
6
|
+
@diffs = []
|
7
|
+
diffs.each { |diff| self << diff }
|
8
|
+
end
|
9
|
+
|
10
|
+
def <<(diff)
|
11
|
+
if diff.changed? || !diff.empty?
|
12
|
+
@diffs << diff
|
13
|
+
@to_s = nil
|
14
|
+
end
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def each(&block)
|
19
|
+
@diffs.each(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(*args, &block)
|
23
|
+
to_s.send(*args, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def respond_to_missing?(method, include_private)
|
27
|
+
to_s.respond_to?(method, include_private)
|
28
|
+
end
|
29
|
+
|
30
|
+
def size
|
31
|
+
@diffs.size
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
@to_s ||= @diffs.join
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SubDiff
|
2
|
+
# Contains information about this gem's version
|
3
|
+
module Version
|
4
|
+
MAJOR = 0
|
5
|
+
MINOR = 0
|
6
|
+
PATCH = 0
|
7
|
+
|
8
|
+
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
|
9
|
+
#
|
10
|
+
# Example
|
11
|
+
#
|
12
|
+
# puts Version # '1.0.2'
|
13
|
+
def self.to_s
|
14
|
+
[MAJOR, MINOR, PATCH].join('.')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/sub_diff.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'respond_to_missing'
|
2
|
+
|
3
|
+
module SubDiff
|
4
|
+
autoload :Diff, 'sub_diff/diff'
|
5
|
+
autoload :DiffCollection, 'sub_diff/diff_collection'
|
6
|
+
autoload :Version, 'sub_diff/version'
|
7
|
+
|
8
|
+
def gsub_diff(*args, &block)
|
9
|
+
with_diff_collection do |diff_collection|
|
10
|
+
match_prefix, suffix_matcher = '', args.first.is_a?(Regexp) ? :match : :include?
|
11
|
+
gsub(args.first) do |match|
|
12
|
+
suffix, prefix, replacement = Diff.new($'), Diff.new($`.sub(match_prefix, '')), Diff.new(match.sub(*args, &block), match)
|
13
|
+
diff_collection << prefix << replacement
|
14
|
+
diff_collection << suffix unless suffix.send(suffix_matcher, args.first)
|
15
|
+
match_prefix << prefix + match
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def sub_diff(*args, &block)
|
21
|
+
with_diff_collection do |diff_collection|
|
22
|
+
sub(args.first) do |match|
|
23
|
+
suffix, prefix, replacement = Diff.new($'), Diff.new($`), Diff.new(match.sub(*args, &block), match)
|
24
|
+
diff_collection << prefix << replacement << suffix
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def with_diff_collection
|
32
|
+
diff_collection = DiffCollection.new
|
33
|
+
yield diff_collection
|
34
|
+
diff_collection << Diff.new(self) if diff_collection.empty?
|
35
|
+
diff_collection
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
String.send(:include, SubDiff)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
module SubDiff
|
4
|
+
class DiffCollectionTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@diffs = [Diff.new('one '), Diff.new('two', '2'), Diff.new(' three ')]
|
7
|
+
@diff_collection = DiffCollection.new(@diffs)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_append
|
11
|
+
size, value = @diff_collection.size, @diff_collection.to_s
|
12
|
+
@diff_collection << Diff.new('four', '4')
|
13
|
+
@diff_collection << Diff.new('', 'test')
|
14
|
+
@diff_collection << Diff.new('') # should skip this since it's empty
|
15
|
+
assert_equal size + 2, @diff_collection.size
|
16
|
+
assert_not_equal value, @diff_collection.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_enumerable
|
20
|
+
assert @diff_collection.is_a?(Enumerable)
|
21
|
+
assert_equal @diffs, @diff_collection.collect { |diff| diff }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_method_missing
|
25
|
+
assert_equal @diffs.to_s.upcase, @diff_collection.upcase
|
26
|
+
assert_raises(NoMethodError) { @diff_collection.invalid_method }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_respond_to_missing?
|
30
|
+
assert @diff_collection.respond_to?(:upcase)
|
31
|
+
assert !@diff_collection.respond_to?(:invalid_method)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_size
|
35
|
+
assert_equal @diffs.size, @diff_collection.size
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
assert_equal @diffs.join, @diff_collection.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/test/diff_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
module SubDiff
|
4
|
+
class DiffTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@diff_value = 'test'
|
7
|
+
@diff_value_was = 'testing'
|
8
|
+
@diff = Diff.new(@diff_value, @diff_value_was)
|
9
|
+
@diff_without_value_was = Diff.new(@diff_value)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_with_value_was
|
13
|
+
assert_equal @diff_value, @diff.value
|
14
|
+
assert_equal @diff_value_was, @diff.value_was
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_initialize_without_value_was
|
18
|
+
assert_equal @diff_value, @diff_without_value_was.value
|
19
|
+
assert_equal @diff_without_value_was.value, @diff_without_value_was.value_was
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_changed?
|
23
|
+
assert @diff.changed?
|
24
|
+
assert !@diff_without_value_was.changed?
|
25
|
+
assert !Diff.new('test', 'test').changed?
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_method_missing
|
29
|
+
assert_equal @diff_value.upcase, @diff.upcase
|
30
|
+
assert_raises(NoMethodError) { @diff.invalid_method }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_respond_to_missing?
|
34
|
+
assert @diff.respond_to?(:upcase)
|
35
|
+
assert !@diff.respond_to?(:invalid_method)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_to_s
|
39
|
+
assert_equal @diff.value, @diff.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
module SubDiff
|
4
|
+
class SubDiffTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@string = 'this is a simple test'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_gsub_diff_with_block
|
10
|
+
diff_collection = @string.gsub_diff('i') { |match| 'x' }
|
11
|
+
assert_equal 'thxs xs a sxmple test', diff_collection.to_s
|
12
|
+
assert_equal 7, diff_collection.size
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_gsub_diff_with_hash
|
16
|
+
diff_collection = @string.gsub_diff(/(\S*is)/, 'is' => 'IS', 'this' => 'THIS')
|
17
|
+
assert_equal 'THIS IS a harder test', diff_collection.to_s
|
18
|
+
assert_equal 4, diff_collection.size
|
19
|
+
rescue TypeError => error # Hash arguments only work in ruby 1.9+
|
20
|
+
assert_equal "can't convert Hash into String", error.message
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_gsub_diff_with_regex
|
24
|
+
diff_collection = @string.gsub_diff(/(\S*is)/, 'X')
|
25
|
+
assert_equal 'X X a simple test', diff_collection.to_s
|
26
|
+
assert_equal 4, diff_collection.size
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_gsub_diff_with_regex_captures
|
30
|
+
diff_collection = @string.gsub_diff(/(\S*is)/, 'X(\1)')
|
31
|
+
assert_equal 'X(this) X(is) a simple test', diff_collection.to_s
|
32
|
+
assert_equal 4, diff_collection.size
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_gsub_diff_with_string
|
36
|
+
diff_collection = @string.gsub_diff('i', 'x')
|
37
|
+
assert_equal 'thxs xs a sxmple test', diff_collection.to_s
|
38
|
+
assert_equal 7, diff_collection.size
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_gsub_diff_without_matches
|
42
|
+
diff_collection = @string.gsub_diff('no-match', 'test')
|
43
|
+
assert_equal @string, diff_collection.to_s
|
44
|
+
assert_equal 1, diff_collection.size
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_sub_diff_with_block
|
48
|
+
diff_collection = @string.sub_diff('simple') { |match| 'block' }
|
49
|
+
assert_equal 'this is a block test', diff_collection.to_s
|
50
|
+
assert_equal 3, diff_collection.size
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_sub_diff_with_hash
|
54
|
+
diff_collection = @string.sub_diff(/simple/, 'simple' => 'harder')
|
55
|
+
assert_equal 'this is a harder test', diff_collection.to_s
|
56
|
+
assert_equal 3, diff_collection.size
|
57
|
+
rescue TypeError => error # Hash arguments only work in ruby 1.9+
|
58
|
+
assert_equal "can't convert Hash into String", error.message
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_sub_diff_with_regex
|
62
|
+
diff_collection = @string.sub_diff(/a \S+/, 'an easy')
|
63
|
+
assert_equal 'this is an easy test', diff_collection.to_s
|
64
|
+
assert_equal 3, diff_collection.size
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_sub_diff_with_regex_captures
|
68
|
+
diff_collection = @string.sub_diff(/a (\S+)/, 'a very \1')
|
69
|
+
assert_equal 'this is a very simple test', diff_collection.to_s
|
70
|
+
assert_equal 3, diff_collection.size
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_sub_diff_with_string
|
74
|
+
diff_collection = @string.sub_diff('simple', 'very simple')
|
75
|
+
assert_equal 'this is a very simple test', diff_collection.to_s
|
76
|
+
assert_equal 3, diff_collection.size
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_sub_diff_without_matches
|
80
|
+
diff_collection = @string.sub_diff('no-match', 'test')
|
81
|
+
assert_equal @string, diff_collection.to_s
|
82
|
+
assert_equal 1, diff_collection.size
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sub_diff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sean Huber
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-08-16 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: respond_to_missing
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Allows you to apply regular expression replacements to strings while presenting the result in a "diff" like format
|
36
|
+
email: shuber@huberry.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/sub_diff/diff.rb
|
45
|
+
- lib/sub_diff/diff_collection.rb
|
46
|
+
- lib/sub_diff/version.rb
|
47
|
+
- lib/sub_diff.rb
|
48
|
+
- Gemfile
|
49
|
+
- MIT-LICENSE
|
50
|
+
- Rakefile
|
51
|
+
- README.rdoc
|
52
|
+
- test/diff_collection_test.rb
|
53
|
+
- test/diff_test.rb
|
54
|
+
- test/sub_diff_test.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/shuber/sub_diff
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.6.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Apply regular expression replacements to strings while presenting the result in a "diff" like format
|
90
|
+
test_files:
|
91
|
+
- test/diff_collection_test.rb
|
92
|
+
- test/diff_test.rb
|
93
|
+
- test/sub_diff_test.rb
|
94
|
+
- test/test_helper.rb
|