Class: Anonymise::DbFaker

Inherits:
Object
  • Object
show all
Includes:
DbConnection
Defined in:
lib/anonymise/db_faker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DbConnection

#column_exists?, #connection, #t_ables

Constructor Details

#initialize(db_args, config, dry_run = false) ⇒ DbFaker

Returns a new instance of DbFaker.



10
11
12
13
14
# File 'lib/anonymise/db_faker.rb', line 10

def initialize(db_args, config, dry_run = false)
  @db_args = db_args
  @config = config
  @dry_run = dry_run
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/anonymise/db_faker.rb', line 9

def config
  @config
end

#db_argsObject (readonly)

Returns the value of attribute db_args.



9
10
11
# File 'lib/anonymise/db_faker.rb', line 9

def db_args
  @db_args
end

Instance Method Details

#fakeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/anonymise/db_faker.rb', line 16

def fake
  config.each do |table, columns|
    if t_ables.include?(table)
      puts "Anonymising table #{table}".colorize(:green)
      columns.each do |column, type|
        if column_exists?(table, column)
          print("...#{fake_column(column, table, type)} records affected for column #{column}")
        else
          print "...column #{column} does not exist in #{table}_table".colorize(:red)
        end
        print "\n"
      end
      print "\n"
    else
      puts "Table #{table} does not exist on #{db_args[:db]}".colorize(:red)
    end
  end

  connection&.close
end

#get_fake_for_type(type, origin_length) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/anonymise/db_faker.rb', line 37

def get_fake_for_type(type, origin_length)
  val = Faker::Alphanumeric.unique.alpha(number: origin_length)
  val = val.gsub(/[^a-z ]/, '')

  val = Faker::Company.name if type == 'company'
  val = Faker::Name.last_name if type == 'lastname'
  val = Faker::Name.first_name if type == 'firstname'
  if type == 'description'
    val = Faker::Lorem.paragraph_by_chars(number: 256, supplemental: false)
  end
  val = Faker::Internet.unique.email if type == 'email'
  val = Faker::Internet.unique.url if type == 'url'
  val = Faker::PhoneNumber.unique.cell if type == 'mobile'
  if type == 'number'
    val = Faker::Faker::Number.unique.number(digits: origin_length)
  end
  val.gsub(/'/, '')
end