Class: HaikuGadget::LineTemplate

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

Direct Known Subclasses

DebugLineTemplate

Constant Summary collapse

EXISTING_SYLLABLES_PRIORITY =

the relative likelyhood that a word with existing syllables is going to be chosen over one that has no syllables (for example, 3 means 3 times as likely)

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*word_templates) ⇒ LineTemplate

Returns a new instance of LineTemplate.



14
15
16
# File 'lib/haiku_gadget/line_template.rb', line 14

def initialize(*word_templates)
  @word_templates = word_templates.flatten
end

Instance Attribute Details

#word_templatesObject (readonly)

Returns the value of attribute word_templates.



12
13
14
# File 'lib/haiku_gadget/line_template.rb', line 12

def word_templates
  @word_templates
end

Instance Method Details

#cloneObject



18
19
20
# File 'lib/haiku_gadget/line_template.rb', line 18

def clone
  self.class.new @word_templates.map{ |wt| wt.clone }
end

#complete_pluralityObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/haiku_gadget/line_template.rb', line 33

def complete_plurality
  obj_pluralities = []
  @word_templates.each do |word_template|
    if word_template.plurality == :any
      # need to pick a plurality for this word
      if word_template.obj_num > 0
        # this word is referencing an object, and its plurality can be randomized
        unless obj_pluralities[word_template.obj_num]
          # this word's plurality has not already been determined via another word on this line
          obj_pluralities[word_template.obj_num] = [:singular, :plural].sample
        end
        word_template.plurality = obj_pluralities[word_template.obj_num]
      else
        word_template.plurality = [:singular, :plural].sample
      end
    end
  end
end

#complete_syllables(required_syllables) ⇒ Object



26
27
28
29
30
31
# File 'lib/haiku_gadget/line_template.rb', line 26

def complete_syllables(required_syllables)
  while current_syllables < required_syllables
    increased_syllable_index = increase_row_syllable
    break if increased_syllable_index.nil?
  end
end

#current_syllablesObject



22
23
24
# File 'lib/haiku_gadget/line_template.rb', line 22

def current_syllables
  @word_templates.map { |w| w.syllables }.inject(:+)
end

#real_wordsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/haiku_gadget/line_template.rb', line 52

def real_words
  words = []
  @word_templates.each do |word_template|
    if word_template.syllables > 0
      word = word_template.word_type.get_word(
        word_template.syllables,
        word_template.plurality
      )
      words << word
    end
  end
  LineTemplate.convert_a_to_an words
  words
end