Class: RealDataTests::DataAnonymizer

Inherits:
Object
  • Object
show all
Defined in:
lib/real_data_tests/data_anonymizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(preset_config) ⇒ DataAnonymizer

Returns a new instance of DataAnonymizer.



6
7
8
# File 'lib/real_data_tests/data_anonymizer.rb', line 6

def initialize(preset_config)
  @preset_config = preset_config
end

Instance Method Details

#anonymize_record(record) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/real_data_tests/data_anonymizer.rb', line 16

def anonymize_record(record)
  return record unless should_anonymize?(record)

  anonymization_rules = @preset_config.anonymization_rules[record.class.name]
  anonymization_rules.each do |attribute, anonymizer|
    begin
      new_value = case anonymizer
                 when String
                   process_faker_string(anonymizer)
                 when Proc, Lambda
                   anonymizer.call(record)
                 else
                   raise Error, "Unsupported anonymizer type: #{anonymizer.class}"
                 end
      record.send("#{attribute}=", new_value)
    rescue => e
      raise Error, "Failed to anonymize #{attribute} using #{anonymizer.inspect}: #{e.message}"
    end
  end
  record
end

#anonymize_records(records) ⇒ Object



10
11
12
13
14
# File 'lib/real_data_tests/data_anonymizer.rb', line 10

def anonymize_records(records)
  records.map do |record|
    anonymize_record(record)
  end
end