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
11
# File 'lib/kitchen/id_tracker.rb', line 8

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

Instance Method Details

#modified_id_to_paste(original_id) ⇒ String

Returns a unique ID given the ID of an element that was copied and is about to be pasted

Parameters:

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitchen/id_tracker.rb', line 54

def modified_id_to_paste(original_id)
  return nil if original_id.nil?
  return '' if original_id.blank?

  count = @id_data[original_id][:count]
  # A count of 0 means the element was cut and this is the first paste, do not
  # modify the ID; otherwise, use the uniquified ID.
  if count.zero?
    original_id
  else
    "#{original_id}#{@id_copy_suffix}#{count}"
  end
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:



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

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:



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

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:



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

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