sub_diff 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -9
- data/lib/sub_diff/builder.rb +18 -14
- data/lib/sub_diff/collection.rb +1 -17
- data/lib/sub_diff/core_ext/string.rb +0 -2
- data/lib/sub_diff/diff.rb +0 -2
- data/lib/sub_diff/differ.rb +4 -8
- data/lib/sub_diff/gsub.rb +1 -3
- data/lib/sub_diff/sub.rb +3 -3
- data/lib/sub_diff/version.rb +1 -1
- data/lib/sub_diff.rb +8 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/sub_diff/collection_spec.rb +9 -21
- data/spec/sub_diff/diff_spec.rb +0 -2
- data/spec/sub_diff_spec.rb +0 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc9000575a6556b4b118600aa932fec2efdf92c6
|
4
|
+
data.tar.gz: 0052e7df82331d0beff26bb09cf447be317ab995
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be6f74f1cb8a5965931792a42e46a0638ff2573bb41893df7a2a4f12f7e2b8557d760b3bb80f474e0e2e8e526361e0684ea06dfd7dc084b0394955120be7608e
|
7
|
+
data.tar.gz: 5f520e25084f52d1a60a1a925bfd9310b741c8a45b77e45893dcb632d6ce258e4eaa8b53ecac4435740405c89fb5c4cd5250356293b5a405719cbe9614f66d45
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -29,10 +29,10 @@ This gem introduces a couple new methods to [`String`](http://ruby-doc.org/core-
|
|
29
29
|
These methods accept the same arguments as their `sub` and `gsub` counterparts.
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
replaced = 'this is a test'.gsub_diff(/(\S*is)/, 'replaced(\1)') #=> #<
|
32
|
+
replaced = 'this is a test'.gsub_diff(/(\S*is)/, 'replaced(\1)') #=> #<SubDiff::Collection:0x007fc532049508>
|
33
33
|
```
|
34
34
|
|
35
|
-
The difference is that it returns a `
|
35
|
+
The difference is that it returns a `SubDiff::Collection` instead. This object behaves like a `String`.
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
puts replaced #=> "replaced(this) replaced(is) a test"
|
@@ -88,13 +88,15 @@ end
|
|
88
88
|
|
89
89
|
[YARD Documentation](http://www.rubydoc.info/github/shuber/sub_diff)
|
90
90
|
|
91
|
-
* `
|
92
|
-
* `
|
93
|
-
* `Diff#
|
94
|
-
* `
|
95
|
-
* `
|
96
|
-
* `
|
97
|
-
* `
|
91
|
+
* `String#gsub`
|
92
|
+
* `String#sub`
|
93
|
+
* `SubDiff::Diff#changed?`
|
94
|
+
* `SubDiff::Diff#value`
|
95
|
+
* `SubDiff::Diff#value_was`
|
96
|
+
* `SubDiff::Collection#changed?`
|
97
|
+
* `SubDiff::Collection#diffs`
|
98
|
+
* `SubDiff::Collection#each`
|
99
|
+
* `SubDiff::Collection#size`
|
98
100
|
|
99
101
|
## Testing
|
100
102
|
|
data/lib/sub_diff/builder.rb
CHANGED
@@ -1,34 +1,38 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
require 'sub_diff/collection'
|
3
|
-
require 'sub_diff/differ'
|
4
|
-
require 'sub_diff/gsub'
|
5
|
-
|
6
1
|
module SubDiff
|
7
2
|
class Builder < Struct.new(:string, :type)
|
8
|
-
|
3
|
+
def diff(*args, &block)
|
4
|
+
builder.diff(*args, &block)
|
5
|
+
collection
|
6
|
+
end
|
9
7
|
|
10
|
-
|
8
|
+
def push(*args)
|
9
|
+
if args.compact.any?
|
10
|
+
diff = Diff.new(*args)
|
11
|
+
collection.push(diff)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
alias_method :<<, :push
|
11
15
|
|
12
16
|
private
|
13
17
|
|
14
|
-
def instance
|
15
|
-
builder.new(differ)
|
16
|
-
end
|
17
|
-
|
18
18
|
def builder
|
19
|
-
|
19
|
+
constant.new(differ)
|
20
20
|
end
|
21
21
|
|
22
22
|
def constant
|
23
|
+
Module.nesting.last.const_get(constant_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def constant_name
|
23
27
|
type.to_s.capitalize
|
24
28
|
end
|
25
29
|
|
26
30
|
def differ
|
27
|
-
Differ.new(
|
31
|
+
Differ.new(self, type)
|
28
32
|
end
|
29
33
|
|
30
34
|
def collection
|
31
|
-
Collection.new(string)
|
35
|
+
@collection ||= Collection.new(string)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
data/lib/sub_diff/collection.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
require 'forwardable'
|
3
|
-
require 'sub_diff/diff'
|
4
|
-
|
5
1
|
module SubDiff
|
6
2
|
class Collection < SimpleDelegator
|
7
3
|
extend Forwardable
|
@@ -21,19 +17,7 @@ module SubDiff
|
|
21
17
|
diffs.any?(&:changed?)
|
22
18
|
end
|
23
19
|
|
24
|
-
def push(
|
25
|
-
if args.compact.any?
|
26
|
-
diff = Diff.new(*args)
|
27
|
-
append(diff)
|
28
|
-
end
|
29
|
-
|
30
|
-
self
|
31
|
-
end
|
32
|
-
alias_method :<<, :push
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def append(diff)
|
20
|
+
def push(diff)
|
37
21
|
unless diff.empty?
|
38
22
|
diffs << diff
|
39
23
|
__setobj__(diffs.join)
|
data/lib/sub_diff/diff.rb
CHANGED
data/lib/sub_diff/differ.rb
CHANGED
@@ -1,22 +1,18 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
1
|
module SubDiff
|
4
|
-
class Differ < Struct.new(:
|
2
|
+
class Differ < Struct.new(:builder, :type)
|
5
3
|
extend Forwardable
|
6
4
|
|
7
|
-
def_delegators :
|
5
|
+
def_delegators :builder, :string
|
8
6
|
|
9
|
-
def
|
7
|
+
def each_diff(*args)
|
10
8
|
# Ruby 1.8.7 does not support additional args after * (splat)
|
11
9
|
block = args.pop
|
12
10
|
|
13
11
|
string.send(type, args.first) do |match|
|
14
12
|
diff = { :match => match, :prefix => $`, :suffix => $' }
|
15
13
|
diff[:replacement] = match.sub(*args, &block)
|
16
|
-
yield(
|
14
|
+
yield(builder, diff)
|
17
15
|
end
|
18
|
-
|
19
|
-
collection
|
20
16
|
end
|
21
17
|
end
|
22
18
|
end
|
data/lib/sub_diff/gsub.rb
CHANGED
data/lib/sub_diff/sub.rb
CHANGED
@@ -4,14 +4,14 @@ module SubDiff
|
|
4
4
|
# Ruby 1.8.7 does not support additional args after * (splat)
|
5
5
|
args.push(block)
|
6
6
|
|
7
|
-
differ.
|
8
|
-
|
7
|
+
differ.each_diff(*args) do |builder, diff|
|
8
|
+
process(builder, diff, args.first)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def
|
14
|
+
def process(builder, diff, search)
|
15
15
|
builder << prefix(diff)
|
16
16
|
builder.push(diff[:replacement], diff[:match])
|
17
17
|
builder << suffix(diff, search)
|
data/lib/sub_diff/version.rb
CHANGED
data/lib/sub_diff.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'forwardable'
|
1
3
|
require 'sub_diff/core_ext/string'
|
4
|
+
require 'sub_diff/builder'
|
5
|
+
require 'sub_diff/collection'
|
6
|
+
require 'sub_diff/diff'
|
7
|
+
require 'sub_diff/differ'
|
8
|
+
require 'sub_diff/sub'
|
9
|
+
require 'sub_diff/gsub'
|
2
10
|
require 'sub_diff/version'
|
3
11
|
|
4
12
|
String.send(:include, SubDiff::CoreExt::String)
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../lib/sub_diff/collection', __FILE__)
|
2
|
-
|
3
1
|
RSpec.describe SubDiff::Collection do
|
4
2
|
subject { described_class.new(diffable) }
|
5
3
|
|
@@ -7,12 +5,14 @@ RSpec.describe SubDiff::Collection do
|
|
7
5
|
|
8
6
|
describe '#changed?' do
|
9
7
|
it 'should return true if any diffs have changed' do
|
10
|
-
|
8
|
+
diff = double('diff', :changed? => true, :empty? => false)
|
9
|
+
subject.push(diff)
|
11
10
|
expect(subject).to be_changed
|
12
11
|
end
|
13
12
|
|
14
13
|
it 'should return false if no diffs have changed' do
|
15
|
-
|
14
|
+
diff = double('diff', :changed? => false, :empty? => false)
|
15
|
+
subject.push(diff)
|
16
16
|
expect(subject).not_to be_changed
|
17
17
|
end
|
18
18
|
|
@@ -27,27 +27,15 @@ RSpec.describe SubDiff::Collection do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
describe '#push' do
|
30
|
-
it 'should
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
it 'should append an unchanged diff' do
|
35
|
-
block = proc { subject.push('unchanged') }
|
30
|
+
it 'should append a diff' do
|
31
|
+
diff = double('diff', :empty? => false)
|
32
|
+
block = proc { subject.push(diff) }
|
36
33
|
expect(block).to change(subject.diffs, :size)
|
37
34
|
end
|
38
35
|
|
39
|
-
it 'should append a changed diff' do
|
40
|
-
block = proc { subject.push('now', 'was') }
|
41
|
-
expect(block).to change(subject.diffs, :size)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should not append a nil diff' do
|
45
|
-
block = proc { subject.push(nil) }
|
46
|
-
expect(block).not_to change(subject.diffs, :size)
|
47
|
-
end
|
48
|
-
|
49
36
|
it 'should not append an empty diff' do
|
50
|
-
|
37
|
+
diff = double('diff', :empty? => true)
|
38
|
+
block = proc { subject.push(diff) }
|
51
39
|
expect(block).not_to change(subject.diffs, :size)
|
52
40
|
end
|
53
41
|
end
|
data/spec/sub_diff/diff_spec.rb
CHANGED
data/spec/sub_diff_spec.rb
CHANGED