class CinnamonSerial::Formatting

Static utility methods for general use.

Public Class Methods

blank?(value) click to toggle source
# File lib/cinnamon_serial/formatting.rb, line 34
def blank?(value)
  if value.respond_to?(:blank?)
    value.blank?
  elsif value.respond_to?(:empty?)
    !!value.empty?
  else
    !value
  end
end
mask(value, keep_last = 4, mask_with = 'X') click to toggle source

Only show the last N positions in a string, replace the rest with the mask_with value. Example:

  • 123-45-6789 becomes: XXXXXXX6789

  • ABCDEFG becomes: XXXDEFG

# File lib/cinnamon_serial/formatting.rb, line 19
def mask(value, keep_last = 4, mask_with = 'X')
  string_value = value.to_s
  return string_value if blank?(string_value) || string_value.size <= keep_last

  (mask_with.to_s * (string_value.size - keep_last)) + string_value[-keep_last..-1]
end
percent(num) click to toggle source
# File lib/cinnamon_serial/formatting.rb, line 26
def percent(num)
  present?(num) ? format('%.2f %%', num) : ''
end
present?(value) click to toggle source
# File lib/cinnamon_serial/formatting.rb, line 30
def present?(value)
  !blank?(value)
end