moolah 5.0.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 115af710c730393a90c68062b1ab169762039a21262e37436253903bd212c6fd
4
- data.tar.gz: '06296e3e90b8d761400150bb5338dcf442fa5b84efde22bc9b83d56146dfe9a9'
3
+ metadata.gz: 259bd848b9c02a4cbec6ae4b48100192caff0c00b9315c82a6dd7b81372711cc
4
+ data.tar.gz: ac0a29f2335095310e45bc3a597d7a4c7db644e7f15d9262d0f05fd5717d1965
5
5
  SHA512:
6
- metadata.gz: 2625001e06725cef62bb406554c015606fbbf82679d039d74bd715432ec9312b6fa0b07b3496dba4853539ee26bdaad17017d243a088656a9925268ceb09a381
7
- data.tar.gz: 34fa35fde5cfd8819cddf8ce89c88509ad6989b73a89e147f8c1f34cfcf03f0119a2f4c72505cf610a041adc93ca8b059324d1900df957628aa8c3de0b034f04
6
+ metadata.gz: 706e6c87160955d2cc3cde1e238e79a7f6c0e2a66d2159591dd0227c4008e4ac30cf7aee1f235b0da3f771f3444393de86642390d50ee2d1f669d9837a1bd2a5
7
+ data.tar.gz: c2b621ca8644131cab99bdf0e13b16447e257472235c54917865bb03695e6af8b92f5343febef98296c69ca86cbecfdca5d95b52e0bd0991942edb9d2500aaed
@@ -14,7 +14,7 @@ module Moolah
14
14
  end
15
15
 
16
16
  def format_fx_rate
17
- ('%.2f' % money.fx_rate)
17
+ sprintf('%.4f', money.fx_rate)
18
18
  end
19
19
 
20
20
  private
data/lib/moolah/money.rb CHANGED
@@ -7,11 +7,12 @@ module Moolah
7
7
  include Comparable
8
8
 
9
9
  attribute :amount, BigDecimal, default: BigDecimal(0)
10
- attribute :currency, String, default: 'AUD'
10
+ attribute :currency, String
11
11
  attribute :fx_rate, BigDecimal
12
12
 
13
13
  def initialize(args = {})
14
- args[:fx_rate] = 1.0 if args[:currency] == 'AUD'
14
+ args[:currency] = 'AUD' if args[:currency].nil? || args[:currency] == ''
15
+ args[:fx_rate] = '1.0000' if args[:currency] == 'AUD'
15
16
 
16
17
  raise ArgumentError.new('missing keyword: :fx_rate') unless args[:fx_rate]
17
18
 
@@ -34,6 +35,10 @@ module Moolah
34
35
  formatter.format(symbol: false, delimit: false)
35
36
  end
36
37
 
38
+ def formatted_fx_rate
39
+ formatter.format_fx_rate
40
+ end
41
+
37
42
  def amount_in_cents
38
43
  (bankers_rounded.amount * 100).to_i
39
44
  end
@@ -56,18 +61,22 @@ module Moolah
56
61
 
57
62
  def +(money)
58
63
  return self unless money
59
- raise ArgumentError, 'Cannot add two Money objects with different currencies' if currency != money.currency
60
- raise ArgumentError, 'Cannot add two Money objects with different fx rates' if fx_rate != money.fx_rate
61
64
 
62
- Money.new(amount: amount + money.amount, currency: currency, fx_rate: fx_rate)
65
+ new_money = money.is_a?(Moolah::Money) ? money : Moolah::Money.new(money)
66
+ raise ArgumentError, "Cannot add currency: #{currency} with given currency: #{new_money.currency}" if currency != new_money.currency
67
+ raise ArgumentError, "Cannot add fx rate: #{formatter.format_fx_rate} with given fx rate: #{new_money.formatted_fx_rate}" if fx_rate != new_money.fx_rate
68
+
69
+ Money.new(amount: amount + new_money.amount, currency: currency, fx_rate: fx_rate)
63
70
  end
64
71
 
65
72
  def -(money)
66
73
  return self unless money
67
- raise ArgumentError, 'Cannot subtract two Money objects with different currencies' if currency != money.currency
68
- raise ArgumentError, 'Cannot subtract two Money objects with different fx rates' if fx_rate != money.fx_rate
69
74
 
70
- Money.new(amount: amount - money.amount, currency: currency, fx_rate: fx_rate)
75
+ new_money = money.is_a?(Moolah::Money) ? money : Moolah::Money.new(money)
76
+ raise ArgumentError, "Cannot subtract currency: #{currency} with given currency: #{new_money.currency}" if currency != new_money.currency
77
+ raise ArgumentError, "Cannot subtract fx rate: #{formatter.format_fx_rate} with given fx rate: #{new_money.formatted_fx_rate}" if fx_rate != new_money.fx_rate
78
+
79
+ Money.new(amount: amount - new_money.amount, currency: currency, fx_rate: fx_rate)
71
80
  end
72
81
 
73
82
  def *(_money)
@@ -83,10 +92,11 @@ module Moolah
83
92
  end
84
93
 
85
94
  def <=>(money)
86
- raise ArgumentError, 'Cannot compare two Money objects with different currencies' if currency != money.currency
87
- raise ArgumentError, 'Cannot compare two Money objects with different fx rates' if fx_rate != money.fx_rate
95
+ new_money = money.is_a?(Moolah::Money) ? money : Moolah::Money.new(money)
96
+ raise ArgumentError, "Cannot compare currency: #{currency} with given currency: #{money.currency}" if currency != new_money.currency
97
+ raise ArgumentError, "Cannot compare fx rate: #{formatter.format_fx_rate} with given fx rate: #{new_money.formatted_fx_rate}" if fx_rate != new_money.fx_rate
88
98
 
89
- amount <=> money.amount
99
+ amount <=> new_money.amount
90
100
  end
91
101
 
92
102
  def bankers_rounded
@@ -1,6 +1,6 @@
1
1
  module Moolah
2
2
  MAJOR = 5
3
- MINOR = 0
3
+ MINOR = 3
4
4
  PATCH = ENV['PATCH_VERSION'] || 0
5
5
 
6
6
  private_constant :MAJOR, :MINOR, :PATCH
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moolah
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc Lee
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-07-14 00:00:00.000000000 Z
12
+ date: 2022-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler