Module: Dsu::Support::Descriptable::ClassMethods

Defined in:
lib/dsu/support/descriptable.rb

Instance Method Summary collapse

Instance Method Details

#short_description(string:, count: DESCRIPTION_MAX_COUNT, elipsis: '...') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dsu/support/descriptable.rb', line 21

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