Class: Anony::Strategies::Overwrite

Inherits:
Object
  • Object
show all
Includes:
FieldLevelStrategies
Defined in:
lib/anony/strategies/overwrite.rb

Overview

The interface for configuring a field-level strategy. All of the methods here are made available inside the ‘overwrite { … }` block:

Examples:

anonymise do
  overwrite do
    nilable :first_name
    email :email_address
    with_strategy(:last_name) { "last-#{id}" }
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldLevelStrategies

[], #current_datetime, #email, #nilable, #noop, #phone_number, register

Instance Attribute Details

#anonymisable_fieldsObject (readonly)

A hash containing the fields and their anonymisation strategies.



29
30
31
# File 'lib/anony/strategies/overwrite.rb', line 29

def anonymisable_fields
  @anonymisable_fields
end

Instance Method Details

#apply(instance) ⇒ Object

Apply the Overwrite strategy on the model instance, which applies each of the configured transformations and updates the :anonymised_at field if it exists.

Parameters:

  • instance (ActiveRecord::Base)

    An instance of the model



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anony/strategies/overwrite.rb', line 47

def apply(instance)
  if !@anonymisable_fields.key?(:anonymised_at) &&
      @model_class.column_names.include?("anonymised_at")
    current_datetime(:anonymised_at)
  end

  @anonymisable_fields.each_key do |field|
    anonymise_field(instance, field)
  end

  result_fields = instance.changes.keys.map(&:to_sym).reject { |s| s == :anonymised_at }

  instance.save!

  Result.overwritten(result_fields, instance)
end

#hex(*fields, max_length: 36) ⇒ Object

Helper method to use the :hex strategy

Examples:

hex :first_name

Parameters:

  • fields (Array<Symbol>)

    A list of one or more fields to apply this strategy to.

See Also:



112
113
114
# File 'lib/anony/strategies/overwrite.rb', line 112

def hex(*fields, max_length: 36)
  with_strategy(Strategies::OverwriteHex.new(max_length), *fields)
end

#ignore(*fields) ⇒ Object

Configure a list of fields that you don’t want to anonymise.

Examples:

ignore :external_system_id, :externalised_at

Parameters:

  • fields (Array<Symbol>)

    The fields to ignore

Raises:

  • (ArgumentError)

    If trying to ignore a field which is already globally ignored in Anony::Config.ignores



124
125
126
127
128
129
130
131
132
133
# File 'lib/anony/strategies/overwrite.rb', line 124

def ignore(*fields)
  already_ignored = fields.select { |field| Config.ignore?(field) }

  if already_ignored.any?
    raise ArgumentError, "Cannot ignore #{already_ignored.inspect} " \
                         "(fields already ignored in Anony::Config)"
  end

  no_op(*fields)
end

#valid?Boolean

Check whether the combination of field-level rules is valid

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/anony/strategies/overwrite.rb', line 32

def valid?
  validate!
  true
rescue FieldException
  false
end

#validate!Object

Raises:



39
40
41
# File 'lib/anony/strategies/overwrite.rb', line 39

def validate!
  raise FieldException, unhandled_fields if unhandled_fields.any?
end

#with_strategy(strategy, *fields) {|previous| ... } ⇒ Object

Configure a custom strategy for one or more fields. If a block is given that is used as the strategy, otherwise the first argument is used as the strategy.

Examples:

With a named class

class Reverse
  def self.call(previous)
    previous.reverse
  end
end

with_strategy(Reverse, :first_name)

With a constant value

with_strategy({}, :metadata)

With a block

with_strategy(:first_name, :last_name) { |previous| previous.reverse }

Parameters:

  • strategy (Proc, Object)

    Any object which responds to ‘.call(previous_value)`. Not used if a block is provided.

  • fields (Array<Symbol>)

    A list of one or more fields to apply this strategy to.

  • &block (Block)

    A block to use as the strategy.

Yield Parameters:

  • previous (Object)

    The previous value of the field

Yield Returns:

  • (Object)

    The value to set on that field.

Raises:

  • (ArgumentError)

    If the combination of strategy, fields and block is invalid.

  • (DuplicateStrategyException)

    If more than one strategy is defined for the same field.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/anony/strategies/overwrite.rb', line 90

def with_strategy(strategy, *fields, &block)
  if block
    fields.unshift(strategy)
    strategy = block
  end

  fields = fields.flatten

  raise ArgumentError, "Block or Strategy object required" unless strategy
  raise ArgumentError, "One or more fields required" unless fields.any?

  guard_duplicate_strategies!(fields)

  fields.each { |field| @anonymisable_fields[field] = strategy }
end