Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ on:
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4']
ruby-version: ['3.1', '3.2', '3.3', '3.4', 'jruby-9.4', 'jruby-10.0']

steps:
- uses: actions/checkout@v4
- name: Set up Java for JRuby 10.0
if: matrix.ruby-version == 'jruby-10.0'
uses: actions/setup-java@v5
with:
distribution: 'temurin'
java-version: '21'
- uses: actions/checkout@v6
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## Current
## Unreleased

## 2.0.0
- **Breaking change**: Remove deprecated `Monetize.extract_cents`.
- **Breaking change**: Update Money gem dependency to ~> 7.0. See the [Money 7.0 upgrading guide](https://github.com/RubyMoney/money/blob/main/UPGRADING-7.0.md)
- **Breaking change**: Drop support for Ruby < 3.1
- Fix parsing multiple delimeters in the amount, after BigDecimal updates
- Fix unused variable `possible_major` Ruby warning.

Expand Down
11 changes: 2 additions & 9 deletions lib/monetize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class << self
attr_accessor :enforce_currency_delimiters


# Where this set to true, the behavior for parsing thousands separators is changed to
# expect that eg. €10.000 is EUR 10 000 and not EUR 10.000 - it's incredibly rare when parsing
# Where this set to true, the behavior for parsing thousands separators is changed to
# expect that eg. €10.000 is EUR 10 000 and not EUR 10.000 - it's incredibly rare when parsing
# human text that we're dealing with fractions of cents.
attr_accessor :expect_whole_subunits

Expand Down Expand Up @@ -70,12 +70,5 @@ def from_numeric(value, currency = Money.default_currency)
fail ArgumentError, "'value' should be a type of Numeric" unless value.is_a?(Numeric)
Money.from_amount(value, currency)
end

def extract_cents(input, currency = Money.default_currency)
warn '[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents'

money = parse(input, currency)
money.cents if money
end
end
end
7 changes: 4 additions & 3 deletions lib/monetize/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Parser
'NT$'=> 'TWD',
'β‚±' => 'PHP',
}

CURRENCY_SYMBOL_REGEX = /(?<![A-Z])(#{CURRENCY_SYMBOLS.keys.map { |key| Regexp.escape(key) }.join('|')})(?![A-Z])/i
MULTIPLIER_SUFFIXES = { 'K' => 3, 'M' => 6, 'B' => 9, 'T' => 12 }
MULTIPLIER_SUFFIXES.default = 0
Expand Down Expand Up @@ -80,8 +80,9 @@ def parse_currency
computed_currency = nil unless Monetize::Parser::CURRENCY_SYMBOLS.value?(computed_currency)
computed_currency ||= compute_currency if assume_from_symbol?


computed_currency || fallback_currency || Money.default_currency
found = computed_currency || fallback_currency || Money.default_currency
raise Money::Currency::UnknownCurrency unless found
Copy link
Member Author

@yukideluxe yukideluxe Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunny I found the commit that caused this! RubyMoney/money@5c71447

Before adding the @default_currency.nil? condition, the code was initializing a currency with Money::Currency.new(nil) and then the Money::Currency::UnknownCurrency which was causing Money.default_currency to raise that error.

One could argue that we should differentiate between the Money::Currency::NoCurrency and Money::Currency::UnknownCurrency errors but I think this is OK to avoid touching more code and keep backwards compatibility. We are wrapping the error on Monetize::ParseError instead, anyways! πŸ™πŸ»

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for looking more into this! πŸ‘πŸ»

found
end

def assume_from_symbol?
Expand Down
2 changes: 1 addition & 1 deletion lib/monetize/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8

module Monetize
VERSION = '1.13.0'
VERSION = '2.0.0'
end
2 changes: 1 addition & 1 deletion monetize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'money', '~> 6.12'
spec.add_dependency 'money', '~> 7.0'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
Expand Down
12 changes: 6 additions & 6 deletions spec/core_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
'-1,000' => Money.new(-1_000_00),
'1,000.5' => Money.new(1_000_50),
'1,000.51' => Money.new(1_000_51),
'1,000.505' => Money.new(1_000_50), # ROUND_HALF_EVEN default bankers rounding
'1,000.515' => Money.new(1_000_52), # ROUND_HALF_EVEN default bankers rounding
'1,000.505' => Money.new(1_000_51), # ROUND_HALF_UP default rounding
'1,000.515' => Money.new(1_000_52), # ROUND_HALF_UP default rounding
'1,000.504' => Money.new(1_000_50),
'1,000.0000' => Money.new(1_000_00),
'1,000.5000' => Money.new(1_000_50),
Expand Down Expand Up @@ -151,16 +151,16 @@
expect('1.5'.to_money('KWD').cents).to eq 1_500
end

it 'respects Money.rounding_mode' do
it 'respects Money.with_rounding_mode' do
expect('1.009'.to_money).to eq(Money.new(1_01))

Money.rounding_mode(BigDecimal::ROUND_DOWN) do
Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do
expect('1.009'.to_money).to eq(Money.new(1_00))
end

expect('1.001'.to_money).to eq(Money.new(1_00))

Money.rounding_mode(BigDecimal::ROUND_UP) do
Money.with_rounding_mode(BigDecimal::ROUND_UP) do
expect('1.001'.to_money).to eq(Money.new(1_01))
end
end
Expand Down Expand Up @@ -230,7 +230,7 @@
expect(hash.to_money).to eq(Money.new(100, 'USD'))
end
end

context 'when Money to_hash is used' do
subject(:hash) { { cents: 100, currency_iso: 'SGD' } }

Expand Down
28 changes: 4 additions & 24 deletions spec/monetize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
end

it 'raises an error if currency code is invalid' do
expect { '20.00 OMG'.to_money }.to raise_error
expect { '20.00 OMG'.to_money }.to raise_error(Monetize::ParseError)
end
end
end
Expand Down Expand Up @@ -545,13 +545,13 @@
expect(m.currency).to eq Money::Currency.wrap('EUR')
end

context 'infinite_precision = true' do
context 'default_infinite_precision = true' do
before do
Money.infinite_precision = true
Money.default_infinite_precision = true
end

after do
Money.infinite_precision = false
Money.default_infinite_precision = false
end

it 'keeps precision' do
Expand Down Expand Up @@ -605,26 +605,6 @@
end
end

describe '.extract_cents' do
it 'is deprecated' do
allow(Monetize).to receive(:warn)

Monetize.extract_cents('100')

expect(Monetize)
.to have_received(:warn)
.with('[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents')
end

it 'extracts cents from a given string' do
expect(Monetize.extract_cents('10.99')).to eq(1099)
end

it "correctly treats pipe marks '|' in input (regression test)" do
expect(Monetize.extract_cents('100|0')).to eq Monetize.extract_cents('100!0')
end
end

context 'given the same inputs to .parse and .from_*' do
it 'gives the same results' do
expect(4.635.to_money).to eq '4.635'.to_money
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'money'

Money.default_currency = Money::Currency.new("USD")

RSpec.configure do |config|
config.order = 'random'
end