Class: RandomNameGenerator

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

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) ⇒ RandomNameGenerator

Returns a new instance of RandomNameGenerator.



13
14
15
16
17
18
19
20
# File 'lib/random_name_generator/random_name_generator.rb', line 13

def initialize(filename = RandomNameGenerator::FANTASY)
  @file = File.new(filename)
  @pre_syllables = []
  @sur_syllables = []
  @mid_syllables = []

  refresh
end

Instance Attribute Details

#mid_syllablesObject (readonly)

Returns the value of attribute mid_syllables.



11
12
13
# File 'lib/random_name_generator/random_name_generator.rb', line 11

def mid_syllables
  @mid_syllables
end

#preObject (readonly)

Returns the value of attribute pre.



11
12
13
# File 'lib/random_name_generator/random_name_generator.rb', line 11

def pre
  @pre
end

#pre_syllablesObject (readonly)

Returns the value of attribute pre_syllables.



11
12
13
# File 'lib/random_name_generator/random_name_generator.rb', line 11

def pre_syllables
  @pre_syllables
end

#sur_syllablesObject (readonly)

Returns the value of attribute sur_syllables.



11
12
13
# File 'lib/random_name_generator/random_name_generator.rb', line 11

def sur_syllables
  @sur_syllables
end

Class Method Details

.flip_modeObject



22
23
24
25
26
27
28
# File 'lib/random_name_generator/random_name_generator.rb', line 22

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

.pick_number_of_syllablesObject



80
81
82
83
# File 'lib/random_name_generator/random_name_generator.rb', line 80

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

Instance Method Details

#compose(count = RandomNameGenerator.pick_number_of_syllables) ⇒ Object



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

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

#determine_last_syllable(next_to_last_syllable) ⇒ Object



43
44
45
# File 'lib/random_name_generator/random_name_generator.rb', line 43

def determine_last_syllable(next_to_last_syllable)
  determine_next_syllable(next_to_last_syllable, @sur_syllables)
end

#determine_middle_syllables(count, pre) ⇒ Object



39
40
41
# File 'lib/random_name_generator/random_name_generator.rb', line 39

def determine_middle_syllables(count, pre)
  determine_next_syllables(count, pre, @mid_syllables)
end

#determine_next_syllable(this_syllable, sampler) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/random_name_generator/random_name_generator.rb', line 58

def determine_next_syllable(this_syllable, sampler)
  next_syllable = ''
  loop do
    next_syllable = sampler.sample
    break unless this_syllable.incompatible?(next_syllable)
  end
  next_syllable
end

#determine_next_syllables(count, pre, syllables) ⇒ Object



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

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

#refreshObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/random_name_generator/random_name_generator.rb', line 67

def refresh
  @file.readlines.each do |line|
    syllable = RNGSyllable.new(line) unless line.empty?
    if syllable.prefix?
      @pre_syllables.push(syllable)
    elsif syllable.suffix?
      @sur_syllables.push(syllable)
    else
      @mid_syllables.push(syllable)
    end
  end
end

#to_sObject



85
86
87
# File 'lib/random_name_generator/random_name_generator.rb', line 85

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