class Proforma::ExtendedEvaluator::Formatter
This library uses Stringento for its string-based formatting. This class is meant to be plugged into Stringento to provide formatting for data types, such as: strings, dates, currency, numbers, etc.
Constants
- DEFAULTS
Attributes
options[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 53 def initialize(opts = {}) @options = OpenStruct.new(DEFAULTS.merge(opts)) end
Public Instance Methods
boolean_formatter(value, nullable)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 97 def boolean_formatter(value, nullable) nullable = nullable.to_s == 'nullable' if nullable && nully?(value) null_value elsif truthy?(value) true_value else false_value end end
currency_formatter(value, _arg)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 78 def currency_formatter(value, _arg) return '' if null_or_empty?(value) prefix = null_or_empty?(currency_symbol) ? '' : currency_symbol suffix = null_or_empty?(currency_code) ? '' : " #{currency_code}" formatted_value = number_formatter(value, currency_round) "#{prefix}#{formatted_value}#{suffix}" end
date_formatter(value, _arg)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 70 def date_formatter(value, _arg) return '' if null_or_empty?(value) date = Date.strptime(value.to_s, iso_date_format) date.strftime(date_format) end
left_mask_formatter(value, keep_last)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 57 def left_mask_formatter(value, keep_last) keep_last = keep_last.to_s.empty? ? 4 : keep_last.to_s.to_i raise ArgumentError, "keep_last cannot be negative (#{keep_last})" if keep_last.negative? string_value = value.to_s return '' if null_or_empty?(string_value) return value if string_value.length <= keep_last mask(string_value, keep_last) end
number_formatter(value, decimal_places)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 89 def number_formatter(value, decimal_places) decimal_places = decimal_places.to_s.empty? ? 6 : decimal_places.to_s.to_i format("%0.#{decimal_places}f", value || 0) .gsub(thousands_regex, "\\0#{thousands_separator}") .gsub('.', decimal_separator) end
Private Instance Methods
mask(string, keep_last)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 111 def mask(string, keep_last) unmasked_part = string[-keep_last..-1] masked_char_count = string.size - keep_last (mask_char * masked_char_count) + unmasked_part end
null_or_empty?(val)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 118 def null_or_empty?(val) val.nil? || val.to_s.empty? end
nully?(val)
click to toggle source
rubocop:disable Style/DoubleNegation
# File lib/proforma/extended_evaluator/formatter.rb, line 123 def nully?(val) null_or_empty?(val) || !!(val.to_s =~ nullish_regex) end
truthy?(val)
click to toggle source
# File lib/proforma/extended_evaluator/formatter.rb, line 127 def truthy?(val) !!(val.to_s =~ truthy_regex) end