Class: Anony::Strategies::Overwrite
- Inherits:
-
Object
- Object
- Anony::Strategies::Overwrite
- 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:
Instance Attribute Summary collapse
-
#anonymisable_fields ⇒ Object
readonly
A hash containing the fields and their anonymisation strategies.
Instance Method Summary collapse
-
#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.
-
#hex(*fields, max_length: 36) ⇒ Object
Helper method to use the :hex strategy.
-
#ignore(*fields) ⇒ Object
Configure a list of fields that you don’t want to anonymise.
-
#valid? ⇒ Boolean
Check whether the combination of field-level rules is valid.
- #validate! ⇒ Object
-
#with_strategy(strategy, *fields) {|previous| ... } ⇒ Object
Configure a custom strategy for one or more fields.
Methods included from FieldLevelStrategies
[], #current_datetime, #email, #nilable, #noop, #phone_number, register
Instance Attribute Details
#anonymisable_fields ⇒ Object (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.
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
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.
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
32 33 34 35 36 37 |
# File 'lib/anony/strategies/overwrite.rb', line 32 def valid? validate! true rescue FieldException false end |
#validate! ⇒ Object
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.
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 |