Class: DataMasker::MaskedName

Inherits:
Object
  • Object
show all
Defined in:
lib/data_masker/masked_name.rb

Overview

Mask common names using names from US census

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ MaskedName

Returns a new instance of MaskedName.



25
26
27
# File 'lib/data_masker/masked_name.rb', line 25

def initialize(name = nil)
  @hash = Digest::MD5.hexdigest("#{DataMasker.salt}#{name}")
end

Class Method Details

.first_namesObject



8
9
10
11
12
# File 'lib/data_masker/masked_name.rb', line 8

def first_names
  return @first_names if @first_names
  file = File.expand_path('../../../data/us_first_names.txt', __FILE__)
  @first_names = File.readlines(file).map(&:strip)
end

.initialsObject



20
21
22
# File 'lib/data_masker/masked_name.rb', line 20

def initials
  @initials ||= ('A'..'Z').to_a
end

.last_namesObject



14
15
16
17
18
# File 'lib/data_masker/masked_name.rb', line 14

def last_names
  return @last_names if @last_names
  file = File.expand_path('../../../data/us_last_names.txt', __FILE__)
  @last_names = File.readlines(file).map(&:strip)
end

Instance Method Details

#first_nameObject



33
34
35
36
37
38
# File 'lib/data_masker/masked_name.rb', line 33

def first_name
  return @first_name if @first_name
  # First 3 hex digits provide 4096 possibilities, we have 2048 first names
  first_name_idx = @hash[0..2].hex / 2
  @first_name = self.class.first_names[first_name_idx]
end

#initialObject



40
41
42
43
44
45
46
# File 'lib/data_masker/masked_name.rb', line 40

def initial
  return @initial if @initial
  # Next 2 hex digits provide 256 possibilities, divide by 10 to distribute
  # over 26 initials
  initial_idx = @hash[3..4].hex / 10
  @initial = self.class.initials[initial_idx]
end

#last_nameObject



48
49
50
51
52
53
# File 'lib/data_masker/masked_name.rb', line 48

def last_name
  return @last_name if @last_name
  # Next 3 hex digits provide 4096 possibilites, we have 4096 last names
  last_name_idx = @hash[5..7].hex
  @last_name = self.class.last_names[last_name_idx]
end

#to_sObject



29
30
31
# File 'lib/data_masker/masked_name.rb', line 29

def to_s
  "#{first_name} #{initial}. #{last_name}"
end