Class: Omelettes::Obfuscate

Inherits:
Object
  • Object
show all
Defined in:
lib/omelettes/obfuscate.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ignore_columnsObject

Returns the value of attribute ignore_columns.



91
92
93
# File 'lib/omelettes/obfuscate.rb', line 91

def ignore_columns
  @ignore_columns
end

.ignore_tablesObject

Returns the value of attribute ignore_tables.



90
91
92
# File 'lib/omelettes/obfuscate.rb', line 90

def ignore_tables
  @ignore_tables
end

.modelsObject

Returns the value of attribute models.



93
94
95
# File 'lib/omelettes/obfuscate.rb', line 93

def models
  @models
end

.word_listObject

Returns the value of attribute word_list.



92
93
94
# File 'lib/omelettes/obfuscate.rb', line 92

def word_list
  @word_list
end

Class Method Details

.cleanup(&callback) ⇒ Object



79
80
81
# File 'lib/omelettes/obfuscate.rb', line 79

def cleanup(&callback)
  @callback = callback
end

.columns_for_table(table) ⇒ Object



74
75
76
77
# File 'lib/omelettes/obfuscate.rb', line 74

def columns_for_table(table)
  @columns_for_table ||= {}
  @columns_for_table[table] ||= model(table).columns.select {|column| !ignore_column?(column.name) && (column.type == :string || column.type == :text)}.map(&:name)
end

.cook(silent = false) ⇒ Object



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
44
45
46
# File 'lib/omelettes/obfuscate.rb', line 4

def cook(silent=false)
  total_tables = 0
  total_attributes = 0
  Words.load(word_list || "/usr/share/dict/words")
  processed = []
  tables.each do |table|
    next if ignore_table?(table)
    processed << table
    pbar = ProgressBar.new(model(table).name, model(table).count) unless silent
    model(table).find_each do |object|
      tries = 0
      begin
        object.obfuscate(columns_for_table(table))
        total_attributes += 1
      rescue => e
        tries += 1
        retry if tries <= 1
        puts e.message
        next
      ensure
        pbar.inc unless silent
      end
    end
    pbar.finish unless silent
    total_tables += 1
  end
  if @callback
    @callback.call
  end
  unless silent
    puts " Obfuscation Report (the following tables and columns were processed)"
    puts ("----------" * 8)
    processed.each do |table|
      label = model(table).name
      columns_for_table(table).join(',').scan(/.{0,60}/).each do |columns|
        puts "%20.20s | %-60.60s" % [label, columns] unless columns.blank?
        label = ""
      end
    end
  end

  [total_tables, total_attributes]
end

.ignore_column?(column) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/omelettes/obfuscate.rb', line 67

def ignore_column?(column)
  ignore_columns.each do |ignore|
    return true if column.match(ignore).to_s == column
  end
  false
end

.ignore_table?(table) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/omelettes/obfuscate.rb', line 58

def ignore_table?(table)
  ignore_tables.each do |ignore|
    return true if table.match(ignore).to_s == table
  end
  return true unless columns_for_table(table).any?
  return true if model(table).count == 0
  false
end

.model(table) ⇒ Object



52
53
54
55
56
# File 'lib/omelettes/obfuscate.rb', line 52

def model(table)
  self.models ||= {}
  self.models[table] ||= table.camelcase.singularize.constantize
  self.models[table]
end

.obfuscate(string) ⇒ Object



83
84
85
86
87
88
# File 'lib/omelettes/obfuscate.rb', line 83

def obfuscate(string)
  return nil if string.nil?
  string.split(/(\s+)|([[:punct:]])|(\d+)/).map do |word|
    word.match(/[a-zA-Z]+/).nil? ? word : Words.replace(word)
  end.join("")
end

.tablesObject



48
49
50
# File 'lib/omelettes/obfuscate.rb', line 48

def tables
  ActiveRecord::Base.connection.tables
end