diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 809ddb16c..cc5ee0d18 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index d93908f71..16b537728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Gemfile b/Gemfile index 14dee0295..500ec730b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,17 +1,6 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -# JSON and I18n gem no longer supports ruby < 2.0.0 -if defined?(JRUBY_VERSION) - gem 'json' -elsif RUBY_VERSION =~ /^1/ - gem 'json', '~> 1.8.3' - gem 'tins', '~> 1.6.0' - gem 'term-ansicolor', '~> 1.3.0' - gem 'i18n', '~> 0.9' -end - -if RUBY_VERSION >= '3.4.0' - gem 'bigdecimal' -end +gem "rake" +gem "rspec", "~> 3.0" gemspec diff --git a/lib/monetize.rb b/lib/monetize.rb index dfedaba7d..1ec63d360 100644 --- a/lib/monetize.rb +++ b/lib/monetize.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'money' require 'monetize/core_extensions' require 'monetize/errors' @@ -21,8 +19,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 @@ -70,12 +68,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 diff --git a/lib/monetize/collection.rb b/lib/monetize/collection.rb index 9e288728f..7c79ff9e4 100644 --- a/lib/monetize/collection.rb +++ b/lib/monetize/collection.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'forwardable' module Monetize diff --git a/lib/monetize/core_extensions.rb b/lib/monetize/core_extensions.rb index 510c6b7d4..98908c065 100644 --- a/lib/monetize/core_extensions.rb +++ b/lib/monetize/core_extensions.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'monetize/core_extensions/nil_class' require 'monetize/core_extensions/numeric' require 'monetize/core_extensions/string' diff --git a/lib/monetize/core_extensions/hash.rb b/lib/monetize/core_extensions/hash.rb index a34826aa5..3dec40fc9 100644 --- a/lib/monetize/core_extensions/hash.rb +++ b/lib/monetize/core_extensions/hash.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - class Hash def to_money(currency = nil) money_hash = self.respond_to?(:with_indifferent_access) ? self.with_indifferent_access : self diff --git a/lib/monetize/core_extensions/numeric.rb b/lib/monetize/core_extensions/numeric.rb index 4ed290826..3dea284e6 100644 --- a/lib/monetize/core_extensions/numeric.rb +++ b/lib/monetize/core_extensions/numeric.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - class Numeric def to_money(currency = nil) Monetize.from_numeric(self, currency || Money.default_currency) diff --git a/lib/monetize/core_extensions/string.rb b/lib/monetize/core_extensions/string.rb index 812defd18..c8daa6f1e 100644 --- a/lib/monetize/core_extensions/string.rb +++ b/lib/monetize/core_extensions/string.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - class String def to_money(currency = nil) Monetize.parse!(self, currency) diff --git a/lib/monetize/core_extensions/symbol.rb b/lib/monetize/core_extensions/symbol.rb index a5d1a2ac0..f5d6f90c8 100644 --- a/lib/monetize/core_extensions/symbol.rb +++ b/lib/monetize/core_extensions/symbol.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - class Symbol def to_currency Money::Currency.new(self) diff --git a/lib/monetize/parser.rb b/lib/monetize/parser.rb index bace626ad..a91810c2c 100644 --- a/lib/monetize/parser.rb +++ b/lib/monetize/parser.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - module Monetize class Parser CURRENCY_SYMBOLS = { @@ -30,7 +28,7 @@ class Parser 'NT$'=> 'TWD', '₱' => 'PHP', } - + CURRENCY_SYMBOL_REGEX = /(? 3, 'M' => 6, 'B' => 9, 'T' => 12 } MULTIPLIER_SUFFIXES.default = 0 @@ -80,8 +78,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 + found end def assume_from_symbol? diff --git a/lib/monetize/version.rb b/lib/monetize/version.rb index 7a7d8eb6c..e48b66a49 100644 --- a/lib/monetize/version.rb +++ b/lib/monetize/version.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - module Monetize - VERSION = '1.13.0' + VERSION = '2.0.0' end diff --git a/monetize.gemspec b/monetize.gemspec index 9467b2a52..9827951db 100644 --- a/monetize.gemspec +++ b/monetize.gemspec @@ -1,34 +1,31 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'monetize/version' -require 'English' +require "monetize/version" -Gem::Specification.new do |spec| - spec.name = 'monetize' - spec.version = Monetize::VERSION - spec.authors = ['Shane Emmons', 'Anthony Dmitriyev'] - spec.email = ['shane@emmons.io', 'anthony.dmitriyev@gmail.com'] - spec.description = 'A library for converting various objects into `Money` objects.' - spec.summary = 'A library for converting various objects into `Money` objects.' - spec.homepage = 'https://github.com/RubyMoney/monetize' - spec.license = 'MIT' +Gem::Specification.new do |s| + s.name = "monetize" + s.version = Monetize::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ["Shane Emmons", "Anthony Dmitriyev"] + s.email = ["shane@emmons.io", "anthony.dmitriyev@gmail.com"] + s.homepage = "https://github.com/RubyMoney/monetize" + s.summary = "A library for converting various objects into `Money` objects." + s.description = "A library for converting various objects into `Money` objects." + s.license = "MIT" - spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ['lib'] + s.add_dependency "money", "~> 7.0" - spec.add_dependency 'money', '~> 6.12' + s.required_ruby_version = ">= 3.1" - spec.add_development_dependency 'bundler' - spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec', '~> 3.0' + s.files = `git ls-files -z -- lib/* CHANGELOG.md LICENSE monetize.gemspec README.md`.split("\x0") + s.require_paths = ["lib"] - if spec.respond_to?(:metadata) - spec.metadata['changelog_uri'] = 'https://github.com/RubyMoney/monetize/blob/master/CHANGELOG.md' - spec.metadata['source_code_uri'] = 'https://github.com/RubyMoney/monetize/' - spec.metadata['bug_tracker_uri'] = 'https://github.com/RubyMoney/monetize/issues' - spec.metadata['rubygems_mfa_required'] = 'true' + if s.respond_to?(:metadata) + s.metadata["changelog_uri"] = "https://github.com/RubyMoney/monetize/blob/master/CHANGELOG.md" + s.metadata["source_code_uri"] = "https://github.com/RubyMoney/monetize/" + s.metadata["bug_tracker_uri"] = "https://github.com/RubyMoney/monetize/issues" + s.metadata["rubygems_mfa_required"] = "true" end end diff --git a/spec/core_extensions_spec.rb b/spec/core_extensions_spec.rb index 96b063359..612cf9172 100644 --- a/spec/core_extensions_spec.rb +++ b/spec/core_extensions_spec.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'spec_helper' require 'monetize' require 'monetize/core_extensions' @@ -67,8 +65,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), @@ -151,16 +149,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 @@ -230,7 +228,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' } } diff --git a/spec/monetize_spec.rb b/spec/monetize_spec.rb index 00aad8002..f71779387 100644 --- a/spec/monetize_spec.rb +++ b/spec/monetize_spec.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'spec_helper' require 'monetize' @@ -192,7 +190,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 @@ -545,13 +543,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 @@ -605,26 +603,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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 51f6235e2..186a07675 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ -# encoding: utf-8 - require 'money' +Money.default_currency = Money::Currency.new("USD") + RSpec.configure do |config| config.order = 'random' end