Class: Namelab::WordGenerator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#wordObject (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_leftObject

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

#generateObject 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_syllableObject

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