Class: RandomNameGenerator

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

Overview

RandomNameGenerator:

Examples

rng = RandomNameGenerator.new(RandomNameGenerator::GOBLIN)
puts rng.compose(3)

By default RandomNameGenerator uses the Fantasy syllable file and creates a name with between 2 and 5 syllables.

rng = RandomNameGenerator.new
puts rng.compose

:reek:TooManyInstanceVariables :reek:TooManyStatements

Constant Summary collapse

ELVEN =
File.new("#{dirname}/languages/elven.txt")
FANTASY =
File.new("#{dirname}/languages/fantasy.txt")
GOBLIN =
File.new("#{dirname}/languages/goblin.txt")
ROMAN =
File.new("#{dirname}/languages/roman.txt")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RandomNameGenerator.



27
28
29
30
31
32
33
34
35
36
# File 'lib/random_name_generator/random_name_generator.rb', line 27

def initialize(filename = RandomNameGenerator::FANTASY, random: Random.new)
  @pre = nil
  @file = File.new(filename)
  @rnd = random
  @pre_syllables = []
  @sur_syllables = []
  @mid_syllables = []

  refresh
end

Instance Attribute Details

#mid_syllablesObject (readonly)

Returns the value of attribute mid_syllables.



25
26
27
# File 'lib/random_name_generator/random_name_generator.rb', line 25

def mid_syllables
  @mid_syllables
end

#preObject (readonly)

Returns the value of attribute pre.



25
26
27
# File 'lib/random_name_generator/random_name_generator.rb', line 25

def pre
  @pre
end

#pre_syllablesObject (readonly)

Returns the value of attribute pre_syllables.



25
26
27
# File 'lib/random_name_generator/random_name_generator.rb', line 25

def pre_syllables
  @pre_syllables
end

#sur_syllablesObject (readonly)

Returns the value of attribute sur_syllables.



25
26
27
# File 'lib/random_name_generator/random_name_generator.rb', line 25

def sur_syllables
  @sur_syllables
end

Class Method Details

.flip_modeObject

Public: Static factory method that instantiates a RandomNameGenerator in a random language.



39
40
41
42
43
44
45
# File 'lib/random_name_generator/random_name_generator.rb', line 39

def self.flip_mode
  langs = [RandomNameGenerator::FANTASY,
           RandomNameGenerator::ELVEN,
           RandomNameGenerator::GOBLIN,
           RandomNameGenerator::ROMAN]
  new(langs.sample)
end

.pick_number_of_syllablesObject



56
57
58
# File 'lib/random_name_generator/random_name_generator.rb', line 56

def self.pick_number_of_syllables
  [2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5].sample
end

Instance Method Details

#compose(count = RandomNameGenerator.pick_number_of_syllables) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/random_name_generator/random_name_generator.rb', line 47

def compose(count = RandomNameGenerator.pick_number_of_syllables)
  @pre = pre_syllables.sample
  return @pre.to_s.capitalize if count < 2

  name = determine_middle_syllables(count - 2, pre)
  name << determine_last_syllable(name.last)
  name.map(&:to_s).join.capitalize
end

#to_sObject



60
61
62
# File 'lib/random_name_generator/random_name_generator.rb', line 60

def to_s
  "RandomNameGenerator (#{@file.path})"
end