Class: Teebo::Name
- Inherits:
-
TeeboGenerator
- Object
- TeeboGenerator
- Teebo::Name
- Defined in:
- lib/teebo/name.rb
Overview
Generates names in accordance with their frequency in the United States population.
Constant Summary collapse
- GIVEN_NAMES_TABLE =
'given_names'- SURNAMES_TABLE =
'surnames'
Instance Method Summary collapse
-
#full_name(sex = nil) ⇒ Object
Generates a normal full name, including a middle name.
-
#given_name(sex) ⇒ Object
Selects a random (weighted) given name from the database.
-
#name(sex = nil) ⇒ Object
Picks a random first & last name, selecting a random gender if it’s not specified.
-
#sum_count(sex) ⇒ Object
Finds the total count for the number of names in the database.
-
#surname ⇒ Object
Selects a random (weighted) surname from the database.
Methods inherited from TeeboGenerator
Constructor Details
This class inherits a constructor from Teebo::TeeboGenerator
Instance Method Details
#full_name(sex = nil) ⇒ Object
Generates a normal full name, including a middle name.
For now, this is fairly sloppy, as the probability that a middle name will simply be another given name of the same gender is almost certainly less than 100%.
29 30 31 32 33 34 35 |
# File 'lib/teebo/name.rb', line 29 def full_name(sex=nil) # TODO: Take into account different probabilities of different types of middle names. if sex.nil? sex = %w(M F).sample end given_name(sex) + ' ' + given_name(sex) + ' ' + surname end |
#given_name(sex) ⇒ Object
Selects a random (weighted) given name from the database.
47 48 49 50 51 52 |
# File 'lib/teebo/name.rb', line 47 def given_name(sex) count = sum_count(sex) selection = rand(count) @db_connection.get_row_for_count(GIVEN_NAMES_TABLE, 'count_to', selection, {column: 'sex', condition: sex})['name'] end |
#name(sex = nil) ⇒ Object
Picks a random first & last name, selecting a random gender if it’s not specified.
15 16 17 18 19 20 |
# File 'lib/teebo/name.rb', line 15 def name (sex=nil) if sex.nil? sex = %w(M F).sample end given_name(sex) + ' ' + surname end |
#sum_count(sex) ⇒ Object
Finds the total count for the number of names in the database.
40 41 42 |
# File 'lib/teebo/name.rb', line 40 def sum_count(sex) @db_connection.get_sum(GIVEN_NAMES_TABLE, 'count', {column: 'sex', condition: sex}) end |
#surname ⇒ Object
Selects a random (weighted) surname from the database.
57 58 59 60 61 |
# File 'lib/teebo/name.rb', line 57 def surname count = @db_connection.get_sum(SURNAMES_TABLE, 'count') selection = rand(count) @db_connection.get_row_for_count(SURNAMES_TABLE, 'count_to', selection)['name'] end |