Class: Kitchen::IdTracker

Inherits:
Object show all
Defined in:
lib/kitchen/id_tracker.rb

Overview

A class to track and modify duplicate IDs in a document

Instance Method Summary collapse

Constructor Details

#initializeIdTracker

Returns a new instance of IdTracker.



8
9
10
# File 'lib/kitchen/id_tracker.rb', line 8

def initialize
  @id_data = Hash.new { |hash, key| hash[key] = { count: 0, last_pasted: false } }
end

Instance Method Details

#first_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/kitchen/id_tracker.rb', line 47

def first_id?(id)
  return nil if id.nil?

  @id_data[id][:count].zero?
end

#record_id_copied(id) ⇒ Object

Keeps track that an element with the given ID has been copied. When such elements are pasted, this information is used to give those elements unique IDs that don’t duplicate the original element.

Parameters:



18
19
20
21
22
23
# File 'lib/kitchen/id_tracker.rb', line 18

def record_id_copied(id)
  return if id.blank?

  @id_data[id][:count] += 1
  @id_data[id][:last_pasted] = false
end

#record_id_cut(id) ⇒ Object

Keeps track that an element with the given ID has been cut.

Parameters:



29
30
31
32
33
34
# File 'lib/kitchen/id_tracker.rb', line 29

def record_id_cut(id)
  return if id.blank?

  @id_data[id][:count] -= 1 if @id_data[id][:count].positive?
  @id_data[id][:last_pasted] = false
end

#record_id_pasted(id) ⇒ Object

Keeps track that an element with the given ID has been pasted.

Parameters:



40
41
42
43
44
45
# File 'lib/kitchen/id_tracker.rb', line 40

def record_id_pasted(id)
  return if id.blank?

  @id_data[id][:count] += 1 if @id_data[id][:last_pasted]
  @id_data[id][:last_pasted] = true
end