Class: DataMasker::MaskedName
- Inherits:
-
Object
- Object
- DataMasker::MaskedName
- 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
- #first_name ⇒ Object
- #initial ⇒ Object
-
#initialize(name = nil) ⇒ MaskedName
constructor
A new instance of MaskedName.
- #last_name ⇒ Object
- #to_s ⇒ Object
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_names ⇒ Object
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.('../../../data/us_first_names.txt', __FILE__) @first_names = File.readlines(file).map(&:strip) end |
.initials ⇒ Object
20 21 22 |
# File 'lib/data_masker/masked_name.rb', line 20 def initials @initials ||= ('A'..'Z').to_a end |
.last_names ⇒ Object
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.('../../../data/us_last_names.txt', __FILE__) @last_names = File.readlines(file).map(&:strip) end |
Instance Method Details
#first_name ⇒ Object
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 |
#initial ⇒ Object
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_name ⇒ Object
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_s ⇒ Object
29 30 31 |
# File 'lib/data_masker/masked_name.rb', line 29 def to_s "#{first_name} #{initial}. #{last_name}" end |