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.



30
31
32
33
34
35
36
# File 'lib/anony/model_config.rb', line 30

def initialize(model_class, &block)
  @model_class = model_class
  @strategy = UndefinedStrategy.new
  @selectors_config = nil
  @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)


43
44
45
46
47
# File 'lib/anony/model_config.rb', line 43

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)


61
62
63
64
65
66
67
68
# File 'lib/anony/model_config.rb', line 61

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:



84
85
86
87
88
89
90
# File 'lib/anony/model_config.rb', line 84

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

#selector_for?(subject) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/anony/model_config.rb', line 110

def selector_for?(subject)
  return false if @selectors_config.nil?

  @selectors_config.selectors[subject].present?
end

#selectors(&block) ⇒ Object

Define selectors to select records that apply to a particular subject. This method taks a configuration block that then builds Selectors

Examples:


anonymise do
  selectors do
    for_subject(:user_id) { |user_id| self.select_for_user(user_id) }
  end
end

ModelName.anonymise_for!(:user_id, "user_1234")

See Also:



106
107
108
# File 'lib/anony/model_config.rb', line 106

def selectors(&block)
  @selectors_config = Selectors.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)


123
124
125
126
127
# File 'lib/anony/model_config.rb', line 123

def skip_if(&if_condition)
  raise ArgumentError, "Block required for :skip_if" unless if_condition

  @skip_filter = if_condition
end