class Anony::ModelConfig

Public Class Methods

new(model_class, &block) click to toggle source

@api private Constructs a new instance of ModelConfig.

@param [ActiveRecord::Base] model_class The model class the config is attached to. @yield [block] For configuration of the ModelConfig instance.

@example

Anony::ModelConfig.new(Manager) { destroy }
# File lib/anony/model_config.rb, line 29
def initialize(model_class, &block)
  @model_class = model_class
  @strategy = UndefinedStrategy.new
  @skip_filter = nil
  instance_exec(&block) if block
end

Public Instance Methods

apply(instance) click to toggle source

@api private Applies the given strategy, taking into account any filters or conditions.

@example

Anony::ModelConfig.new(Manager).apply(Manager.new)
# File lib/anony/model_config.rb, line 41
def apply(instance)
  return Result.skipped if @skip_filter && instance.instance_exec(&@skip_filter)

  @strategy.apply(instance)
end
destroy() click to toggle source

Use the deletion strategy instead of anonymising individual fields. This method is incompatible with the fields strategy.

This method takes no arguments or blocks.

@example

anonymise do
  destroy
end
# File lib/anony/model_config.rb, line 58
def destroy
  raise ArgumentError, ":destroy takes no block" if block_given?
  unless @strategy.is_a?(UndefinedStrategy)
    raise ArgumentError, "Cannot specify :destroy when another strategy already defined"
  end

  @strategy = Strategies::Destroy.new
end
overwrite(&block) click to toggle source

Use the overwrite strategy to configure rules for individual fields. This method is incompatible with the destroy strategy.

This method takes a configuration block. All configuration is applied to Anony::Strategies::Overwrite.

@see Anony::Strategies::Overwrite

@example

anonymise do
  overwrite do
    hex :first_name
  end
end
# File lib/anony/model_config.rb, line 81
def overwrite(&block)
  unless @strategy.is_a?(UndefinedStrategy)
    raise ArgumentError, "Cannot specify :overwrite when another strategy already defined"
  end

  @strategy = Strategies::Overwrite.new(@model_class, &block)
end
skip_if(&if_condition) click to toggle source

Prevent any anonymisation strategy being applied when the provided block evaluates to true. The block is executed in the model context.

@example

anonymise do
  skip_if { !persisted? }
end
# File lib/anony/model_config.rb, line 96
def skip_if(&if_condition)
  raise ArgumentError, "Block required for :skip_if" unless if_condition

  @skip_filter = if_condition
end