Sha256: 43cebffb779ea3ee4b512ddde88e639c3de140c3efb310f44425ac986ae74e68
Contents?: true
Size: 1.06 KB
Versions: 26
Compression:
Stored size: 1.06 KB
Contents
# frozen_string_literal: true module Dsu module Support module Descriptable DESCRIPTION_MAX_COUNT = 25 class << self def included(base) base.extend(ClassMethods) end end def short_description return '' if description.blank? self.class.short_description(string: description) end module ClassMethods def short_description(string:, count: DESCRIPTION_MAX_COUNT, elipsis: '...') return elipsis unless string.is_a?(String) elipsis_length = elipsis.length count = elipsis_length if count.nil? || count < elipsis_length return string if string.length <= count tokens = string.split string = '' return "#{tokens.first[0...(count - elipsis_length)]}#{elipsis}" if tokens.count == 1 tokens.each do |token| break if string.length + token.length + elipsis_length > count string = "#{string} #{token}" end "#{string.strip}#{elipsis}" end end end end end
Version data entries
26 entries across 26 versions & 1 rubygems