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_float? 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 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] }.merge(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 }.merge!(i18n_options)) end
def valid_float?
def valid_float? Float(number, exception: false) end