Sha256: a4bd19f6708ed314b668ba09ca171263663b4dd8f5c728bf793d5e5e0d7059c2

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

# lib/real_data_tests/data_anonymizer.rb
require 'faker'

module RealDataTests
  class DataAnonymizer
    def initialize(preset_config)
      @preset_config = preset_config
    end

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

    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

    private

    def should_anonymize?(record)
      @preset_config.anonymization_rules.key?(record.class.name)
    end

    def process_faker_string(faker_method)
      faker_class, faker_method = faker_method.split('::')[1..].join('::').split('.')
      faker_class = Object.const_get("Faker::#{faker_class}")
      faker_class.send(faker_method)
    rescue => e
      raise Error, "Failed to process Faker method '#{faker_method}': #{e.message}"
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
real_data_tests-0.3.17 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.16 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.15 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.14 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.13 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.12 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.11 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.10 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.9 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.8 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.7 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.6 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.5 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.4 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.3 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.2 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.1 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.3.0 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.2.1 lib/real_data_tests/data_anonymizer.rb
real_data_tests-0.2.0 lib/real_data_tests/data_anonymizer.rb