Module: Poefy::Generation

Included in:
Poem
Defined in:
lib/poefy/generation.rb

Instance Method Summary collapse

Instance Method Details

#poem(poetic_form = @poetic_form) ⇒ Object

Generate specific poem types.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/poefy/generation.rb', line 17

def poem poetic_form = @poetic_form

  # Can't do much if the database doesn't exist.
  raise Poefy::MissingDatabase unless @corpus.exists?

  # Validate the poetic form hash.
  raise ArgumentError, 'Argument must be a hash' unless
        poetic_form.is_a?(Hash)
  poetic_form = validate_poetic_form poetic_form
  poetic_form = @poetic_form.merge poetic_form

  # Make sure the hash contains ':form' or ':rhyme' keys.
  # Make sure the rhyme token is not empty.
  rhyme = poetic_form[:rhyme]
  if !(rhyme || poetic_form[:form])
    raise Poefy::MissingFormOrRhyme
  elsif (rhyme && rhyme.count == 1 && rhyme.first[:token] == ' ')
    raise Poefy::MissingFormOrRhyme
  end

  # Loop until we find a valid poem.
  # There are cases where valid permutations are not able to be
  #   genned on the first try, so keep trying a few more times.
  output, count_down = nil, 10
  loop do
    output = gen_poem_using_conditions poetic_form
    break if !output.nil? || count_down == 0
    count_down -= 1
  end

  # Return nil if poem could not be created.
  return nil if (output.nil? or output == [] or output == [''])

  # Indent the output using the :indent string.
  output = do_indent(output, get_poetic_form_indent(poetic_form))

  # Append blank lines to the end if the :rhyme demands it.
  rhyme = tokenise_rhyme get_poetic_form_rhyme poetic_form
  (output.length...rhyme.length).each do |i|
    output[i] = ''
  end

  output
end

#poem!(poetic_form = @poetic_form) ⇒ Object

Same as the above method, but swallow any errors.



63
64
65
66
67
68
69
# File 'lib/poefy/generation.rb', line 63

def poem! poetic_form = @poetic_form
  begin
    poem poetic_form
  rescue Poefy::Error
    nil
  end
end