Module: Anony::FieldLevelStrategies

Included in:
Strategies::Overwrite
Defined in:
lib/anony/field_level_strategies.rb

Overview

This class is a singleton, containing all of the known strategies that Anony can use to anonymise individual fields in your models.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Helper method for retrieving the strategy block (or testing that it exists).

Parameters:

  • name (Symbol)

    The name of the strategy to retrieve.

Raises:

  • (ArgumentError)

    If the strategy is not already registered



66
67
68
69
70
# File 'lib/anony/field_level_strategies.rb', line 66

def self.[](name)
  @strategies.fetch(name) do
    raise ArgumentError, "Unrecognised strategy `#{name.inspect}`"
  end
end

.register(name, klass_or_constant = nil) {|original_value| ... } ⇒ Object

Registers a new Anony strategy (or overwrites an existing strategy) of a given name. Strategies are then available everywhere inside the ‘anonymise` block.

Examples:

Reversing a string using a block

Anony::FieldLevelStrategies.register(:reverse) do |original_value|
  original_value.reverse
end

class Manager
  anonymise { reverse :first_name }
end

Using a named strategy class

class Classifier
  def self.call(original_value)
    "Classy version of #{original_value}"
  end
end

Anony::FieldLevelStrategies.register(:classify, Classifier)

class Manager
  anonymise { classify :resource_type }
end

Using a constant value

Anony::FieldLevelStrategies.register(:forty_two, 42)

class Manager
  anonymise { forty_two :date_of_birth }
end

Parameters:

  • name (Symbol)

    The name of the strategy you’d like to use

  • klass_or_constant (Object) (defaults to: nil)

    The object you’d like to statically return, or an object which responds to ‘#call(original_value)`.

Yields:

  • (original_value)

    The previous value of the field. The result of the block will be applied to that field. If a block is not given, klass_or_constant will be used as the strategy instead.

Raises:

  • (ArgumentError)

    If using neither a block nor strategy class



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/anony/field_level_strategies.rb', line 48

def self.register(name, klass_or_constant = nil, &block)
  if block
    strategy = block
  elsif !klass_or_constant.nil?
    strategy = klass_or_constant
  else
    raise ArgumentError, "Must pass either a block, constant value or strategy class"
  end

  define_method(name) { |*fields| with_strategy(strategy, *fields) }

  @strategies[name] = strategy
end

Instance Method Details

#current_datetime(field) ⇒ Object

Overwrite a field with the current datetime. This is provided by ‘current_time_from_proper_timezone`, an internal method exposed from ActiveRecord::Timestamp.

Examples:

Overwriting the field called :signed_up_at

current_datetime :signed_up_at


105
# File 'lib/anony/field_level_strategies.rb', line 105

register(:current_datetime) { |_original| current_time_from_proper_timezone }

#email(field) ⇒ Object

Overwrite a field with a randomised email, where the user part is generated with ‘SecureRandom.uuid` and the domain is “example.com”.

For example, this might generate an email like: ‘“[email protected]”`

Examples:

Overwriting the field called :email_address

email :email_address


83
84
85
# File 'lib/anony/field_level_strategies.rb', line 83

register(:email) do
  sprintf("%<random>[email protected]", random: SecureRandom.uuid)
end

#nilable(field) ⇒ Object

Overwrite a field with the value ‘nil`.

Examples:

Overwriting the field called :optional_field

nilable :optional_field


112
# File 'lib/anony/field_level_strategies.rb', line 112

register(:nilable) { nil }

#noop(field) ⇒ Object

This strategy applies no transformation rules at all. It is used internally by the ignore strategy so you should probably use that instead.

See Also:

  • Strategies::Fields#ignore


119
# File 'lib/anony/field_level_strategies.rb', line 119

register(:no_op) { |value| value }

#phone_number(field) ⇒ Object

Overwrite a field with a static phone number. Currently this is “+1 617 555 1294” but you would probably want to override this for your use case.

Examples:

Overwriting the field called :phone

phone_number :phone

Using a different phone number

Anony::FieldLevelStrategies.register(:phone_number, "+44 07700 000 000")


96
# File 'lib/anony/field_level_strategies.rb', line 96

register(:phone_number, "+1 617 555 1294")