Class: MemeCaptain::Caption

Inherits:
String
  • Object
show all
Defined in:
lib/meme_captain/caption.rb

Instance Method Summary collapse

Constructor Details

#initialize(s = '') ⇒ Caption

Returns a new instance of Caption.



7
8
9
# File 'lib/meme_captain/caption.rb', line 7

def initialize(s = '')
  super s.to_s
end

Instance Method Details

#annotate_quoteObject

Return the contents of the string quoted for ImageMagick annotate.



12
13
14
# File 'lib/meme_captain/caption.rb', line 12

def annotate_quote
  Caption.new(gsub('\\', '\\\\\\').gsub('%', '\%'))
end

#drawable?Boolean

Whether the string contains any non-whitespace.

Returns:

  • (Boolean)


17
18
19
# File 'lib/meme_captain/caption.rb', line 17

def drawable?
  match(/[^\s]/) ? true : false
end

#wrap(num_lines) ⇒ Object

Wrap the string of into num_lines lines.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/meme_captain/caption.rb', line 22

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