Class: NameBank
- Inherits:
-
Object
- Object
- NameBank
- Defined in:
- lib/name_bank.rb,
lib/name_bank/errors.rb,
lib/name_bank/version.rb,
lib/name_bank/pool_store.rb,
lib/name_bank/pool_schema.rb
Overview
Declared as a class, not a module: NameBank itself is one (see lib/name_bank.rb).
Defined Under Namespace
Modules: Error, PoolSchema Classes: EmptyPool, MalformedPool, PoolExhausted, UnknownCountry, UnknownGender, UnknownScript, UnknownVariant
Constant Summary collapse
- DATA_DIR =
File.("../data", __dir__)
- VERSION =
"0.4.0"
Class Method Summary collapse
- .countries ⇒ Object
- .default ⇒ Object
- .first_name ⇒ Object
- .first_names ⇒ Object
- .full_name ⇒ Object
- .full_names ⇒ Object
- .last_name ⇒ Object
- .last_names ⇒ Object
- .scripts ⇒ Object
- .variants ⇒ Object
Instance Method Summary collapse
- #countries ⇒ Object
- #first_name(country:, gender:, rng:, variant: nil, script: :latin) ⇒ Object
-
#first_names(country:, gender:, variant: nil, script: :latin) ⇒ Object
The whole frequency-ordered pool, frozen.
- #full_name(country:, gender:, rng:, variant: nil, script: :latin) ⇒ Object
-
#full_names(country:, gender:, rng:, count:, variant: nil, script: :latin) ⇒ Object
Distinct given/family pairs, for seeding many rows at once.
-
#initialize(data_dir: DATA_DIR) ⇒ NameBank
constructor
A new instance of NameBank.
- #last_name(country:, rng:, variant: nil, script: :latin) ⇒ Object
- #last_names(country:, variant: nil, script: :latin) ⇒ Object
-
#scripts(country:, variant: nil) ⇒ Object
The script forms this country offers, not writing systems — see CONTEXT.md.
- #variants(country:) ⇒ Object
Constructor Details
Class Method Details
.countries ⇒ Object
33 |
# File 'lib/name_bank.rb', line 33 def countries(...) = default.countries(...) |
.default ⇒ Object
23 24 25 |
# File 'lib/name_bank.rb', line 23 def default @default ||= new end |
.first_name ⇒ Object
27 |
# File 'lib/name_bank.rb', line 27 def first_name(...) = default.first_name(...) |
.first_names ⇒ Object
31 |
# File 'lib/name_bank.rb', line 31 def first_names(...) = default.first_names(...) |
.full_name ⇒ Object
29 |
# File 'lib/name_bank.rb', line 29 def full_name(...) = default.full_name(...) |
.full_names ⇒ Object
30 |
# File 'lib/name_bank.rb', line 30 def full_names(...) = default.full_names(...) |
.last_name ⇒ Object
28 |
# File 'lib/name_bank.rb', line 28 def last_name(...) = default.last_name(...) |
.last_names ⇒ Object
32 |
# File 'lib/name_bank.rb', line 32 def last_names(...) = default.last_names(...) |
.scripts ⇒ Object
35 |
# File 'lib/name_bank.rb', line 35 def scripts(...) = default.scripts(...) |
.variants ⇒ Object
34 |
# File 'lib/name_bank.rb', line 34 def variants(...) = default.variants(...) |
Instance Method Details
#countries ⇒ Object
81 82 83 |
# File 'lib/name_bank.rb', line 81 def countries @store.countries end |
#first_name(country:, gender:, rng:, variant: nil, script: :latin) ⇒ Object
42 43 44 |
# File 'lib/name_bank.rb', line 42 def first_name(country:, gender:, rng:, variant: nil, script: :latin) first_names(country: country, gender: gender, variant: variant, script: script).sample(random: rng) end |
#first_names(country:, gender:, variant: nil, script: :latin) ⇒ Object
The whole frequency-ordered pool, frozen. The name strings stay mutable.
73 74 75 |
# File 'lib/name_bank.rb', line 73 def first_names(country:, gender:, variant: nil, script: :latin) pool(country, variant, PoolSchema.gender_key(gender), script) end |
#full_name(country:, gender:, rng:, variant: nil, script: :latin) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/name_bank.rb', line 50 def full_name(country:, gender:, rng:, variant: nil, script: :latin) { firstname: first_name(country: country, gender: gender, rng: rng, variant: variant, script: script), lastname: last_name(country: country, rng: rng, variant: variant, script: script) } end |
#full_names(country:, gender:, rng:, count:, variant: nil, script: :latin) ⇒ Object
Distinct given/family pairs, for seeding many rows at once. No pair is drawn twice; single names do recur, which is what a population looks like. The guarantee holds within one call — two calls know nothing of each other. Asking for more pairs than the two pools can form raises PoolExhausted.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/name_bank.rb', line 61 def full_names(country:, gender:, rng:, count:, variant: nil, script: :latin) firsts = first_names(country: country, gender: gender, variant: variant, script: script) lasts = last_names(country: country, variant: variant, script: script) available = firsts.size * lasts.size raise PoolExhausted, "#{country}: #{count} pairs requested, #{available} available" if count > available distinct_indices(available, count, rng).map do |index| { firstname: firsts[index / lasts.size], lastname: lasts[index % lasts.size] } end end |
#last_name(country:, rng:, variant: nil, script: :latin) ⇒ Object
46 47 48 |
# File 'lib/name_bank.rb', line 46 def last_name(country:, rng:, variant: nil, script: :latin) last_names(country: country, variant: variant, script: script).sample(random: rng) end |
#last_names(country:, variant: nil, script: :latin) ⇒ Object
77 78 79 |
# File 'lib/name_bank.rb', line 77 def last_names(country:, variant: nil, script: :latin) pool(country, variant, PoolSchema::SURNAMES, script) end |
#scripts(country:, variant: nil) ⇒ Object
The script forms this country offers, not writing systems — see CONTEXT.md. With a variant, the forms that variant offers, which may differ.
91 92 93 94 |
# File 'lib/name_bank.rb', line 91 def scripts(country:, variant: nil) data = @store.pools(country, variant) PoolSchema::KEYS.any? { |k| data[PoolSchema.native_key(k)]&.any? } ? %i[latin native] : %i[latin] end |
#variants(country:) ⇒ Object
85 86 87 |
# File 'lib/name_bank.rb', line 85 def variants(country:) @store.variants(country) end |