sub_diff 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db20a712d5a1c4827ecdb355b8bf7908014acf55
4
- data.tar.gz: 2e0fada841e581713251b66ead8df9dad486d3cd
3
+ metadata.gz: dc9000575a6556b4b118600aa932fec2efdf92c6
4
+ data.tar.gz: 0052e7df82331d0beff26bb09cf447be317ab995
5
5
  SHA512:
6
- metadata.gz: 210f157fb1dd0ec4da7384d888ed07399507372a8afd0f11bfdc5583935012dc736c947c1f528aabe86386030570e29f5976fc1057f0c95e2c3064a0e79b0520
7
- data.tar.gz: 3f4c90c216981bb97c04edb43785e5a1cabc5376858e8b82f0c5b36f853ed02da542addfab1f491eab301622505601962d2f07eff137877ffc46af6ae70bee45
6
+ metadata.gz: be6f74f1cb8a5965931792a42e46a0638ff2573bb41893df7a2a4f12f7e2b8557d760b3bb80f474e0e2e8e526361e0684ea06dfd7dc084b0394955120be7608e
7
+ data.tar.gz: 5f520e25084f52d1a60a1a925bfd9310b741c8a45b77e45893dcb632d6ce258e4eaa8b53ecac4435740405c89fb5c4cd5250356293b5a405719cbe9614f66d45
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sub_diff (1.0.1)
4
+ sub_diff (1.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://www.rubygems.org/
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)') #=> #<DiffCollection:0x007fc532049508>
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 `DiffCollection` instead. This object behaves just like a `String`.
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
- * `Diff#changed?`
92
- * `Diff#value`
93
- * `Diff#value_was`
94
- * `DiffCollection#changed?`
95
- * `DiffCollection#diffs`
96
- * `DiffCollection#each`
97
- * `DiffCollection#size`
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
 
@@ -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
- extend Forwardable
3
+ def diff(*args, &block)
4
+ builder.diff(*args, &block)
5
+ collection
6
+ end
9
7
 
10
- def_delegators :instance, :diff
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
- Module.nesting.last.const_get(constant)
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(collection, type)
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
@@ -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(*args)
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)
@@ -1,5 +1,3 @@
1
- require 'sub_diff/builder'
2
-
3
1
  module SubDiff
4
2
  module CoreExt
5
3
  module String
data/lib/sub_diff/diff.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'delegate'
2
-
3
1
  module SubDiff
4
2
  class Diff < SimpleDelegator
5
3
  attr_reader :value_was
@@ -1,22 +1,18 @@
1
- require 'forwardable'
2
-
3
1
  module SubDiff
4
- class Differ < Struct.new(:collection, :type)
2
+ class Differ < Struct.new(:builder, :type)
5
3
  extend Forwardable
6
4
 
7
- def_delegators :collection, :string
5
+ def_delegators :builder, :string
8
6
 
9
- def diff(*args)
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(collection, diff)
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
@@ -1,10 +1,8 @@
1
- require 'sub_diff/sub'
2
-
3
1
  module SubDiff
4
2
  class Gsub < Sub
5
3
  private
6
4
 
7
- def diff!(_builder, diff, _search)
5
+ def process(_builder, diff, _search)
8
6
  super
9
7
  last_prefix << prefix(diff) << diff[:match]
10
8
  end
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.diff(*args) do |builder, diff|
8
- diff!(builder, diff, args.first)
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 diff!(builder, diff, search)
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)
@@ -1,3 +1,3 @@
1
1
  module SubDiff
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
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
@@ -10,6 +10,8 @@ rescue LoadError
10
10
  # Ignore when testing with Ruby 1.8.7
11
11
  end
12
12
 
13
+ require File.expand_path('../../lib/sub_diff', __FILE__)
14
+
13
15
  RSpec.configure do |config|
14
16
  config.filter_run :focus
15
17
  config.raise_errors_for_deprecations!
@@ -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
- subject.push('one', 'two')
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
- subject.push('same', 'same')
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 return self' do
31
- expect(subject.push).to eq(subject)
32
- end
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
- block = proc { subject.push('') }
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
@@ -1,5 +1,3 @@
1
- require File.expand_path('../../../lib/sub_diff/diff', __FILE__)
2
-
3
1
  RSpec.describe SubDiff::Diff do
4
2
  let(:diff) { described_class.new(value, value_was) }
5
3
  let(:diff_without_value_was) { described_class.new(value) }
@@ -1,5 +1,3 @@
1
- require File.expand_path('../../lib/sub_diff', __FILE__)
2
-
3
1
  RSpec.describe SubDiff do
4
2
  subject { 'this is a simple test' }
5
3
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sub_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Huber