module Trenni::Formatters::TruncatedText

Public Class Methods

truncate_text(text, truncate_at, omission: nil, separator: nil, **options) click to toggle source
# File lib/trenni/formatters/truncated_text.rb, line 37
def self.truncate_text(text, truncate_at, omission: nil, separator: nil, **options)
        return text.dup unless text.length > truncate_at
        
        omission ||= '...'
        
        length_with_room_for_omission = truncate_at - omission.length
        
        stop = nil
        
        if separator
                stop = text.rindex(separator, length_with_room_for_omission)
        end
        
        stop ||= length_with_room_for_omission
        
        "#{text[0...stop]}#{omission}"
end

Public Instance Methods

truncated_text(content, length: 30, **options) click to toggle source
# File lib/trenni/formatters/truncated_text.rb, line 29
def truncated_text(content, length: 30, **options)
        if content
                content = TruncatedText.truncate_text(content, length, **options)
                
                return self.format(content)
        end
end