Sha256: e9e5e2a85a4b11e420e488ea2a5f0b9144e8572d01790e0f49fd3b79d0fe6c2d

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
# Allows Writing of 100.to_money for +Numeric+ types
#   100.to_money => #<Money @cents=10000>
#   100.37.to_money => #<Money @cents=10037>
class Numeric
  def to_money(currency = nil)
    Money.new(self, currency)
  end
end

# Allows Writing of '100'.to_money for +String+ types
# Excess characters will be discarded
#   '100'.to_money => #<Money @cents=10000>
#   '100.37'.to_money => #<Money @cents=10037>
class String
  def to_money(currency = nil)
    currency = Money::Helpers.value_to_currency(currency)

    unless Money.config.legacy_deprecations
      return Money.new(self, currency)
    end

    Money::Parser::Fuzzy.parse(self, currency).tap do |money|
      new_value = BigDecimal(self, exception: false)&.round(currency.minor_units)
      old_value = money.value

      if new_value != old_value
        message = "`\"#{self}\".to_money` will soon behave like `Money.new(\"#{self}\")` and "
        message +=
          if new_value.nil?
            "raise an ArgumentError exception. Use the browser's locale to parse money strings."
          else
            "return #{new_value} instead of #{old_value}."
          end
        Money.deprecate(message)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shopify-money-2.2.2 lib/money/core_extensions.rb