Class: ActiveRecordAnonymizer::FakeValue

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_anonymizer/fake_value.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, column) ⇒ FakeValue

Returns a new instance of FakeValue.



5
6
7
8
# File 'lib/active_record_anonymizer/fake_value.rb', line 5

def initialize(name, column)
  @name = name
  @column = column
end

Instance Method Details

#generate_fake_valueObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record_anonymizer/fake_value.rb', line 10

def generate_fake_value
  case @column.type
  when :string, :text, :citext then Faker::Lorem.word
  when :uuid then SecureRandom.uuid
  when :integer, :bigint, :smallint then Faker::Number.number(digits: 5)
  when :decimal, :float, :real then Faker::Number.decimal(l_digits: 2, r_digits: 2)
  when :datetime, :timestamp, :timestamptz then Faker::Time.between(from: DateTime.now - 1, to: DateTime.now)
  when :date then Faker::Date.between(from: Date.today - 2, to: Date.today)
  when :time, :timetz then Faker::Time.forward(days: 23, period: :morning)
  when :boolean then Faker::Boolean.boolean
  when :json, :jsonb then generate_json
  when :inet then Faker::Internet.ip_v4_address
  when :cidr, :macaddr then Faker::Internet.mac_address
  when :bytea then Faker::Internet.password
  when :bit, :bit_varying then %w[0 1].sample
  when :money then generate_money
  when :hstore then generate_json
  when :year then rand(1901..2155)
  else raise UnknownColumnTypeError, "Unknown column type: #{@column.type}"
  end
end