Class: Anony::ModelConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/anony/model_config.rb

Defined Under Namespace

Classes: UndefinedStrategy

Instance Method Summary collapse

Constructor Details

#initialize(model_class) {|block| ... } ⇒ ModelConfig

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Constructs a new instance of ModelConfig.

Examples:

Anony::ModelConfig.new(Manager) { destroy }

Parameters:

  • model_class (ActiveRecord::Base)

    The model class the config is attached to.

Yields:

  • (block)

    For configuration of the ModelConfig instance.



29
30
31
32
33
34
# 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

Instance Method Details

#apply(instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Anony::ModelConfig.new(Manager).apply(Manager.new)


41
42
43
44
45
# 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

#destroyObject

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

This method takes no arguments or blocks.

Examples:

anonymise do
  destroy
end

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
# 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) ⇒ Object

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.

Examples:

anonymise do
  overwrite do
    hex :first_name
  end
end

See Also:



81
82
83
84
85
86
87
# 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) ⇒ Object

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

Examples:

anonymise do
  skip_if { !persisted? }
end

Raises:

  • (ArgumentError)


96
97
98
99
100
# 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