class MemeCaptain::Caption

Public Class Methods

new(s = '') click to toggle source
Calls superclass method
# File lib/meme_captain/caption.rb, line 7
def initialize(s = '')
  super s.to_s
end

Public Instance Methods

annotate_quote() click to toggle source

Return the contents of the string quoted for ImageMagick annotate.

# File lib/meme_captain/caption.rb, line 12
def annotate_quote
  Caption.new(
    gsub('\\', '\\\\\\').
    gsub('%', '%%').
    gsub(/^@/, '\@'))
end
drawable?() click to toggle source

Whether the string contains any non-whitespace.

# File lib/meme_captain/caption.rb, line 20
def drawable?
  match(/[^\s]/) ? true : false
end
wrap(num_lines) click to toggle source

Wrap the string of into num_lines lines.

# File lib/meme_captain/caption.rb, line 25
def wrap(num_lines)
  cleaned = gsub(/\s+/, ' ').strip

  chars_per_line = cleaned.size / num_lines.to_f

  lines = []
  cleaned.split.each do |word|
    if lines.empty?
      lines << word
    else
      if (lines[-1].size + 1 + word.size) <= chars_per_line ||
          lines.size >= num_lines
        lines[-1] << ' ' unless lines[-1].empty?
        lines[-1] << word
      else
        lines << word
      end
    end
  end

  Caption.new(lines.join("\n"))
end