Class: DataAnon::Strategy::Field::RandomFormattedString

Inherits:
Object
  • Object
show all
Defined in:
lib/strategy/field/string/random_formatted_string.rb

Overview

Keeping the format same it changes each digit with random digit, character with character preserving the case.

anonymize('PhoneNumber').using FieldStrategy::RandomFormattedString.new
anonymize('Email').using FieldStrategy::RandomFormattedString.new

Constant Summary collapse

SMALL_CHARS =
"abcdefghjkmnpqrstuvwxyz"
CAPS_CHARS =
"ABCDEFGHJKLMNPQRSTUVWXYZ"

Instance Method Summary collapse

Instance Method Details

#anonymize(field) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/strategy/field/string/random_formatted_string.rb', line 16

def anonymize field
  @original_string = field.value
  @anonymized_string = ""
  @original_string.each_char do |char|
    if /\d/.match(char)
      @anonymized_string += DataAnon::Utils::RandomInt.generate(0, 9).to_s
    elsif /[a-z]/.match(char)
      @anonymized_string += SMALL_CHARS[rand(SMALL_CHARS.length)]
    elsif /[A-Z]/.match(char)
      @anonymized_string += CAPS_CHARS[rand(CAPS_CHARS.length)]
    else
      @anonymized_string += char
    end
  end

  @anonymized_string
end