Class: MemeCaptain::Caption
- Inherits:
-
String
- Object
- String
- MemeCaptain::Caption
- Defined in:
- lib/meme_captain/caption.rb
Instance Method Summary collapse
-
#annotate_quote ⇒ Object
Return the contents of the string quoted for ImageMagick annotate.
-
#drawable? ⇒ Boolean
Whether the string contains any non-whitespace.
-
#initialize(s = '') ⇒ Caption
constructor
A new instance of Caption.
-
#wrap(num_lines) ⇒ Object
Wrap the string of into num_lines lines.
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_quote ⇒ Object
Return the contents of the string quoted for ImageMagick annotate.
12 13 14 15 16 17 |
# File 'lib/meme_captain/caption.rb', line 12 def annotate_quote Caption.new( gsub('\\', '\\\\\\'). gsub('%', '%%'). gsub(/^@/, '\@')) end |
#drawable? ⇒ Boolean
Whether the string contains any non-whitespace.
20 21 22 |
# File 'lib/meme_captain/caption.rb', line 20 def drawable? match(/[^\s]/) ? true : false end |
#wrap(num_lines) ⇒ Object
Wrap the string of into num_lines lines.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |