Module: Dsu::Support::ShortString

Included in:
Descriptable::ClassMethods, Validators::ProjectNameValidator
Defined in:
lib/dsu/support/short_string.rb

Constant Summary collapse

SHORT_STRING_MAX_COUNT =
25

Class Method Summary collapse

Class Method Details

.short_string(string:, count: SHORT_STRING_MAX_COUNT, elipsis: '...') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dsu/support/short_string.rb', line 10

def short_string(string:, count: SHORT_STRING_MAX_COUNT, elipsis: '...')
  return '' if string.blank?
  return string if string.length <= count

  # Trim to max count and cut at the last space within the limit
  trimmed_string = string[0...count].rpartition(' ')[0]

  # If no space found, trim by characters
  trimmed_string = string[0...(count - elipsis.length)] if trimmed_string.empty? && !string.empty?

  "#{trimmed_string}#{elipsis}"
end