Class: HaikuGadget::HaikuTemplate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_matrix = nil) ⇒ HaikuTemplate

Returns a new instance of HaikuTemplate.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/haiku_gadget/haiku_template.rb', line 10

def initialize(template_matrix = nil)
  # generate a template_matrix randomly if one was not provided
  if template_matrix.nil?
    template_matrix = [
      HaikuTemplate.random_template(LineTemplates::ALL_TOP_LINES),
      HaikuTemplate.random_template(LineTemplates::ALL_MIDDLE_LINES),
      HaikuTemplate.random_template(LineTemplates::ALL_BOTTOM_LINES)
    ]
  end
  # clone the two-dimensional array (so syllable counts can be changed safely)
  @template_matrix = template_matrix.map { |lt| lt.clone } 
  complete_template
end

Instance Attribute Details

#template_matrixObject (readonly)

Returns the value of attribute template_matrix.



8
9
10
# File 'lib/haiku_gadget/haiku_template.rb', line 8

def template_matrix
  @template_matrix
end

Instance Method Details

#generateObject



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

def generate
  lines = []
  @template_matrix.each do |line_template|
    words = line_template.real_words
    lines << words.join(' ')
  end
  lines
end