class Anonymous::Anonymizer

Attributes

anonymization_definitions[R]
attributes[R]

Public Class Methods

new(attributes, anonymization_definitions) click to toggle source
# File lib/anonymous/anonymizer.rb, line 5
def initialize(attributes, anonymization_definitions)
  @attributes = symbolize_keys(attributes)
  @anonymization_definitions = symbolize_keys(anonymization_definitions)
  @anonymization_attempts = 0
end

Public Instance Methods

anonymized_attributes() click to toggle source
# File lib/anonymous/anonymizer.rb, line 11
def anonymized_attributes
  attributes_to_anonymise = non_nil_attributes

  attributes_to_anonymise.each_with_object({}) do |(attr_name, value), result|
    anonymization_definition = anonymization_definitions.fetch(attr_name)

    if anonymization_definition.respond_to?(:call)
      result[attr_name] = anonymization_definition.call(value)
    else
      result[attr_name] = anonymization_definition
    end
  end
end

Private Instance Methods

non_nil_attributes() click to toggle source
# File lib/anonymous/anonymizer.rb, line 29
def non_nil_attributes
  @non_nil_attributes ||= attributes.select do |attr_name, value|
    !value.nil? && anonymization_definitions[attr_name]
  end
end
symbolize_keys(hash) click to toggle source
# File lib/anonymous/anonymizer.rb, line 35
def symbolize_keys(hash)
  hash.each_with_object({}) do |(key, value), obj|
    key = key.to_sym rescue key
    obj[key] = value
  end
end