4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/tidus/strategies/postgresql/cond_anonymizer.rb', line 4
def self.anonymize(table_name, column_name, options = {})
name = "#{table_name}.#{column_name}"
options = options.dup
options[:result_type] ||= "text"
type = options[:result_type]
if options[:default] && options[:default_strategy]
raise "CondAnonymizer: Only one of default and default_strategy can be used"
end
if options[:conditions].blank?
raise "Missing option :conditions for CondAnonymizer on #{name}"
elsif options[:conditions].kind_of?(Array)
conditions = options[:conditions]
else
conditions = [options[:conditions]]
end
command = "CASE "
conditions.each do |cond|
raise ":column for condition must be set" if cond[:column].blank?
raise ":value for condition must be set" if cond[:value].nil?
raise ":result for condition must be set" if cond[:result].nil?
cond_column = cond[:column]
cond_value = cond[:value]
cond_type = cond[:type] || "text"
comparator = cond[:comparator] || "="
cond_result = cond[:result]
command += "WHEN ((#{table_name}.#{cond_column})::#{cond_type} #{comparator} " +
"'#{cond_value}'::#{cond_type}) THEN '#{cond_result}'::#{type} "
end
command += "ELSE #{self.default(table_name, column_name, options)} END"
return command
end
|