Sha256: 98a0e4ec7820395c189386152b9693b3a103d8a7dc02f55fe9cd177d2f4756e3
Contents?: true
Size: 1.09 KB
Versions: 4
Compression:
Stored size: 1.09 KB
Contents
# frozen_string_literal: true module Normalizy module Filters module Money class << self def call(input, options = {}) return input unless input.is_a?(String) value = input.gsub(/[^[0-9-]#{separator(options)}]/, '') return nil if value.blank? if cents?(options) value = precisioned(value, options).delete('.') if value.include? separator(options) else value = precisioned(value, options) end return value.send(options[:cast]) if options[:cast] value end private def cents?(options) options[:type]&.to_sym == :cents end def precision(options) options.fetch :precision, I18n.t('currency.format.precision', default: 2) end def precisioned(value, options) format("%0.#{precision(options)}f", value.sub(separator(options), '.')) end def separator(options) options.fetch :separator, I18n.t('currency.format.separator', default: '.') end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems