Class: FakePerson
- Inherits:
-
Object
- Object
- FakePerson
- 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
-
.female_titles ⇒ Object
Return all female titles.
-
.free_email_domains ⇒ Object
Return an array of free email hosting domains.
-
.given_names(gender) ⇒ Object
Return an array of all possible first names.
-
.male_titles ⇒ Object
Return all male titles.
-
.surnames ⇒ Object
Return an array of all possible last names.
-
.unisex_titles ⇒ Object
Return all unisex titles.
Instance Method Summary collapse
-
#age ⇒ Object
Return this person’s age.
-
#date_of_birth(min_age = 18, max_age = 80) ⇒ Object
Return the person’s date of birth.
-
#email_address(domain = 'example.com') ⇒ Object
Return an email address for this person.
-
#first_name ⇒ Object
Return the first name.
-
#free_email_address ⇒ Object
Return a free email address.
-
#gender ⇒ Object
Return the gender for this person.
-
#initials ⇒ Object
Return the initials.
-
#last_name ⇒ Object
Return the last name.
-
#middle_name ⇒ Object
Return the middle name.
-
#name(format = :standard) ⇒ Object
Return a full name.
-
#title ⇒ Object
Return a title.
-
#username ⇒ Object
Return a username which this user can be assigned.
Class Method Details
.female_titles ⇒ Object
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_domains ⇒ Object
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.(File.join('..', '..', '..', 'db', "given_names.#{gender}.txt"), __FILE__) File.read(path).split("\n").compact.map(&:capitalize) end end |
.male_titles ⇒ Object
Return all male titles
26 27 28 |
# File 'lib/fake_person/titles.rb', line 26 def male_titles @male_titles ||= ['Mr'] end |
.surnames ⇒ Object
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.(File.join('..', '..', '..', 'db', "surnames.txt"), __FILE__) File.read(path).split("\n").compact.map(&:capitalize) end end |
.unisex_titles ⇒ Object
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
#age ⇒ Object
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_name ⇒ Object
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_address ⇒ Object
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 |
#gender ⇒ Object
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 |
#initials ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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 |
#title ⇒ Object
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 |
#username ⇒ Object
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 |