Class: RandomNameGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/random_name_generator.rb

Overview

Generator

Workhorse class that assembles names from dialect files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language = RandomNameGenerator::FANTASY, random: Random.new) ⇒ Generator

Returns a new instance of Generator.



72
73
74
75
76
77
78
79
80
81
# 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

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



70
71
72
# File 'lib/random_name_generator.rb', line 70

def language
  @language
end

#mid_syllablesObject (readonly)

Returns the value of attribute mid_syllables.



70
71
72
# File 'lib/random_name_generator.rb', line 70

def mid_syllables
  @mid_syllables
end

#pre_syllablesObject (readonly)

Returns the value of attribute pre_syllables.



70
71
72
# File 'lib/random_name_generator.rb', line 70

def pre_syllables
  @pre_syllables
end

#sur_syllablesObject (readonly)

Returns the value of attribute sur_syllables.



70
71
72
# File 'lib/random_name_generator.rb', line 70

def sur_syllables
  @sur_syllables
end

Instance Method Details

#compose(count = RandomNameGenerator.pick_number_of_syllables) ⇒ Object



93
94
95
# 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) ⇒ Object

Returns the composed name as an array of Syllables.



84
85
86
87
88
89
90
91
# 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_sObject



97
98
99
# File 'lib/random_name_generator.rb', line 97

def to_s
  "RandomNameGenerator::Generator (#{@language.path.split("/")[-1]})"
end