class FakePerson
Constants
- AVATAR_QUANTITIES
- AVATAR_SIZE_GROUPS
Public Class Methods
activities()
click to toggle source
# File lib/fake_person/activities.rb, line 7 def self.activities @activities ||= begin path = File.expand_path(File.join('..', '..', '..', 'db', "activities.txt"), __FILE__) File.read(path).split("\n").compact.map(&:capitalize) end end
colors()
click to toggle source
# File lib/fake_person/color.rb, line 7 def self.colors @colors ||= begin path = File.expand_path(File.join('..', '..', '..', 'db', "colors.txt"), __FILE__) File.read(path).split("\n").compact.map(&:capitalize) end end
female_titles()
click to toggle source
Return all female titles
# File lib/fake_person/titles.rb, line 33 def female_titles @female_titles ||= ['Mrs', 'Miss', 'Ms'] end
foods()
click to toggle source
# File lib/fake_person/foods.rb, line 7 def self.foods @foods ||= begin path = File.expand_path(File.join('..', '..', '..', 'db', "foods.txt"), __FILE__) File.read(path).split("\n").compact.map(&:capitalize) end end
free_email_domains()
click to toggle source
Return an array of free email hosting domains
# 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)
click to toggle source
Return an array of all possible first names
# 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_titles()
click to toggle source
Return all male titles
# File lib/fake_person/titles.rb, line 26 def male_titles @male_titles ||= ['Mr'] end
surnames()
click to toggle source
Return an array of all possible last names
# 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_titles()
click to toggle source
Return all unisex titles
# File lib/fake_person/titles.rb, line 19 def unisex_titles @unisex_titles ||= ['Dr', 'Prof', 'Rev'] end
Public Instance Methods
age()
click to toggle source
Return this person’s age
# 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
avatar_identifier()
click to toggle source
# File lib/fake_person/avatar.rb, line 6 def avatar_identifier @avatar_identifier ||= rand(AVATAR_QUANTITIES[self.gender]) end
avatar_url(options = {})
click to toggle source
# File lib/fake_person/avatar.rb, line 10 def avatar_url(options = {}) options[:size] ||= 128 closest_size = AVATAR_SIZE_GROUPS.select {|s| s >= options[:size]}.first "#{options[:secure] == false ? 'http' : 'https'}://s3-eu-west-1.amazonaws.com/fakepeople/#{gender}/#{closest_size}/#{avatar_identifier}.png" end
date_of_birth(min_age = 18, max_age = 80)
click to toggle source
Return the person’s date of birth
# 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
dislikes(count=3)
click to toggle source
# File lib/fake_person/likes.rb, line 15 def dislikes(count=3) @dislikes ||= activities_and_foods(count, @likes) end
email_address(domain = 'example.com')
click to toggle source
Return an email address for this person. All messages sent to example.com will bounce.
# File lib/fake_person/email.rb, line 7 def email_address(domain = 'example.com') @email_address ||= "#{username}@#{domain}" end
favorite_activity()
click to toggle source
# File lib/fake_person/activities.rb, line 3 def favorite_activity @favorite_activity ||= self.class.activities.sample end
favorite_color()
click to toggle source
# File lib/fake_person/color.rb, line 3 def favorite_color self.class.colors.sample end
favorite_food()
click to toggle source
# File lib/fake_person/foods.rb, line 3 def favorite_food @favorite_food ||= self.class.foods.sample end
first_name()
click to toggle source
Return the first name
# 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()
click to toggle source
Return a free email address. This might be a real person!
# 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()
click to toggle source
Return the gender for this person
# File lib/fake_person/gender.rb, line 6 def gender @gender ||= rand(2) == 0 ? :male : :female end
initials()
click to toggle source
Return the initials
# 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()
click to toggle source
Return the last name
# 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
likes(count=3)
click to toggle source
# File lib/fake_person/likes.rb, line 3 def likes(count=3) @likes ||= begin if count == 1 [favorite_activity] elsif count == 2 [favorite_activity, favorite_food] else [favorite_activity, favorite_food] + activities_and_foods(count - 2, @dislikes) end end end
middle_name()
click to toggle source
Return the middle name
# 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)
click to toggle source
Return a full name
# 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()
click to toggle source
Return a title
# 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()
click to toggle source
Return a username which this user can be assigned.
# 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
Private Instance Methods
activities_and_foods(count, exclusion_list=nil)
click to toggle source
# File lib/fake_person/likes.rb, line 21 def activities_and_foods(count, exclusion_list=nil) exclusion_list ||= [] num_activities_to_get = (1..count).to_a.sample num_foods_to_get = (count - num_activities_to_get) (self.class.activities - exclusion_list).sample(num_activities_to_get) + (self.class.foods - exclusion_list).sample(num_foods_to_get) end