Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/frozen string literals #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['3.1', '3.2', '3.3']
ruby-version: ['3.1', '3.2', '3.3', 'jruby-9.4']

steps:
- uses: actions/checkout@v4
Expand Down
14 changes: 4 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'pry', require: false

# JSON gem no longer supports ruby < 2.0.0
if defined?(JRUBY_VERSION)
gem 'json'
elsif RUBY_VERSION =~ /^1/
# Legacy gem locks for ruby 1.9.x
gem 'json', '~> 1.8.3'
gem 'tins', '~> 1.6.0'
gem 'term-ansicolor', '< 1.4'
end
gem 'i18n', '<= 1.2.0' if RUBY_VERSION < '2.3'
gem 'rbs', platforms: %i[mri mingw x64_mingw]
gem 'typeprof', platforms: %i[mri mingw x64_mingw]

gemspec
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ A Ruby Library for dealing with money and currency conversion.

- Your app must use UTF-8 to function with this library. There are a
number of non-ASCII currency attributes.
- This app requires JSON. If you're using JRuby < 1.7.0
you'll need to add `gem "json"` to your Gemfile or similar.

## Downloading

Expand Down Expand Up @@ -351,11 +349,11 @@ The following example implements a `Redis` store to save exchange rates to a red

class RedisRateStore
INDEX_KEY_SEPARATOR = '_TO_'.freeze

# Using second db of the redis instance
# because sidekiq uses the first db
REDIS_DATABASE = 1

# Using Hash to store rates data
REDIS_STORE_KEY = 'rates'

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/clean"
require "rspec/core/rake_task"
Expand Down
31 changes: 31 additions & 0 deletions benchmarks/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby -s
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'money'
require 'benchmark'
require_relative 'readme_examples'

I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

Benchmark.bmbm(30) do |x|
x.report('README Examples') do
10_000.times do
ReadmeExamples.call
end
end

x.report('Money#from_cents') do
100_000.times do
Money.from_cents(100, "USD")
end
end

x.report('Money#format') do
50_000.times do
Money.from_cents(100, "USD").format
end
end
end
20 changes: 20 additions & 0 deletions benchmarks/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby -s
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'money'
require 'ruby-prof'
require_relative 'readme_examples'

I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

result = RubyProf::Profile.profile do
10.times do
ReadmeExamples.call
end
end

printer = RubyProf::FlatPrinter.new(result)
printer.print($stdout, {})
19 changes: 19 additions & 0 deletions benchmarks/profile_memory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby -s
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'money'
require 'memory_profiler'
require_relative 'readme_examples'

I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

report = MemoryProfiler.report(allow_files: 'money') do
10.times do
ReadmeExamples.call
end
end

report.pretty_print
29 changes: 29 additions & 0 deletions benchmarks/readme_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module ReadmeExamples
def self.call
money = Money.from_cents(1000, "USD")
money.cents #=> 1000
money.currency #=> Currency.new("USD")

Money.from_cents(1000, "USD") == Money.from_cents(1000, "USD") #=> true
Money.from_cents(1000, "USD") == Money.from_cents(100, "USD") #=> false
Money.from_cents(1000, "USD") == Money.from_cents(1000, "EUR") #=> false
Money.from_cents(1000, "USD") != Money.from_cents(1000, "EUR") #=> true

Money.from_cents(1000, "USD") + Money.from_cents(500, "USD") == Money.from_cents(1500, "USD")
Money.from_cents(1000, "USD") - Money.from_cents(200, "USD") == Money.from_cents(800, "USD")
Money.from_cents(1000, "USD") / 5 == Money.from_cents(200, "USD")
Money.from_cents(1000, "USD") * 5 == Money.from_cents(5000, "USD")

Money.from_amount(5, "USD") == Money.from_cents(500, "USD") # 5 USD
Money.from_amount(5, "JPY") == Money.from_cents(5, "JPY") # 5 JPY
Money.from_amount(5, "TND") == Money.from_cents(5000, "TND") # 5 TND

Money.from_cents(1000, "USD").with_currency("EUR") == Money.from_cents(1000, "EUR")

Money.from_cents(100, "USD").format #=> "$1.00"
Money.from_cents(100, "GBP").format #=> "£1.00"
Money.from_cents(100, "EUR").format #=> "€1.00"
end
end
2 changes: 2 additions & 0 deletions lib/money.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bigdecimal"
require "bigdecimal/util"
require "set"
Expand Down
2 changes: 2 additions & 0 deletions lib/money/bank/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
# Provides classes that aid in the ability of exchange one currency with
# another.
Expand Down
2 changes: 2 additions & 0 deletions lib/money/bank/single_currency.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/bank/base'

class Money
Expand Down
2 changes: 2 additions & 0 deletions lib/money/bank/variable_exchange.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/bank/base'
require 'money/rates_store/memory'
require 'json'
Expand Down
2 changes: 1 addition & 1 deletion lib/money/currency.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

