module ContentTruncate::String

Public Instance Methods

content_truncate(limit_length, *separators) click to toggle source
# File lib/content_truncate.rb, line 5
def content_truncate limit_length, *separators
  sub_string = self.dup
  sep = separators.shift
  if sep
    sub_string.send(:smart_truncate, limit_length, sep, *separators)
  else
    sub_string.send(:smart_truncate, limit_length, "<br/>", "\n", "。", ".", " ")
  end
end

Private Instance Methods

smart_truncate(limit_length, sep, *separators) click to toggle source
# File lib/content_truncate.rb, line 16
def smart_truncate limit_length, sep, *separators
  while sep
    position = self.index(sep)||limit_length+1
    if position <= limit_length
      while position && position <= limit_length
        prev_index, position = position, self.index(sep, position+1)
      end
      return self[0...(prev_index+sep.length)].strip
    end
    sep = separators.shift
  end
  return self[0...limit_length].strip
end