class ActiveSupport::NumberHelper::NumberConverter
:nodoc:
def self.convert(number, options)
def self.convert(number, options) new(number, options).execute end
def default_format_options
def default_format_options options = DEFAULTS[:format].dup options.merge!(DEFAULTS[namespace][:format]) if namespace options end
def default_value(key)
def default_value(key) key.split(".").reduce(DEFAULTS) { |defaults, k| defaults[k.to_sym] } end
def execute
def execute if !number nil elsif validate_float? && !valid_bigdecimal number else convert end end
def format_options
def format_options default_format_options.merge!(i18n_format_options) end
def i18n_format_options
def i18n_format_options locale = opts[:locale] options = I18n.translate(:'number.format', locale: locale, default: {}).dup if namespace options.merge!(I18n.translate(:"number.#{namespace}.format", locale: locale, default: {})) end options end
def initialize(number, options)
def initialize(number, options) @number = number @opts = options.symbolize_keys @options = nil end
def options
def options @options ||= format_options.merge(opts) end
def translate_in_locale(key, **i18n_options)
def translate_in_locale(key, **i18n_options) translate_number_value_with_default(key, locale: options[:locale], **i18n_options) end
def translate_number_value_with_default(key, **i18n_options)
def translate_number_value_with_default(key, **i18n_options) I18n.translate(key, default: default_value(key), scope: :number, **i18n_options) end
def valid_bigdecimal
def valid_bigdecimal case number when Float, Rational number.to_d(0) when String BigDecimal(number, exception: false) else number.to_d rescue nil end end