moolah 5.3.0 → 5.5.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: 259bd848b9c02a4cbec6ae4b48100192caff0c00b9315c82a6dd7b81372711cc
4
- data.tar.gz: ac0a29f2335095310e45bc3a597d7a4c7db644e7f15d9262d0f05fd5717d1965
3
+ metadata.gz: e1d1a1e892be6a4317fb014f59b05ab26db3b9e1d38272b89e59fe76bb005c9a
4
+ data.tar.gz: b60ac538ac82a63ffa21afdf643d41e7da02144e3ffa01b211caa003379374c0
5
5
  SHA512:
6
- metadata.gz: 706e6c87160955d2cc3cde1e238e79a7f6c0e2a66d2159591dd0227c4008e4ac30cf7aee1f235b0da3f771f3444393de86642390d50ee2d1f669d9837a1bd2a5
7
- data.tar.gz: c2b621ca8644131cab99bdf0e13b16447e257472235c54917865bb03695e6af8b92f5343febef98296c69ca86cbecfdca5d95b52e0bd0991942edb9d2500aaed
6
+ metadata.gz: cc2acc4608c640be60f8833ecfedee7d56a41fe8fca58b21c521052b47d85c4ac93fa1bab37333eb8ce4c26fce7e189eaf65a679bf5fbd8606f4a52a1b079547
7
+ data.tar.gz: 31728aa78f418380e551a2be5dfbbe7b9e6bf3ba889663d5522fb00af5b254e2a2cfde8e2a94b8c64481b2a8c78e87674476606b8add8814cc014d22eb533116
data/README.md CHANGED
@@ -1,13 +1,9 @@
1
1
  # Moolah
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/moolah`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Moolah is a Money gem that deals with Currency and FX_RATE. It is being used in multiple repos in Hotels/Bookings.
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
10
-
11
7
  ```ruby
12
8
  gem 'moolah'
13
9
  ```
@@ -20,9 +16,48 @@ Or install it yourself as:
20
16
 
21
17
  $ gem install moolah
22
18
 
19
+ ### How to upgrade to version 5
20
+
21
+ For version 5, `fx_rate` is a must field while initializing a Moolah::Money object with different currency other than AUD.
22
+ When you initialize Moolah::Money with AUD as the currency, the fx_rate will be default to 1.0
23
+
24
+ This will break projects that are using older Moolah version expect the project is using AUD as the main currency.
25
+
26
+ ```ruby
27
+ gem 'moolah', -> '5.3'
28
+ ```
29
+
30
+ ```
31
+ $ bundle update moolah
32
+ ```
33
+
23
34
  ## Usage
24
35
 
25
- TODO: Write usage instructions here
36
+ ### To create a new Moolah::Money object
37
+ ```ruby
38
+ Moolah::Money.new(amount: '10', currency: 'AUD', fx_rate: '1.0000')
39
+ ```
40
+
41
+ ### To create a AUD dollar Moolah::Money object
42
+ ```ruby
43
+ Moolah::Money.in_aud('10')
44
+ ```
45
+
46
+ ### To create a zero dollar Moolah::Money object
47
+ ```ruby
48
+ Moolah::Money.zero(currency: 'AUD', fx_rate: '1.0000')
49
+ ```
50
+
51
+ ### To display a json format of the Moolah::Money object
52
+ ```ruby
53
+ money = Moolah::Money.new(amount: '10', currency: 'AUD', fx_rate: '1.0000')
54
+ money.as_json
55
+ => {
56
+ 'amount' => '10.00',
57
+ 'currency' => 'AUD',
58
+ 'fx_rate' => '1.0000'
59
+ }
60
+ ```
26
61
 
27
62
  ## Development
28
63
 
@@ -9,7 +9,7 @@ module Moolah
9
9
  def load_csv(file)
10
10
  currencies = {}
11
11
 
12
- CSV.foreach(file, headers: true, header_converters: :symbol, encoding: 'ISO-8859-1') do |row|
12
+ CSV.foreach(file, headers: true, header_converters: :symbol, encoding: 'UTF-8') do |row|
13
13
  code = row[:code]
14
14
  currencies[code] = Currency.new(
15
15
  name: row[:currency],
data/lib/moolah/money.rb CHANGED
@@ -1,30 +1,55 @@
1
1
  require 'anemic/model'
2
2
  require 'bigdecimal'
3
+ require 'active_support'
3
4
 
4
5
  module Moolah
5
6
  class Money
6
7
  include Anemic::Model
7
8
  include Comparable
8
9
 
10
+ delegate :zero?, :positive?, :negative?, to: :amount
11
+
9
12
  attribute :amount, BigDecimal, default: BigDecimal(0)
10
13
  attribute :currency, String
11
14
  attribute :fx_rate, BigDecimal
12
15
 
13
16
  def initialize(args = {})
14
- args[:currency] = 'AUD' if args[:currency].nil? || args[:currency] == ''
17
+ args[:currency] = 'AUD' if args[:currency].blank?
15
18
  args[:fx_rate] = '1.0000' if args[:currency] == 'AUD'
16
19
 
17
- raise ArgumentError.new('missing keyword: :fx_rate') unless args[:fx_rate]
20
+ if args[:fx_rate].nil?
21
+ # So fx_rate appears as nil as part of the error message
22
+ args[:fx_rate] = nil
23
+ raise ArgumentError, "missing keyword: :fx_rate in #{self.class.name} (#{args.sort.to_h})"
24
+ end
18
25
 
19
26
  super(args)
20
27
  end
21
28
 
22
29
  def self.in_aud(amount)
23
- Money.new(currency: 'AUD', amount: amount)
30
+ new(amount: amount)
31
+ end
32
+
33
+ def self.zero(currency = 'AUD', fx_rate = nil)
34
+ new(currency: currency, amount: 0, fx_rate: fx_rate)
24
35
  end
25
36
 
26
- def self.zero(currency='AUD', fx_rate=1)
27
- Money.new(currency: currency, amount: 0, fx_rate: fx_rate)
37
+ def to_aud
38
+ return Money.new if fx_rate.zero?
39
+
40
+ Money.new(amount: amount / fx_rate)
41
+ end
42
+
43
+ def convert_to(other)
44
+ money = other.is_a?(Money) ? other : Money.new(other)
45
+
46
+ converted = Money.new(
47
+ amount: to_aud.amount * money.fx_rate,
48
+ currency: money.currency,
49
+ fx_rate: money.fx_rate
50
+ )
51
+
52
+ converted.bankers_rounded
28
53
  end
29
54
 
30
55
  def formatted(symbol: true, delimit: true)
@@ -59,31 +84,31 @@ module Moolah
59
84
  "#{self.class.name} (#{as_json})"
60
85
  end
61
86
 
62
- def +(money)
63
- return self unless money
87
+ def +(other)
88
+ return self if other.nil?
64
89
 
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
90
+ money = other.is_a?(Money) ? other : Money.new(other)
91
+ raise ArgumentError, "Cannot add currency: #{currency} with given currency: #{money.currency}" if currency != money.currency
92
+ raise ArgumentError, "Cannot add fx rate: #{formatter.format_fx_rate} with given fx rate: #{money.formatted_fx_rate}" if fx_rate != money.fx_rate
68
93
 
69
- Money.new(amount: amount + new_money.amount, currency: currency, fx_rate: fx_rate)
94
+ Money.new(amount: amount + money.amount, currency: currency, fx_rate: fx_rate)
70
95
  end
71
96
 
72
- def -(money)
73
- return self unless money
97
+ def -(other)
98
+ return self if other.nil?
74
99
 
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
100
+ money = other.is_a?(Money) ? other : Money.new(other)
101
+ raise ArgumentError, "Cannot subtract currency: #{currency} with given currency: #{money.currency}" if currency != money.currency
102
+ raise ArgumentError, "Cannot subtract fx rate: #{formatter.format_fx_rate} with given fx rate: #{money.formatted_fx_rate}" if fx_rate != money.fx_rate
78
103
 
79
- Money.new(amount: amount - new_money.amount, currency: currency, fx_rate: fx_rate)
104
+ Money.new(amount: amount - money.amount, currency: currency, fx_rate: fx_rate)
80
105
  end
81
106
 
82
- def *(_money)
107
+ def *(_other)
83
108
  raise ArgumentError, 'Cannot multiply Money objects due to rounding issues'
84
109
  end
85
110
 
86
- def /(_money)
111
+ def /(_other)
87
112
  raise ArgumentError, 'Cannot divide Money objects due to rounding issues'
88
113
  end
89
114
 
@@ -91,22 +116,15 @@ module Moolah
91
116
  Money.new(amount: -amount, currency: currency, fx_rate: fx_rate)
92
117
  end
93
118
 
94
- def <=>(money)
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
98
-
99
- amount <=> new_money.amount
119
+ def <=>(other)
120
+ money = other.is_a?(Money) ? other : Money.new(other)
121
+ to_aud.amount <=> money.to_aud.amount
100
122
  end
101
123
 
102
124
  def bankers_rounded
103
125
  Rounder.new(self).bankers_rounded
104
126
  end
105
127
 
106
- def zero?
107
- amount.zero?
108
- end
109
-
110
128
  def negate
111
129
  negated_amount = amount * -1
112
130
  negated_amount = 0 if negated_amount.to_s == '-0.0'
@@ -1,6 +1,6 @@
1
1
  module Moolah
2
2
  MAJOR = 5
3
- MINOR = 3
3
+ MINOR = 5
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.3.0
4
+ version: 5.5.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-24 00:00:00.000000000 Z
12
+ date: 2022-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler