Module: BMO::Utils
- Defined in:
- lib/bmo/utils.rb
Overview
Utility module
Class Method Summary collapse
-
.bytesize_force_truncate(string, bytesize, options = {}) ⇒ Object
Byte aware truncation.
-
.coerce_to_symbols(hash) ⇒ Object
Coerce string hash keys to symbols.
Class Method Details
.bytesize_force_truncate(string, bytesize, options = {}) ⇒ Object
Byte aware truncation
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bmo/utils.rb', line 15 def self.bytesize_force_truncate(string, bytesize, = {}) if bytesize <= 0 return '' elsif bytesize > string.bytesize return string.dup end [:omission] ||= '...' computed_bytesize = bytesize - [:omission].bytesize return bytesize_force_truncate([:omission], bytesize, omission: '') if computed_bytesize <= 0 new_string = '' string.chars.each_with_index do |char, i| break if (new_string + char).bytesize > computed_bytesize new_string << char end stop = if [:separator] new_string.rindex([:separator]) || new_string.length else new_string.length end (new_string[0...stop] + [:omission]).to_s end |
.coerce_to_symbols(hash) ⇒ Object
Coerce string hash keys to symbols
5 6 7 8 9 10 11 12 |
# File 'lib/bmo/utils.rb', line 5 def self.coerce_to_symbols(hash) hash_symbolized = {} hash.each_pair do |key, value| key = key.to_sym if key.respond_to?(:to_sym) hash_symbolized[key] = value end hash_symbolized end |