Sha256: 722ee467e4dca089096c047394112d240e9e8f0c8a67bed6f242b8c6ca1d3dfd
Contents?: true
Size: 1.11 KB
Versions: 2
Compression:
Stored size: 1.11 KB
Contents
# frozen_string_literal: true module Normalizy module Filters module Percent 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('number.percentage.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('number.percentage.format.separator', default: '.')) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
normalizy-1.7.0 | lib/normalizy/filters/percent.rb |
normalizy-1.6.0 | lib/normalizy/filters/percent.rb |