lib/anony/strategies/overwrite.rb
# 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.
#
# @param [Proc, Object] strategy Any object which responds to
#   `.call(previous_value)`. Not used if a block is provided.
# @param [Array<Symbol>] fields A list of one or more fields to apply this strategy to.
# @param [Block] &block A block to use as the strategy.
# @yieldparam previous [Object] The previous value of the field
# @yieldreturn [Object] The value to set on that field.
# @raise [ArgumentError] If the combination of strategy, fields and block is invalid.
# @raise [DuplicateStrategyException] If more than one strategy is defined for the same field.
#
# @example With a named class
#   class Reverse
#     def self.call(previous)
#       previous.reverse
#     end
#   end
#
#   with_strategy(Reverse, :first_name)
#
# @example With a constant value
#   with_strategy({}, :metadata)
#
# @example With a block
#   with_strategy(:first_name, :last_name) { |previous| previous.reverse }
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.
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 .
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
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
Configure a custom strategy for one or more fields.
Configure a custom strategy for one or more fields .
The value to set on that field.
The value to set on that field .
Object
[ArgumentError] If the combination of strategy, fields and block is invalid.
[ ArgumentError ] If the combination of strategy fields and block is invalid .
[DuplicateStrategyException] If more than one strategy is defined for the same field.
[ DuplicateStrategyException ] If more than one strategy is defined for the same field .
strategy
<not_specific>
fields
<not_specific>
block
<not_specific>
<not_specific>
█ A block to use as the strategy.
█ A block to use as the strategy .
previous
<not_specific>
The previous value of the field
The previous value of the field
example
With a named class class Reverse def self.call(previous) previous.reverse end end with_strategy(Reverse, :first_name)
With a named class class Reverse def self . call ( previous ) previous . reverse end end with_strategy ( Reverse : first_name )
example
With a constant value with_strategy({}, :metadata)
With a constant value with_strategy ( {} : metadata )
example
With a block with_strategy(:first_name, :last_name) { |previous| previous.reverse }
With a block with_strategy ( : first_name : last_name ) { |previous| previous . reverse }