Class: Proforma::ExtendedEvaluator::Formatter

Inherits:
Stringento::Formatter
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/proforma/extended_evaluator/formatter.rb

Overview

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.

Constant Summary collapse

DEFAULTS =
{
  currency_code: 'USD',
  currency_round: 2,
  currency_symbol: '$',
  date_format: '%m/%d/%Y',
  decimal_separator: '.',
  iso_date_format: '%Y-%m-%d',
  mask_char: 'X',
  false_value: 'No',
  null_value: 'Unknown',
  nullish_regex: /\A(nil|null)\z/i.freeze,
  thousands_regex: /[0-9](?=(?:[0-9]{3})+(?![0-9]))/.freeze,
  thousands_separator: ',',
  true_value: 'Yes',
  truthy_regex: /\A(true|t|yes|y|1)\z/i.freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Formatter

Returns a new instance of Formatter.



53
54
55
# File 'lib/proforma/extended_evaluator/formatter.rb', line 53

def initialize(opts = {})
  @options = OpenStruct.new(DEFAULTS.merge(opts))
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/proforma/extended_evaluator/formatter.rb', line 35

def options
  @options
end

Instance Method Details

#boolean_formatter(value, nullable) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



70
71
72
73
74
75
76
# 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) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



89
90
91
92
93
94
95
# 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