Class: Namelab::WordGenerator
- Inherits:
-
Object
- Object
- Namelab::WordGenerator
- Defined in:
- lib/namelab/word_generator.rb
Overview
The word generator class.
Constant Summary collapse
- LengthFilter =
-> (op, len, syl) { syl.length.send(op, len) }.freeze.curry(3).freeze
Instance Attribute Summary collapse
-
#word ⇒ Object
readonly
Returns the value of attribute word.
Instance Method Summary collapse
-
#chars_left ⇒ Object
Calculates how many chars are left to generate.
-
#generate ⇒ Object
(also: #call)
Generates word and cleanups instance variables.
-
#sample_syllable ⇒ Object
Fetches random syllable.
Instance Attribute Details
#word ⇒ Object (readonly)
Returns the value of attribute word.
17 18 19 |
# File 'lib/namelab/word_generator.rb', line 17 def word @word end |
Instance Method Details
#chars_left ⇒ Object
Calculates how many chars are left to generate.
37 38 39 |
# File 'lib/namelab/word_generator.rb', line 37 def chars_left @target_length - word.length end |
#generate ⇒ Object Also known as: call
Generates word and cleanups instance variables.
24 25 26 27 28 29 30 31 32 |
# File 'lib/namelab/word_generator.rb', line 24 def generate while chars_left > 0 word << sample_syllable normalize! if normalize end return @word.capitalize ensure cleanup! end |
#sample_syllable ⇒ Object
Fetches random syllable.
44 45 46 47 |
# File 'lib/namelab/word_generator.rb', line 44 def sample_syllable filter = LengthFilter[:<=, chars_left.next] if chars_left <= 3 Registry['syls.sample'][filter].to_s end |