Sha256: f60e6d58af9e2ef935656bfbd6e36d43cd68613de237b1a7369c5369ef2f302a

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

module SubDiff
  # 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
  # {Collection#diffs} - an {Array} containing each {Diff}.
  #
  # @api public
  class Collection < SimpleDelegator
    extend Forwardable
    include Enumerable

    def_delegators :diffs, :each, :size

    attr_reader :string, :diffs

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

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

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sub_diff-1.1.1 lib/sub_diff/collection.rb