Method: BBLib.chars_up_to
- Defined in:
- lib/bblib/core/util/string.rb
.chars_up_to(str, cap, too_long = '...', style: :front) ⇒ Object
Displays a portion of an object (as a string) with an ellipse displayed if the string is over a certain size. Supported styles:
> front - “for exam…”
> back - “… example”
> middle - “… exam…”
> outter - “for e…ple”
The length of the too_long string is NOT factored into the cap
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bblib/core/util/string.rb', line 66 def self.chars_up_to(str, cap, too_long = '...', style: :front) return str if str.to_s.size <= cap str = str.to_s case style when :back "#{too_long}#{str[(str.size - cap)..-1]}" when :outter "#{str[0...(cap / 2).to_i + (cap.odd? ? 1 : 0)]}#{too_long}#{str[-(cap / 2).to_i..-1]}" when :middle "#{too_long}#{str[(str.size / 2 - cap / 2 - (cap.odd? ? 1 : 0)).to_i...(str.size / 2 + cap / 2).to_i]}#{too_long}" else "#{str[0...cap]}#{too_long}" end end |