Sha256: ba0bb3c7479e28e55f0556cea9a0e48db91a13fc28d111821424dfcdfc783685

Contents?: true

Size: 915 Bytes

Versions: 2

Compression:

Stored size: 915 Bytes

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?

          value = "%0.#{precision(options)}f" % [value.sub(separator(options), '.')]
          value = value.delete('.') if cents?(options)

          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 separator(options)
          options.fetch :separator, I18n.t('currency.format.separator', default: '.')
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
normalizy-1.0.0 lib/normalizy/filters/money.rb
normalizy-0.2.0 lib/normalizy/filters/money.rb