require "json"
require "money/currency/loader"
Expand Down
2 changes: 1 addition & 1 deletion lib/money/currency/heuristics.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

class Money
class Currency
Expand Down
2 changes: 2 additions & 0 deletions lib/money/currency/loader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
class Currency
module Loader
Expand Down
2 changes: 2 additions & 0 deletions lib/money/locale_backend/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/locale_backend/errors'

class Money
Expand Down
2 changes: 2 additions & 0 deletions lib/money/locale_backend/currency.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/locale_backend/base'

class Money
Expand Down
2 changes: 2 additions & 0 deletions lib/money/locale_backend/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
module LocaleBackend
class NotSupported < StandardError; end
Expand Down
2 changes: 2 additions & 0 deletions lib/money/locale_backend/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/locale_backend/base'

class Money
Expand Down
2 changes: 2 additions & 0 deletions lib/money/locale_backend/legacy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'money/locale_backend/base'
require 'money/locale_backend/i18n'

Expand Down
3 changes: 2 additions & 1 deletion lib/money/money.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true

require "money/bank/variable_exchange"
require "money/bank/single_currency"
require "money/money/arithmetic"
Expand Down
2 changes: 1 addition & 1 deletion lib/money/money/allocation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

class Money
class Allocation
Expand Down
2 changes: 2 additions & 0 deletions lib/money/money/arithmetic.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
module Arithmetic
# Wrapper for coerced numeric values to distinguish
Expand Down
2 changes: 2 additions & 0 deletions lib/money/money/constructors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
module Constructors

Expand Down
3 changes: 2 additions & 1 deletion lib/money/money/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: UTF-8
# frozen_string_literal: true

require 'money/money/formatting_rules'

class Money
Expand Down
2 changes: 1 addition & 1 deletion lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
# frozen_string_literal: true

class Money
class FormattingRules
Expand Down
2 changes: 1 addition & 1 deletion lib/money/money/locale_backend.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
# frozen_string_literal: true

require 'money/locale_backend/errors'
require 'money/locale_backend/legacy'
Expand Down
2 changes: 2 additions & 0 deletions lib/money/rates_store/memory.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'monitor'

class Money
Expand Down
2 changes: 2 additions & 0 deletions lib/money/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Money
VERSION = '6.19.0'
end
5 changes: 2 additions & 3 deletions money.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "money/version"
Expand All @@ -22,8 +23,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 3.4"
s.add_development_dependency "yard", "~> 0.9.11"
s.add_development_dependency "kramdown", "~> 2.3"
s.add_development_dependency "rbs" if RUBY_VERSION >= "2.7.0"
s.add_development_dependency "typeprof" if RUBY_VERSION >= "2.7.0"

s.required_ruby_version = '>= 3.1'

Expand Down
2 changes: 2 additions & 0 deletions spec/bank/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Money::Bank::Base do

describe ".instance" do
Expand Down
2 changes: 2 additions & 0 deletions spec/bank/single_currency_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Money::Bank::SingleCurrency do
describe "#exchange_with" do
it "raises when called" do
Expand Down
2 changes: 2 additions & 0 deletions spec/bank/variable_exchange_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'yaml'

Expand Down
2 changes: 1 addition & 1 deletion spec/currency/heuristics_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Currency::Heuristics do
describe "#analyze_string" do
Expand Down
2 changes: 1 addition & 1 deletion spec/currency/loader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Currency::Loader do
it "returns a currency table hash" do
Expand Down
2 changes: 1 addition & 1 deletion spec/currency_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Currency do
FOO = '{ "priority": 1, "iso_code": "FOO", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 1000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }'
Expand Down
2 changes: 1 addition & 1 deletion spec/locale_backend/currency_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::LocaleBackend::Currency do
describe '#lookup' do
Expand Down
2 changes: 1 addition & 1 deletion spec/locale_backend/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::LocaleBackend::I18n do
describe '#initialize' do
Expand Down
2 changes: 1 addition & 1 deletion spec/locale_backend/legacy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::LocaleBackend::Legacy do
after { Money.use_i18n = true }
Expand Down
2 changes: 1 addition & 1 deletion spec/money/allocation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Allocation do
describe 'given number as argument' do
Expand Down
2 changes: 1 addition & 1 deletion spec/money/arithmetic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Arithmetic do
describe "-@" do
Expand Down
2 changes: 1 addition & 1 deletion spec/money/constructors_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::Constructors do

Expand Down
2 changes: 1 addition & 1 deletion spec/money/formatting_rules_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::FormattingRules do
it 'does not modify frozen rules in place' do
Expand Down
2 changes: 1 addition & 1 deletion spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money, "formatting" do

Expand Down
2 changes: 1 addition & 1 deletion spec/money/locale_backend_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

describe Money::LocaleBackend do
describe '.find' do
Expand Down
Loading