module SmartTruncate

Constants

VERSION

Public Class Methods

by_chars(text, char_limit, ending = '.') click to toggle source

truncate by characters count

# File lib/smart_truncate.rb, line 12
def self.by_chars(text, char_limit, ending = '.')
  text = text.to_s
  text = text.squish
  size = 0
  new_text = text.mb_chars.split().reject do |token|
    size+=token.size()
    size>char_limit
  end.join(" ")
  if size > char_limit
    return new_text += ending
  else
    return new_text
  end
end
by_mixed(text, sentence_limit, char_limit) click to toggle source
# File lib/smart_truncate.rb, line 37
def self.by_mixed(text, sentence_limit, char_limit)
  text = self.by_sentences(text, sentence_limit)
  text = self.by_chars(text, char_limit)
end
by_sentences(text, sentence_limit, ending = '.') click to toggle source
# File lib/smart_truncate.rb, line 27
def self.by_sentences(text, sentence_limit, ending = '.')
  text = text.to_s
  text = text.squish
  size = 0
  arr = text.mb_chars.split(/(?:\.|\?|\!|\…|\;)(?= [^a-z]|$)/)
  arr = arr[0...sentence_limit]
  new_text = arr.join(".")
  new_text += ending
end
hi() click to toggle source

sample gem verification

# File lib/smart_truncate.rb, line 7
def self.hi
  return 'hi'
end