Class: SubDiff::Collection

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/sub_diff/collection.rb

Overview

Stores a collection of Diff objects for all matches from a String#sub_diff or String#gsub_diff replacement.

It behaves like a String that represents the entire replacement result from String#sub or String#gsub.

It also behaves like an Enumerable by delegating to #diffs - an Array containing each Diff.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Collection

Returns a new instance of Collection


20
21
22
23
24
# File 'lib/sub_diff/collection.rb', line 20

def initialize(string)
  @string = string
  @diffs = []
  super(string)
end

Instance Attribute Details

#diffsObject (readonly)


18
19
20
# File 'lib/sub_diff/collection.rb', line 18

def diffs
  @diffs
end

#stringObject (readonly)


18
19
20
# File 'lib/sub_diff/collection.rb', line 18

def string
  @string
end

Instance Method Details

#changed?Boolean


26
27
28
# File 'lib/sub_diff/collection.rb', line 26

def changed?
  diffs.any?(&:changed?)
end

#clearObject


30
31
32
33
34
# File 'lib/sub_diff/collection.rb', line 30

def clear
  diffs.clear
  __setobj__('')
  self
end

#push(diff) ⇒ Object


36
37
38
39
40
41
# File 'lib/sub_diff/collection.rb', line 36

def push(diff)
  unless diff.empty?
    diffs << diff
    __setobj__(diffs.join)
  end
end

#resetObject


43
44
45
46
47
48
# File 'lib/sub_diff/collection.rb', line 43

def reset
  clear
  __setobj__(string)
  yield if block_given?
  self
end