Class: Anony::ModelConfig
- Inherits:
-
Object
- Object
- Anony::ModelConfig
- Defined in:
- lib/anony/model_config.rb
Defined Under Namespace
Classes: UndefinedStrategy
Instance Method Summary collapse
-
#apply(instance) ⇒ Object
private
Applies the given strategy, taking into account any filters or conditions.
-
#destroy ⇒ Object
Use the deletion strategy instead of anonymising individual fields.
-
#initialize(model_class) {|block| ... } ⇒ ModelConfig
constructor
private
Constructs a new instance of ModelConfig.
-
#overwrite(&block) ⇒ Object
Use the overwrite strategy to configure rules for individual fields.
- #selector_for?(subject) ⇒ Boolean
-
#selectors(&block) ⇒ Object
Define selectors to select records that apply to a particular subject.
-
#skip_if(&if_condition) ⇒ Object
Prevent any anonymisation strategy being applied when the provided block evaluates to true.
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.
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.
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 |
#destroy ⇒ Object
Use the deletion strategy instead of anonymising individual fields. This method is incompatible with the fields strategy.
This method takes no arguments or blocks.
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.
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
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
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.
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 |