Class: CinnamonSerial::Formatting
- Inherits:
-
Object
- Object
- CinnamonSerial::Formatting
- Defined in:
- lib/cinnamon_serial/formatting.rb
Overview
Static utility methods for general use.
Class Method Summary collapse
- .blank?(value) ⇒ Boolean
-
.mask(value, keep_last = 4, mask_with = 'X') ⇒ Object
Only show the last N positions in a string, replace the rest with the mask_with value.
- .percent(num) ⇒ Object
- .present?(value) ⇒ Boolean
Class Method Details
.blank?(value) ⇒ Boolean
34 35 36 37 38 39 40 41 42 |
# 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') ⇒ Object
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
19 20 21 22 23 24 |
# 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) ⇒ Object
26 27 28 |
# File 'lib/cinnamon_serial/formatting.rb', line 26 def percent(num) present?(num) ? format('%.2f %%', num) : '' end |
.present?(value) ⇒ Boolean
30 31 32 |
# File 'lib/cinnamon_serial/formatting.rb', line 30 def present?(value) !blank?(value) end |