Class: FakePerson

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_person/email.rb,
lib/fake_person/names.rb,
lib/fake_person/gender.rb,
lib/fake_person/titles.rb,
lib/fake_person/username.rb,
lib/fake_person/date_of_birth.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.female_titlesObject

Return all female titles



33
34
35
# File 'lib/fake_person/titles.rb', line 33

def female_titles
  @female_titles ||= ['Mrs', 'Miss', 'Ms']
end

.free_email_domainsObject

Return an array of free email hosting domains



23
24
25
# File 'lib/fake_person/email.rb', line 23

def free_email_domains
  @free_email_domains ||= ["hotmail.com", "gmail.com", "yahoo.com", "gmx.com"]
end

.given_names(gender) ⇒ Object

Return an array of all possible first names



58
59
60
61
62
63
64
# File 'lib/fake_person/names.rb', line 58

def given_names(gender)
  @given_names ||= {}
  @given_names[gender.to_sym] ||= begin
    path = File.expand_path(File.join('..', '..', '..', 'db', "given_names.#{gender}.txt"), __FILE__)
    File.read(path).split("\n").compact.map(&:capitalize)
  end
end

.male_titlesObject

Return all male titles



26
27
28
# File 'lib/fake_person/titles.rb', line 26

def male_titles
  @male_titles ||= ['Mr']
end

.surnamesObject

Return an array of all possible last names



69
70
71
72
73
74
# File 'lib/fake_person/names.rb', line 69

def surnames
  @surnames ||= begin
    path = File.expand_path(File.join('..', '..', '..', 'db', "surnames.txt"), __FILE__)
    File.read(path).split("\n").compact.map(&:capitalize)
  end
end

.unisex_titlesObject

Return all unisex titles



19
20
21
# File 'lib/fake_person/titles.rb', line 19

def unisex_titles
  @unisex_titles ||= ['Dr', 'Prof', 'Rev']
end

Instance Method Details

#ageObject

Return this person’s age



15
16
17
18
19
20
21
# File 'lib/fake_person/date_of_birth.rb', line 15

def age
  @age ||= begin
    years  = Date.today.year - self.date_of_birth.year
    years -= 1 if Date.today.yday < self.date_of_birth.yday
    years
  end
end

#date_of_birth(min_age = 18, max_age = 80) ⇒ Object

Return the person’s date of birth



8
9
10
# File 'lib/fake_person/date_of_birth.rb', line 8

def date_of_birth(min_age = 18, max_age = 80)
  @date_of_birth ||= Date.new(Date.today.year - max_age + rand(max_age - min_age), rand(12) + 1, rand(28) + 1)
end

#email_address(domain = 'example.com') ⇒ Object

Return an email address for this person. All messages sent to example.com will bounce.



7
8
9
# File 'lib/fake_person/email.rb', line 7

def email_address(domain = 'example.com')
  @email_address ||= "#{username}@#{domain}"
end

#first_nameObject

Return the first name



25
26
27
# File 'lib/fake_person/names.rb', line 25

def first_name
  @first_name ||= self.class.given_names(self.gender).shuffle.first
end

#free_email_addressObject

Return a free email address. This might be a real person!



14
15
16
# File 'lib/fake_person/email.rb', line 14

def free_email_address
  @free_email_address ||= "#{username}@#{self.class.free_email_domains.shuffle.first}"
end

#genderObject

Return the gender for this person



6
7
8
# File 'lib/fake_person/gender.rb', line 6

def gender
  @gender ||= rand(2) == 0 ? :male : :female
end

#initialsObject

Return the initials



18
19
20
# File 'lib/fake_person/names.rb', line 18

def initials
  "#{first_name[0,1]}#{middle_name[0,1]}#{last_name[0,1]}"
end

#last_nameObject

Return the last name



44
45
46
47
48
49
50
51
# File 'lib/fake_person/names.rb', line 44

def last_name
  @last_name ||= begin
    while @last_name.nil? || @last_name == self.first_name || @last_name == self.middle_name
      @last_name = self.class.surnames.shuffle.first
    end
    @last_name
  end
end

#middle_nameObject

Return the middle name



32
33
34
35
36
37
38
39
# File 'lib/fake_person/names.rb', line 32

def middle_name
  @middle_name ||= begin
    while @middle_name.nil? || @middle_name == self.first_name
      @middle_name = self.class.given_names(self.gender).shuffle.first
    end
    @middle_name
  end
end

#name(format = :standard) ⇒ Object

Return a full name



6
7
8
9
10
11
12
13
# File 'lib/fake_person/names.rb', line 6

def name(format = :standard)
  case format
  when :standard            then "#{first_name} #{last_name}"
  when :full                then "#{first_name} #{middle_name} #{last_name}"
  when :formal              then "#{title}. #{last_name}"
  when :formal_with_first   then "#{title}. #{first_name} #{last_name}" 
  end
end

#titleObject

Return a title



6
7
8
9
10
11
12
# File 'lib/fake_person/titles.rb', line 6

def title
  @title ||= begin
    base = (gender == :male ? self.class.male_titles : self.class.female_titles)
    base = base | self.class.unisex_titles if rand(5) == 0
    base.shuffle.first
  end
end

#usernameObject

Return a username which this user can be assigned.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fake_person/username.rb', line 6

def username
  @username ||= begin
    case rand(7)
    when 0 then "#{first_name.downcase}.#{last_name.downcase}"
    when 1 then "#{first_name.downcase[0,1]}#{last_name.downcase}"
    when 2 then "#{last_name.downcase}-#{first_name.downcase}"
    when 3 then "#{first_name.downcase}#{last_name.downcase}"
    when 4 then "#{first_name.downcase[0,1]}.#{middle_name.downcase[0,1]}.#{last_name.downcase}"
    when 5 then "#{first_name.downcase}#{date_of_birth.year.to_s[2,2]}"
    when 6 then "#{first_name.downcase}#{last_name.downcase}#{date_of_birth.year}"
    end
  end
end