Sha256: 9d5e5f62fc7b209cec7072036675ac8885ea099150d860f7cc4ac98dbaac2c80

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'bigdecimal'
require 'bigdecimal/util'
require_relative './exceptions'

module Mint
  # Special utilities
  class Utils
    class << self
      # Normalize string to our keys format
      # @example
      #   'usd' -> :USD
      # @param [String,Symbol]
      # @return [Symbol]
      def to_key(key)
        key.to_s.upcase.to_sym
      end

      # Normalize and guess amount's type
      # @example
      #   '1.178' -> <BigDecimal: 1.18>
      # @param value [BigDecimal]
      # @param currency [Symbol]
      # @return [BigDecimal]
      def to_amount(value, currency)
        to_bigdecimal(value).round(precision(currency))
      end

      # Format number as string with correct precision
      # @param value [BigDecimal]
      # @param currency [Symbol]
      # @return [String]
      def to_format(value, currency)
        format("%.#{precision(currency)}f", value.to_f)
      end

      private

      # Return precision for round or default is 2
      # @param currency [String,Symbol]
      # @return [Integer]
      def precision(currency)
        (Mint::Currency.precisions || {}).fetch(to_key(currency), 2)
      end

      # Trying to guess type
      # @param value [Mixed]
      # @return [BigDecimal]
      def to_bigdecimal(value)
        case value
        when Mint::Money
          value.value
        when Rational
          value.to_d(16)
        else
          value.respond_to?(:to_d) ? value.to_d : raise(WrongMoneyError, value)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mint-money-0.1.1 lib/mint/money/utils.rb