Class: DumpCleaner::Cleanup::CleaningSteps::RandomizeFormattedNumber

Inherits:
Base
  • Object
show all
Includes:
Inspection
Defined in:
lib/dump_cleaner/cleanup/cleaning_steps/randomize_formatted_number.rb

Instance Attribute Summary

Attributes inherited from Base

#step_context

Instance Method Summary collapse

Methods included from Inspection

#inspect_step_context, #subset, #truncate

Methods inherited from Base

#crc32, #initialize, #raise_params_error

Constructor Details

This class inherits a constructor from DumpCleaner::Cleanup::CleaningSteps::Base

Instance Method Details

#run(format:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dump_cleaner/cleanup/cleaning_steps/randomize_formatted_number.rb', line 9

def run(format:)
  regex = Regexp.new("\\A#{format}\\z")

  unless regex.names.any? { _1.start_with?("x") }
    raise_params_error('The format has no named group starting with \'x\', e.g. \'(?<x>\d)\')')
  end

  unless current_value.match?(regex)
    if repetition.zero?
      Log.warn { "Invalid value: type=#{type}, id=#{record['id']}, value=#{truncate(current_value)}" }
    end
    step_context.current_value = nil
    return step_context
  end

  random = Random.new(crc32)
  new_value = randomize_named_captures(regex:, random:)

  if new_value.length != current_value.length
    raise ArgumentError, "The new value length does not match the original value length.
                          Do the named groups in the format regexp match the whole value?".gsub(/\s+/, " ")
  end

  step_context.current_value = new_value
  step_context
end