class RandomNameGenerator::Generator
Workhorse class that assembles names from dialect files.
Attributes
language[R]
mid_syllables[R]
pre_syllables[R]
sur_syllables[R]
Public Class Methods
new(language = RandomNameGenerator::FANTASY, random: Random.new)
click to toggle source
# File lib/random_name_generator.rb, line 72 def initialize(language = RandomNameGenerator::FANTASY, random: Random.new) @pre = nil @language = language @rnd = random @pre_syllables = [] @mid_syllables = [] @sur_syllables = [] refresh end
Public Instance Methods
compose(count = RandomNameGenerator.pick_number_of_syllables)
click to toggle source
# File lib/random_name_generator.rb, line 93 def compose(count = RandomNameGenerator.pick_number_of_syllables) compose_array(count).map(&:to_s).join.capitalize end
compose_array(count = RandomNameGenerator.pick_number_of_syllables)
click to toggle source
Returns the composed name as an array of Syllables.
# File lib/random_name_generator.rb, line 84 def compose_array(count = RandomNameGenerator.pick_number_of_syllables) @pre = pre_syllables.sample(random: @rnd) return @pre.to_s.capitalize if count < 2 name = determine_middle_syllables(count - 2, @pre) name << determine_last_syllable(name.last) name end
to_s()
click to toggle source
# File lib/random_name_generator.rb, line 97 def to_s "RandomNameGenerator::Generator (#{@language.path.split("/")[-1]})" end
Private Instance Methods
determine_last_syllable(next_to_last_syllable)
click to toggle source
# File lib/random_name_generator.rb, line 107 def determine_last_syllable(next_to_last_syllable) determine_next_syllable(next_to_last_syllable, @sur_syllables) end
determine_middle_syllables(count, pre)
click to toggle source
# File lib/random_name_generator.rb, line 103 def determine_middle_syllables(count, pre) determine_next_syllables(count, pre, @mid_syllables) end
determine_next_syllable(this_syllable, sampler)
click to toggle source
# File lib/random_name_generator.rb, line 123 def determine_next_syllable(this_syllable, sampler) next_syllable = "" loop do next_syllable = sampler.sample(random: @rnd) break unless this_syllable.incompatible?(next_syllable) end next_syllable end
determine_next_syllables(count, pre, syllables)
click to toggle source
# File lib/random_name_generator.rb, line 111 def determine_next_syllables(count, pre, syllables) name = Array(pre) return name if count < 1 next_syllable = pre count.times do next_syllable = determine_next_syllable(next_syllable, syllables) name << next_syllable end name end
push(syllable)
click to toggle source
# File lib/random_name_generator.rb, line 140 def push(syllable) if syllable.prefix? @pre_syllables.push(syllable) elsif syllable.suffix? @sur_syllables.push(syllable) else @mid_syllables.push(syllable) end end
refresh()
click to toggle source
Loops through the language file, and pushes each syllable into the correct array.
# File lib/random_name_generator.rb, line 133 def refresh @language.readlines.each do |line| push(RandomNameGenerator::Syllable.new(line)) unless line.empty? end @language.rewind end