Class: RealDataTests::PresetConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/real_data_tests/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePresetConfig

Returns a new instance of PresetConfig.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/real_data_tests/configuration.rb', line 69

def initialize
  @association_filter_mode = nil
  @association_filter_list = []
  @model_specific_associations = {}
  @association_limits = {}
  @prevent_reciprocal_loading = {}
  @anonymization_rules = {}
  @prevented_reciprocals = Set.new
  @max_depth = 10
  @max_self_ref_depth = 2
end

Instance Attribute Details

#anonymization_rulesObject (readonly)

Returns the value of attribute anonymization_rules.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def anonymization_rules
  @anonymization_rules
end

#association_filter_listObject (readonly)

Returns the value of attribute association_filter_list.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def association_filter_list
  @association_filter_list
end

#association_filter_modeObject (readonly)

Returns the value of attribute association_filter_mode.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def association_filter_mode
  @association_filter_mode
end

#association_limitsObject (readonly)

Returns the value of attribute association_limits.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def association_limits
  @association_limits
end

#max_depthObject

Returns the value of attribute max_depth.



67
68
69
# File 'lib/real_data_tests/configuration.rb', line 67

def max_depth
  @max_depth
end

#max_self_ref_depthObject

Returns the value of attribute max_self_ref_depth.



67
68
69
# File 'lib/real_data_tests/configuration.rb', line 67

def max_self_ref_depth
  @max_self_ref_depth
end

#model_specific_associationsObject (readonly)

Returns the value of attribute model_specific_associations.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def model_specific_associations
  @model_specific_associations
end

#prevent_reciprocal_loadingObject (readonly)

Returns the value of attribute prevent_reciprocal_loading.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def prevent_reciprocal_loading
  @prevent_reciprocal_loading
end

#prevented_reciprocalsObject (readonly)

Returns the value of attribute prevented_reciprocals.



62
63
64
# File 'lib/real_data_tests/configuration.rb', line 62

def prevented_reciprocals
  @prevented_reciprocals
end

Instance Method Details

#anonymize(model_name, mappings = {}) ⇒ Object



151
152
153
# File 'lib/real_data_tests/configuration.rb', line 151

def anonymize(model_name, mappings = {})
  @anonymization_rules[model_name.to_s] = mappings
end

#exclude_associations(*associations) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/real_data_tests/configuration.rb', line 115

def exclude_associations(*associations)
  if @association_filter_mode == :whitelist
    raise Error, "Cannot set excluded_associations when included_associations is already set"
  end
  @association_filter_mode = :blacklist
  @association_filter_list = associations.flatten
end

#get_association_limit(record_class, association_name) ⇒ Object



132
133
134
135
# File 'lib/real_data_tests/configuration.rb', line 132

def get_association_limit(record_class, association_name)
  path = "#{record_class.name}.#{association_name}"
  @association_limits[path]
end

#get_max_self_ref_depth(model) ⇒ Object



94
95
96
# File 'lib/real_data_tests/configuration.rb', line 94

def get_max_self_ref_depth(model)
  @max_self_ref_depth
end

#has_circular_dependency?(klass, association_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
# File 'lib/real_data_tests/configuration.rb', line 98

def has_circular_dependency?(klass, association_name)
  key = if klass.is_a?(String)
    "#{klass}:#{association_name}"
  else
    "#{klass.name}:#{association_name}"
  end
  @prevented_reciprocals.include?(key)
end

#include_associations(*associations) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/real_data_tests/configuration.rb', line 107

def include_associations(*associations)
  if @association_filter_mode == :blacklist
    raise Error, "Cannot set included_associations when excluded_associations is already set"
  end
  @association_filter_mode = :whitelist
  @association_filter_list = associations.flatten
end

#include_associations_for(model, *associations) ⇒ Object



123
124
125
126
# File 'lib/real_data_tests/configuration.rb', line 123

def include_associations_for(model, *associations)
  model_name = model.is_a?(String) ? model : model.name
  @model_specific_associations[model_name] = associations.flatten
end

#limit_association(path, limit) ⇒ Object



128
129
130
# File 'lib/real_data_tests/configuration.rb', line 128

def limit_association(path, limit)
  @association_limits[path.to_s] = limit
end

#prevent_circular_dependency(klass, association_name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/real_data_tests/configuration.rb', line 81

def prevent_circular_dependency(klass, association_name)
  key = if klass.is_a?(String)
    "#{klass}:#{association_name}"
  else
    "#{klass.name}:#{association_name}"
  end
  @prevented_reciprocals << key
end

#prevent_reciprocal(path) ⇒ Object



147
148
149
# File 'lib/real_data_tests/configuration.rb', line 147

def prevent_reciprocal(path)
  @prevent_reciprocal_loading[path.to_s] = true
end

#prevent_reciprocal?(record_class, association_name) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/real_data_tests/configuration.rb', line 142

def prevent_reciprocal?(record_class, association_name)
  path = "#{record_class.name}.#{association_name}"
  @prevent_reciprocal_loading[path] || has_circular_dependency?(record_class, association_name)
end

#set_association_limit(model_name, association_name, limit) ⇒ Object



137
138
139
140
# File 'lib/real_data_tests/configuration.rb', line 137

def set_association_limit(model_name, association_name, limit)
  path = "#{model_name}.#{association_name}"
  @association_limits[path] = limit
end

#should_process_association?(model, association_name) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/real_data_tests/configuration.rb', line 155

def should_process_association?(model, association_name)
  model_name = model.is_a?(Class) ? model.name : model.class.name

  if @model_specific_associations.key?(model_name)
    return @model_specific_associations[model_name].include?(association_name)
  end

  case @association_filter_mode
  when :whitelist
    @association_filter_list.include?(association_name)
  when :blacklist
    !@association_filter_list.include?(association_name)
  else
    true
  end
end