moolah 5.3.0 → 5.7.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 +4 -4
 - data/README.md +41 -6
 - data/lib/moolah/currency_registry.rb +1 -1
 - data/lib/moolah/money.rb +49 -29
 - data/lib/moolah/version.rb +1 -1
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 642cfed3ddab0155c90c5c283aa400e4814a7ccfcaff40ba33349f0192267fbb
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a18e5d28b65c719a1f09779df3fd54c33ad24c806b58aaf06a6a6c0c29cba911
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 10403a45a27a97ed52c8ccd5d402bc6af220e7869cb3cefa0aee1cef8a65db0e5df03702b93a4c082f5509583bc1a76f38700eed611a5c4f23d93fc5538678f7
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: f78ec1432d700e5cbffe0c440b6224bd22d37c8452a8f9b51b10317940389ea52d76f49198bbf1a419a92806fa312dc3089fc719874e74056298e7e4d1930223
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,13 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # Moolah
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
       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 
     | 
    
         
            -
             
     | 
| 
      
 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: ' 
     | 
| 
      
 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 
     | 
| 
      
 17 
     | 
    
         
            +
                  args = args.transform_keys(&:to_sym) unless args.is_a?(Money)
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  args[:currency] = 'AUD' if args[:currency].blank?
         
     | 
| 
       15 
20 
     | 
    
         
             
                  args[:fx_rate] = '1.0000' if args[:currency] == 'AUD'
         
     | 
| 
       16 
21 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
                   
     | 
| 
      
 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 
     | 
    
         
            -
                   
     | 
| 
      
 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)
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def to_aud
         
     | 
| 
      
 40 
     | 
    
         
            +
                  return Money.new if fx_rate.zero?
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  Money.new(amount: amount / fx_rate)
         
     | 
| 
       24 
43 
     | 
    
         
             
                end
         
     | 
| 
       25 
44 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
                def  
     | 
| 
       27 
     | 
    
         
            -
                   
     | 
| 
      
 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)
         
     | 
| 
         @@ -59,31 +86,31 @@ module Moolah 
     | 
|
| 
       59 
86 
     | 
    
         
             
                  "#{self.class.name} (#{as_json})"
         
     | 
| 
       60 
87 
     | 
    
         
             
                end
         
     | 
| 
       61 
88 
     | 
    
         | 
| 
       62 
     | 
    
         
            -
                def +( 
     | 
| 
       63 
     | 
    
         
            -
                  return self  
     | 
| 
      
 89 
     | 
    
         
            +
                def +(other)
         
     | 
| 
      
 90 
     | 
    
         
            +
                  return self if other.nil?
         
     | 
| 
       64 
91 
     | 
    
         | 
| 
       65 
     | 
    
         
            -
                   
     | 
| 
       66 
     | 
    
         
            -
                  raise ArgumentError, "Cannot add currency: #{currency} with given currency: #{ 
     | 
| 
       67 
     | 
    
         
            -
                  raise ArgumentError, "Cannot add fx rate: #{formatter.format_fx_rate} with given fx rate: #{ 
     | 
| 
      
 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
         
     | 
| 
       68 
95 
     | 
    
         | 
| 
       69 
     | 
    
         
            -
                  Money.new(amount: amount +  
     | 
| 
      
 96 
     | 
    
         
            +
                  Money.new(amount: amount + money.amount, currency: currency, fx_rate: fx_rate)
         
     | 
| 
       70 
97 
     | 
    
         
             
                end
         
     | 
| 
       71 
98 
     | 
    
         | 
| 
       72 
     | 
    
         
            -
                def -( 
     | 
| 
       73 
     | 
    
         
            -
                  return self  
     | 
| 
      
 99 
     | 
    
         
            +
                def -(other)
         
     | 
| 
      
 100 
     | 
    
         
            +
                  return self if other.nil?
         
     | 
| 
       74 
101 
     | 
    
         | 
| 
       75 
     | 
    
         
            -
                   
     | 
| 
       76 
     | 
    
         
            -
                  raise ArgumentError, "Cannot subtract currency: #{currency} with given currency: #{ 
     | 
| 
       77 
     | 
    
         
            -
                  raise ArgumentError, "Cannot subtract fx rate: #{formatter.format_fx_rate} with given fx rate: #{ 
     | 
| 
      
 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
         
     | 
| 
       78 
105 
     | 
    
         | 
| 
       79 
     | 
    
         
            -
                  Money.new(amount: amount -  
     | 
| 
      
 106 
     | 
    
         
            +
                  Money.new(amount: amount - money.amount, currency: currency, fx_rate: fx_rate)
         
     | 
| 
       80 
107 
     | 
    
         
             
                end
         
     | 
| 
       81 
108 
     | 
    
         | 
| 
       82 
     | 
    
         
            -
                def *( 
     | 
| 
      
 109 
     | 
    
         
            +
                def *(_other)
         
     | 
| 
       83 
110 
     | 
    
         
             
                  raise ArgumentError, 'Cannot multiply Money objects due to rounding issues'
         
     | 
| 
       84 
111 
     | 
    
         
             
                end
         
     | 
| 
       85 
112 
     | 
    
         | 
| 
       86 
     | 
    
         
            -
                def /( 
     | 
| 
      
 113 
     | 
    
         
            +
                def /(_other)
         
     | 
| 
       87 
114 
     | 
    
         
             
                  raise ArgumentError, 'Cannot divide Money objects due to rounding issues'
         
     | 
| 
       88 
115 
     | 
    
         
             
                end
         
     | 
| 
       89 
116 
     | 
    
         | 
| 
         @@ -91,22 +118,15 @@ module Moolah 
     | 
|
| 
       91 
118 
     | 
    
         
             
                  Money.new(amount: -amount, currency: currency, fx_rate: fx_rate)
         
     | 
| 
       92 
119 
     | 
    
         
             
                end
         
     | 
| 
       93 
120 
     | 
    
         | 
| 
       94 
     | 
    
         
            -
                def <=>( 
     | 
| 
       95 
     | 
    
         
            -
                   
     | 
| 
       96 
     | 
    
         
            -
                   
     | 
| 
       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
         
     | 
| 
      
 121 
     | 
    
         
            +
                def <=>(other)
         
     | 
| 
      
 122 
     | 
    
         
            +
                  money = other.is_a?(Money) ? other : Money.new(other)
         
     | 
| 
      
 123 
     | 
    
         
            +
                  to_aud.amount <=> money.to_aud.amount
         
     | 
| 
       100 
124 
     | 
    
         
             
                end
         
     | 
| 
       101 
125 
     | 
    
         | 
| 
       102 
126 
     | 
    
         
             
                def bankers_rounded
         
     | 
| 
       103 
127 
     | 
    
         
             
                  Rounder.new(self).bankers_rounded
         
     | 
| 
       104 
128 
     | 
    
         
             
                end
         
     | 
| 
       105 
129 
     | 
    
         | 
| 
       106 
     | 
    
         
            -
                def zero?
         
     | 
| 
       107 
     | 
    
         
            -
                  amount.zero?
         
     | 
| 
       108 
     | 
    
         
            -
                end
         
     | 
| 
       109 
     | 
    
         
            -
             
     | 
| 
       110 
130 
     | 
    
         
             
                def negate
         
     | 
| 
       111 
131 
     | 
    
         
             
                  negated_amount = amount * -1
         
     | 
| 
       112 
132 
     | 
    
         
             
                  negated_amount = 0 if negated_amount.to_s == '-0.0'
         
     | 
    
        data/lib/moolah/version.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 5.7.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- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2022-09-08 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: bundler
         
     |