string.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. class String
  2. def message_quote
  3. quote = self.split("\n")
  4. body_quote = ''
  5. quote.each do |line|
  6. body_quote = body_quote + '> ' + line + "\n"
  7. end
  8. body_quote
  9. end
  10. def word_wrap(*args)
  11. options = args.extract_options!
  12. unless args.blank?
  13. options[:line_width] = args[0] || 82
  14. end
  15. options.reverse_merge!(:line_width => 82)
  16. lines = self
  17. lines.split("\n").collect do |line|
  18. line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
  19. end * "\n"
  20. end
  21. def to_filename
  22. camel_cased_word = self.to_s
  23. camel_cased_word.gsub(/::/, '/').downcase
  24. end
  25. # because of mysql inno_db limitations, strip 4 bytes utf8 chars (e. g. emojis)
  26. # unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes
  27. # More details: http://pjambet.github.io/blog/emojis-and-mysql/
  28. def utf8_to_3bytesutf8
  29. return if ActiveRecord::Base.connection_config[:adapter] != 'mysql2'
  30. self.each_char.select {|c|
  31. if c.bytes.count > 3
  32. puts "WARNING: strip out 4 bytes utf8 chars '#{c}' of '#{ self }'"
  33. next
  34. end
  35. c
  36. }
  37. .join('')
  38. end
  39. end