moolah 5.2.0 → 5.6.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: 6d6d273ed7273e3e1f788c93369570aeed0f4b4040abb8d494b84b8f3a0eda21
4
- data.tar.gz: cc68a4a6bf14797382ef669ad34ef16decd87fdb7f11028067bffa62b59c9d11
3
+ metadata.gz: 0cfb80941d30fee4042a1186672a5463fc99d2e0b4700f1ef6138862ce5b7a19
4
+ data.tar.gz: ab463d36b7d644ad8f6ec3adb4a1d8104e1348623558997c7f5c66a0133166bc
5
5
  SHA512:
6
- metadata.gz: 4bb4d5be819af9158b0b1491e62ed1b914fe410365168e31ca777b0e70e2e1e943da4337f6a7d6d38dab51dd2bdb8af4b4b3366d12b8c75a303f8646de59fa14
7
- data.tar.gz: d188591ef2f06d424d5ba5b4c34d6fc94a334e1c1ca3494db2ddd52ec24403842fd1fcfe0f525586435d22c6fdc6e94b0c2dbe53536f1d03cb18c9a8a315d281
6
+ metadata.gz: '0889eaa959a7730ddbad5775f94d13b80cd316d5d6abf873d77fa9407cc43e2ab37c4d6f0f66ded04e4e49cb704bc81fa6931214b0864418d3cf70cee7364cf1'
7
+ data.tar.gz: 6898f7b1ea7d4ce11c885215653cac18149120f845a47f5463994a2bfa2a787f5b30409552771bc87ab57757ce64ac98c53f5df2beb861270e8f7ed73ca9d837
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,57 @@
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 = args.transform_keys(&:to_sym)
18
+
19
+ args[:currency] = 'AUD' if args[:currency].blank?
15
20
  args[:fx_rate] = '1.0000' if args[:currency] == 'AUD'
16
21
 
17
- raise ArgumentError.new('missing keyword: :fx_rate') unless args[:fx_rate]
22
+ if args[:fx_rate].nil?
23
+ # So fx_rate appears as nil as part of the error message
24
+ args[:fx_rate] = nil
25
+ raise ArgumentError, "missing keyword: :fx_rate in #{self.class.name} (#{args.sort.to_h})"
26
+ end
18
27
 
19
28
  super(args)
20
29
  end
21
30
 
22
31
  def self.in_aud(amount)
23
- Money.new(currency: 'AUD', amount: amount)
32
+ new(amount: amount)
33
+ end
34
+
35
+ def self.zero(currency = 'AUD', fx_rate = nil)
36
+ new(currency: currency, amount: 0, fx_rate: fx_rate)
24
37
  end
25
38
 
26
- def self.zero(currency='AUD', fx_rate=1)
27
- Money.new(currency: currency, amount: 0, fx_rate: fx_rate)
39
+ def to_aud
40
+ return Money.new if fx_rate.zero?
41
+
42
+ Money.new(amount: amount / fx_rate)
43
+ end
44
+
45
+ def convert_to(other)
46
+ money = other.is_a?(Money) ? other : Money.new(other)
47
+
48
+ converted = Money.new(
49
+ amount: to_aud.amount * money.fx_rate,
50
+ currency: money.currency,
51
+ fx_rate: money.fx_rate
52
+ )
53
+
54
+ converted.bankers_rounded
28
55
  end
29
56
 
30
57
  def formatted(symbol: true, delimit: true)
@@ -35,6 +62,10 @@ module Moolah
35
62
  formatter.format(symbol: false, delimit: false)
36
63
  end
37
64
 
65
+ def formatted_fx_rate
66
+ formatter.format_fx_rate
67
+ end
68
+
38
69
  def amount_in_cents
39
70
  (bankers_rounded.amount * 100).to_i
40
71
  end
@@ -55,27 +86,31 @@ module Moolah
55
86
  "#{self.class.name} (#{as_json})"
56
87
  end
57
88
 
58
- def +(money)
59
- return self unless money
60
- raise ArgumentError, 'Cannot add two Money objects with different currencies' if currency != money.currency
61
- raise ArgumentError, 'Cannot add two Money objects with different fx rates' if fx_rate != money.fx_rate
89
+ def +(other)
90
+ return self if other.nil?
91
+
92
+ money = other.is_a?(Money) ? other : Money.new(other)
93
+ raise ArgumentError, "Cannot add currency: #{currency} with given currency: #{money.currency}" if currency != money.currency
94
+ raise ArgumentError, "Cannot add fx rate: #{formatter.format_fx_rate} with given fx rate: #{money.formatted_fx_rate}" if fx_rate != money.fx_rate
62
95
 
63
96
  Money.new(amount: amount + money.amount, currency: currency, fx_rate: fx_rate)
64
97
  end
65
98
 
66
- def -(money)
67
- return self unless money
68
- raise ArgumentError, 'Cannot subtract two Money objects with different currencies' if currency != money.currency
69
- raise ArgumentError, 'Cannot subtract two Money objects with different fx rates' if fx_rate != money.fx_rate
99
+ def -(other)
100
+ return self if other.nil?
101
+
102
+ money = other.is_a?(Money) ? other : Money.new(other)
103
+ raise ArgumentError, "Cannot subtract currency: #{currency} with given currency: #{money.currency}" if currency != money.currency
104
+ raise ArgumentError, "Cannot subtract fx rate: #{formatter.format_fx_rate} with given fx rate: #{money.formatted_fx_rate}" if fx_rate != money.fx_rate
70
105
 
71
106
  Money.new(amount: amount - money.amount, currency: currency, fx_rate: fx_rate)
72
107
  end
73
108
 
74
- def *(_money)
109
+ def *(_other)
75
110
  raise ArgumentError, 'Cannot multiply Money objects due to rounding issues'
76
111
  end
77
112
 
78
- def /(_money)
113
+ def /(_other)
79
114
  raise ArgumentError, 'Cannot divide Money objects due to rounding issues'
80
115
  end
81
116
 
@@ -83,21 +118,15 @@ module Moolah
83
118
  Money.new(amount: -amount, currency: currency, fx_rate: fx_rate)
84
119
  end
85
120
 
86
- def <=>(money)
87
- raise ArgumentError, 'Cannot compare two Money objects with different currencies' if currency != money.currency
88
- raise ArgumentError, 'Cannot compare two Money objects with different fx rates' if fx_rate != money.fx_rate
89
-
90
- amount <=> money.amount
121
+ def <=>(other)
122
+ money = other.is_a?(Money) ? other : Money.new(other)
123
+ to_aud.amount <=> money.to_aud.amount
91
124
  end
92
125
 
93
126
  def bankers_rounded
94
127
  Rounder.new(self).bankers_rounded
95
128
  end
96
129
 
97
- def zero?
98
- amount.zero?
99
- end
100
-
101
130
  def negate
102
131
  negated_amount = amount * -1
103
132
  negated_amount = 0 if negated_amount.to_s == '-0.0'
@@ -1,6 +1,6 @@
1
1
  module Moolah
2
2
  MAJOR = 5
3
- MINOR = 2
3
+ MINOR = 6
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.2.0
4
+ version: 5.6.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-19 00:00:00.000000000 Z
12
+ date: 2022-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler