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
-
.[](name) ⇒ Object
Helper method for retrieving the strategy block (or testing that it exists).
-
.register(name, klass_or_constant = nil) {|original_value| ... } ⇒ Object
Registers a new Anony strategy (or overwrites an existing strategy) of a given name.
Instance Method Summary collapse
-
#current_datetime(field) ⇒ Object
Overwrite a field with the current datetime.
-
#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”.
-
#nilable(field) ⇒ Object
Overwrite a field with the value ‘nil`.
-
#noop(field) ⇒ Object
This strategy applies no transformation rules at all.
-
#phone_number(field) ⇒ Object
Overwrite a field with a static phone number.
Class Method Details
.[](name) ⇒ Object
Helper method for retrieving the strategy block (or testing that it exists).
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.
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.
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]”`
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`.
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.
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.
96 |
# File 'lib/anony/field_level_strategies.rb', line 96 register(:phone_number, "+1 617 555 1294") |