Module: XRB::Formatters::TruncatedText
- Defined in:
- lib/xrb/formatters/truncated_text.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.truncate_text(text, truncate_at, omission: nil, separator: nil, **options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/xrb/formatters/truncated_text.rb', line 20 def self.truncate_text(text, truncate_at, omission: nil, separator: nil, **) 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 |
Instance Method Details
#truncated_text(content, length: 30, **options) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/xrb/formatters/truncated_text.rb', line 12 def truncated_text(content, length: 30, **) if content content = TruncatedText.truncate_text(content, length, **) return self.format(content) end end |