moolah 5.0.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 +7 -0
- data/.buildkite/pipeline.yml +30 -0
- data/.gitignore +10 -0
- data/.rbenv-gemsets +1 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/docker-compose.yml.template +17 -0
- data/lib/moolah/charge.rb +54 -0
- data/lib/moolah/charge_class_builder.rb +63 -0
- data/lib/moolah/currency.rb +26 -0
- data/lib/moolah/currency_registry.rb +44 -0
- data/lib/moolah/formatter.rb +47 -0
- data/lib/moolah/money.rb +116 -0
- data/lib/moolah/rounder.rb +18 -0
- data/lib/moolah/version.rb +9 -0
- data/lib/moolah.rb +10 -0
- data/lib/supported_currencies.csv +150 -0
- data/moolah.gemspec +28 -0
- metadata +152 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 115af710c730393a90c68062b1ab169762039a21262e37436253903bd212c6fd
|
|
4
|
+
data.tar.gz: '06296e3e90b8d761400150bb5338dcf442fa5b84efde22bc9b83d56146dfe9a9'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2625001e06725cef62bb406554c015606fbbf82679d039d74bd715432ec9312b6fa0b07b3496dba4853539ee26bdaad17017d243a088656a9925268ceb09a381
|
|
7
|
+
data.tar.gz: 34fa35fde5cfd8819cddf8ce89c88509ad6989b73a89e147f8c1f34cfcf03f0119a2f4c72505cf610a041adc93ca8b059324d1900df957628aa8c3de0b034f04
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
env:
|
|
2
|
+
RAILS_ENV: "test"
|
|
3
|
+
RACK_ENV: "test"
|
|
4
|
+
REPO_URL: "730011650125.dkr.ecr.ap-southeast-2.amazonaws.com"
|
|
5
|
+
REPO_NAME: "base/ruby"
|
|
6
|
+
IMAGE_TAG: "3.0.0-builder-34"
|
|
7
|
+
|
|
8
|
+
steps:
|
|
9
|
+
- label: 'Specs'
|
|
10
|
+
command:
|
|
11
|
+
- bundle install
|
|
12
|
+
- bundle exec rake
|
|
13
|
+
agents:
|
|
14
|
+
- "ci-env=production"
|
|
15
|
+
plugins:
|
|
16
|
+
docker-compose#v3.0.0:
|
|
17
|
+
run: app
|
|
18
|
+
|
|
19
|
+
- wait
|
|
20
|
+
|
|
21
|
+
- label: 'Publish Internally'
|
|
22
|
+
command:
|
|
23
|
+
- bundle install
|
|
24
|
+
- bundle exec rake publish:internal PATCH_VERSION=$BUILDKITE_BUILD_NUMBER
|
|
25
|
+
agents:
|
|
26
|
+
- "ci-env=production"
|
|
27
|
+
plugins:
|
|
28
|
+
docker-compose#v3.0.0:
|
|
29
|
+
run: app
|
|
30
|
+
branches: "master"
|
data/.gitignore
ADDED
data/.rbenv-gemsets
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
moolah
|
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.0.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Marc Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Moolah
|
|
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
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'moolah'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install moolah
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/moolah.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "moolah"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
app:
|
|
2
|
+
image: ${REPO_URL}/${REPO_NAME}:${IMAGE_TAG}
|
|
3
|
+
environment:
|
|
4
|
+
TZ: Australia/Melbourne
|
|
5
|
+
LANG: en_US.UTF-8
|
|
6
|
+
LC_ALL: en_US.UTF-8
|
|
7
|
+
LC_CTYPE: en_US.UTF-8
|
|
8
|
+
HEADLESS: 'true'
|
|
9
|
+
BUILDKITE_COMMIT:
|
|
10
|
+
BUILDKITE_BUILD_NUMBER:
|
|
11
|
+
# Environments from caller
|
|
12
|
+
RACK_ENV:
|
|
13
|
+
RAILS_ENV:
|
|
14
|
+
RSPEC_FORMATTER:
|
|
15
|
+
volumes:
|
|
16
|
+
- .:/application
|
|
17
|
+
working_dir: /application
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module Moolah
|
|
2
|
+
class Charge
|
|
3
|
+
def initialize(valid_attributes: [], **kwargs)
|
|
4
|
+
extend Anemic::Model
|
|
5
|
+
validate_attributes!(kwargs, valid_attributes)
|
|
6
|
+
kwargs.each do |component_name, component_value|
|
|
7
|
+
attribute component_name, Money
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
self.attributes = kwargs
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def +(other_charge)
|
|
14
|
+
return self unless other_charge
|
|
15
|
+
all_component_keys = Set.new(attributes.keys + other_charge.attributes.keys)
|
|
16
|
+
|
|
17
|
+
combined_components = all_component_keys.inject({}) do |com, key|
|
|
18
|
+
com.tap do |c|
|
|
19
|
+
values = [attributes[key], other_charge.attributes[key]].compact # will have at least one value
|
|
20
|
+
first_value, second_value = *values # second value could be nil but adding a nil value to a money will work
|
|
21
|
+
c[key] = first_value + second_value
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Charge.new(**combined_components)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def total
|
|
29
|
+
totals = attributes.values.compact.map(&:total)
|
|
30
|
+
first = totals.shift # use first value so we have the right currency, otherwise we'd need to set one on a zeroed money
|
|
31
|
+
totals.inject(first, &:+) || Money.zero
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.sum(charges)
|
|
35
|
+
charges_to_sum = charges.dup
|
|
36
|
+
charge = charges_to_sum.shift
|
|
37
|
+
charges_to_sum.inject(charge, &:+)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def ==(other_charge)
|
|
41
|
+
attributes == other_charge.attributes
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def validate_attributes!(attributes, valid_attribute_keys)
|
|
47
|
+
return if valid_attribute_keys.empty?
|
|
48
|
+
given_keys = attributes.keys
|
|
49
|
+
unless (given_keys - valid_attribute_keys).empty?
|
|
50
|
+
raise ArgumentError, "Invalid Charge attributes given: #{given_keys} given and only #{valid_attribute_keys} are valid"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module Moolah
|
|
2
|
+
module ChargeClassBuilder
|
|
3
|
+
module InstanceMethods
|
|
4
|
+
|
|
5
|
+
def +(other_charge)
|
|
6
|
+
return self unless other_charge
|
|
7
|
+
|
|
8
|
+
all_component_keys = Set.new(attributes.keys + other_charge.attributes.keys)
|
|
9
|
+
|
|
10
|
+
combined_components = all_component_keys.inject({}) do |com, key|
|
|
11
|
+
com.tap do |c|
|
|
12
|
+
values = [attributes[key], other_charge.attributes[key]].compact # will have at least one value
|
|
13
|
+
c[key] = values.inject(Money.zero, &:+)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
charge_class = ChargeClassBuilder.build(combined_components.keys)
|
|
18
|
+
charge_class.new(combined_components)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def total
|
|
22
|
+
attributes.values.inject(Money.zero, &:+)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def ==(other_charge)
|
|
26
|
+
attributes == other_charge.attributes
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module ClassMethods
|
|
31
|
+
def sum(charges)
|
|
32
|
+
charges.inject(self.zero, &:+)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def zero
|
|
36
|
+
new
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.build(charge_attributes)
|
|
41
|
+
Class.new do
|
|
42
|
+
include Anemic::Model
|
|
43
|
+
|
|
44
|
+
charge_attributes.each do |attr|
|
|
45
|
+
attribute attr.to_sym, Money, default: Money.zero
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
extend ClassMethods
|
|
49
|
+
include InstanceMethods
|
|
50
|
+
|
|
51
|
+
def initialize(construct_attributes = {})
|
|
52
|
+
unexpected_attributes = construct_attributes.to_h.keys.reject { |k| self.class.attribute_defaults.include?(k.to_sym) }
|
|
53
|
+
|
|
54
|
+
if unexpected_attributes.any?
|
|
55
|
+
raise ArgumentError, "unexpected attributes given: #{unexpected_attributes}. Only #{self.attributes.keys} allowed"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
super
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'anemic/model'
|
|
2
|
+
|
|
3
|
+
module Moolah
|
|
4
|
+
class Currency
|
|
5
|
+
include Anemic::Model
|
|
6
|
+
|
|
7
|
+
attribute :name, String
|
|
8
|
+
attribute :code, String
|
|
9
|
+
attribute :iso_number, String
|
|
10
|
+
attribute :symbol, String
|
|
11
|
+
attribute :thousand_delimiter, String
|
|
12
|
+
attribute :decimal_delimiter, String
|
|
13
|
+
|
|
14
|
+
def self.supported_currency_codes
|
|
15
|
+
currencies.keys
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.currency(code = 'AUD')
|
|
19
|
+
currencies[code]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.currencies
|
|
23
|
+
CurrencyRegistry.currencies
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'csv'
|
|
2
|
+
require 'singleton'
|
|
3
|
+
|
|
4
|
+
module Moolah
|
|
5
|
+
class CurrencyRegistry
|
|
6
|
+
include Singleton
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def load_csv(file)
|
|
10
|
+
currencies = {}
|
|
11
|
+
|
|
12
|
+
CSV.foreach(file, headers: true, header_converters: :symbol, encoding: 'ISO-8859-1') do |row|
|
|
13
|
+
code = row[:code]
|
|
14
|
+
currencies[code] = Currency.new(
|
|
15
|
+
name: row[:currency],
|
|
16
|
+
code: code,
|
|
17
|
+
iso_number: row[:isonumber],
|
|
18
|
+
thousand_delimiter: row[:thousanddelimiter],
|
|
19
|
+
decimal_delimiter: row[:decimaldelimiter],
|
|
20
|
+
symbol: row[:symbol]
|
|
21
|
+
) if code && !code.empty?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
instance.load(currencies)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def currencies
|
|
28
|
+
instance.loaded_currencies
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def load(currencies)
|
|
33
|
+
@currencies = currencies
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def loaded_currencies
|
|
37
|
+
currencies
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
attr_reader :currencies
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Moolah
|
|
2
|
+
class Formatter
|
|
3
|
+
|
|
4
|
+
def initialize(money)
|
|
5
|
+
@money = money
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def format(symbol: true, delimit: true)
|
|
9
|
+
str = ''
|
|
10
|
+
str << currency_symbol if symbol
|
|
11
|
+
str << formatted_amount
|
|
12
|
+
str.gsub!(thousand_regex, "\\1#{currency_thousand_delimiter}") if delimit
|
|
13
|
+
str
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def format_fx_rate
|
|
17
|
+
('%.2f' % money.fx_rate)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
attr_reader :money
|
|
22
|
+
|
|
23
|
+
def currency_symbol
|
|
24
|
+
currency.symbol
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def currency_thousand_delimiter
|
|
28
|
+
currency.thousand_delimiter
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def currency
|
|
32
|
+
Currency.currency(money.currency) || Currency.currency('AUD')
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def thousand_regex
|
|
36
|
+
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|\#{currency.decimal_delimiter}))/
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def formatted_amount
|
|
40
|
+
('%.2f' % rounded_amount).sub('.', currency.decimal_delimiter)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def rounded_amount
|
|
44
|
+
Rounder.new(money).bankers_rounded.amount
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/moolah/money.rb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'anemic/model'
|
|
2
|
+
require 'bigdecimal'
|
|
3
|
+
|
|
4
|
+
module Moolah
|
|
5
|
+
class Money
|
|
6
|
+
include Anemic::Model
|
|
7
|
+
include Comparable
|
|
8
|
+
|
|
9
|
+
attribute :amount, BigDecimal, default: BigDecimal(0)
|
|
10
|
+
attribute :currency, String, default: 'AUD'
|
|
11
|
+
attribute :fx_rate, BigDecimal
|
|
12
|
+
|
|
13
|
+
def initialize(args = {})
|
|
14
|
+
args[:fx_rate] = 1.0 if args[:currency] == 'AUD'
|
|
15
|
+
|
|
16
|
+
raise ArgumentError.new('missing keyword: :fx_rate') unless args[:fx_rate]
|
|
17
|
+
|
|
18
|
+
super(args)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.in_aud(amount)
|
|
22
|
+
Money.new(currency: 'AUD', amount: amount)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.zero(currency='AUD', fx_rate=1)
|
|
26
|
+
Money.new(currency: currency, amount: 0, fx_rate: fx_rate)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def formatted(symbol: true, delimit: true)
|
|
30
|
+
formatter.format(symbol: symbol, delimit: delimit)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def formatted_amount
|
|
34
|
+
formatter.format(symbol: false, delimit: false)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def amount_in_cents
|
|
38
|
+
(bankers_rounded.amount * 100).to_i
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def as_json(*_args)
|
|
42
|
+
{
|
|
43
|
+
'amount' => formatted_amount,
|
|
44
|
+
'currency' => currency,
|
|
45
|
+
'fx_rate' => formatter.format_fx_rate
|
|
46
|
+
}.compact
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def total
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def inspect
|
|
54
|
+
"#{self.class.name} (#{as_json})"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def +(money)
|
|
58
|
+
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
|
+
|
|
62
|
+
Money.new(amount: amount + money.amount, currency: currency, fx_rate: fx_rate)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def -(money)
|
|
66
|
+
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
|
+
|
|
70
|
+
Money.new(amount: amount - money.amount, currency: currency, fx_rate: fx_rate)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def *(_money)
|
|
74
|
+
raise ArgumentError, 'Cannot multiply Money objects due to rounding issues'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def /(_money)
|
|
78
|
+
raise ArgumentError, 'Cannot divide Money objects due to rounding issues'
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def -@
|
|
82
|
+
Money.new(amount: -amount, currency: currency, fx_rate: fx_rate)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
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
|
|
88
|
+
|
|
89
|
+
amount <=> money.amount
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def bankers_rounded
|
|
93
|
+
Rounder.new(self).bankers_rounded
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def zero?
|
|
97
|
+
amount.zero?
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def negate
|
|
101
|
+
negated_amount = amount * -1
|
|
102
|
+
negated_amount = 0 if negated_amount.to_s == '-0.0'
|
|
103
|
+
Money.new(amount: negated_amount, currency: currency, fx_rate: fx_rate)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def abs
|
|
107
|
+
Money.new(amount: amount.abs, currency: currency, fx_rate: fx_rate)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
def formatter
|
|
113
|
+
Formatter.new(self)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Moolah
|
|
2
|
+
class Rounder
|
|
3
|
+
def initialize(money)
|
|
4
|
+
@money = money
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def bankers_rounded
|
|
8
|
+
Money.new(amount: bankers_rounded_amount, currency: money.currency, fx_rate: money.fx_rate)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
attr_reader :money
|
|
13
|
+
|
|
14
|
+
def bankers_rounded_amount
|
|
15
|
+
money.amount.round(2, BigDecimal::ROUND_HALF_EVEN)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/moolah.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'moolah/version'
|
|
2
|
+
|
|
3
|
+
Dir.glob(File.join(File.dirname(__FILE__), '**', '*.rb')).each do |file|
|
|
4
|
+
require file
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module Moolah
|
|
8
|
+
# Your code goes here...
|
|
9
|
+
Moolah::CurrencyRegistry.load_csv(File.expand_path('supported_currencies.csv', File.dirname(__FILE__)))
|
|
10
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Currency,Code,ISONumber,Symbol,ThousandDelimiter,DecimalDelimiter
|
|
2
|
+
United Arab Emirates Dirham,AED,784,,",",.
|
|
3
|
+
Afghan Afghani,AFN,971,,",",.
|
|
4
|
+
Albanian Lek,ALL,008,,",",.
|
|
5
|
+
Netherlands Antillean Guilder,ANG,532,,",",.
|
|
6
|
+
Angola Kwanza,AOA,973,,",",.
|
|
7
|
+
Argentine Peso,ARS,032,,",",.
|
|
8
|
+
Australian Dollar,AUD,036,$,",",.
|
|
9
|
+
Aruban Florin,AWG,533,,",",.
|
|
10
|
+
Azerbaijani Manat,AZN,944,,",",.
|
|
11
|
+
Bosnia and Herzegovina Convertible Mark,BAM,977,,",",.
|
|
12
|
+
Barbadian Dollar,BBD,052,,",",.
|
|
13
|
+
Bangladeshi Taka,BDT,050,,",",.
|
|
14
|
+
Bulgarian Lev,BGN,975,,",",.
|
|
15
|
+
Bahraini Dinar,BHD,048,,",",.
|
|
16
|
+
Burundian Franc,BIF,108,,",",.
|
|
17
|
+
Bermudian Dollar,BMD,060,,",",.
|
|
18
|
+
Brunei Dollar,BND,096,,",",.
|
|
19
|
+
Bolivian Boliviano,BOB,068,,",",.
|
|
20
|
+
Brazilian Real,BRL,986,,",",.
|
|
21
|
+
Bahamian Dollar,BSD,044,,",",.
|
|
22
|
+
Bhutanese Ngultrum,BTN,064,,",",.
|
|
23
|
+
Botswana Pula,BWP,072,,",",.
|
|
24
|
+
Belarusian Ruble,BYN,933,,",",.
|
|
25
|
+
Belize Dollar,BZD,084,,",",.
|
|
26
|
+
Canadian Dollar,CAD,124,,",",.
|
|
27
|
+
Congolese Franc,CDF,976,,",",.
|
|
28
|
+
Swiss Franc,CHF,756,,",",.
|
|
29
|
+
Chilean Peso,CLP,152,,",",.
|
|
30
|
+
Chinese Renminbi Yuan,CNY,156,¥,",",.
|
|
31
|
+
Colombian peso,COP,170,,",",.
|
|
32
|
+
Costa Rican Colón,CRC,188,,",",.
|
|
33
|
+
Cuban Peso,CUP,192,,",",.
|
|
34
|
+
Cape Verdean Escudo,CVE,132,,",",.
|
|
35
|
+
Czech Koruna,CZK,203,,",",.
|
|
36
|
+
Djiboutian Franc,DJF,262,,",",.
|
|
37
|
+
Danish Krone,DKK,208,,",",.
|
|
38
|
+
Dominican Peso,DOP,214,,",",.
|
|
39
|
+
Algerian Dinar,DZD,012,,",",.
|
|
40
|
+
Egyptian Pound,EGP,818,,",",.
|
|
41
|
+
Ethiopian Birr,ETB,230,,",",.
|
|
42
|
+
Euro,EUR,978,€,",",.
|
|
43
|
+
Fijian Dollar,FJD,242,FJ$,",",.
|
|
44
|
+
Falkland Pound,FKP,238,,",",.
|
|
45
|
+
British Pound,GBP,826,£,",",.
|
|
46
|
+
Georgian Lari,GEL,981,,",",.
|
|
47
|
+
Ghanaian Cedi,GHS,936,,",",.
|
|
48
|
+
Gibraltar Pound,GIP,292,,",",.
|
|
49
|
+
Gambian Dalasi,GMD,270,,",",.
|
|
50
|
+
Guinean Franc,GNF,324,,",",.
|
|
51
|
+
Guatemalan Quetzal,GTQ,320,,",",.
|
|
52
|
+
Guyananese Dollar,GYD,328,,",",.
|
|
53
|
+
Hong Kong Dollar,HKD,344,,",",.
|
|
54
|
+
Honduran Lempira,HNL,340,,",",.
|
|
55
|
+
Croatian Kuna,HRK,191,,",",.
|
|
56
|
+
Haitian Gourde,HTG,332,,",",.
|
|
57
|
+
Hungarian Forint,HUF,348,,",",.
|
|
58
|
+
Indonesian Rupiah,IDR,360,Rp,",",.
|
|
59
|
+
Israeli New Shekel,ILS,376,,",",.
|
|
60
|
+
Indian Rupee,INR,356,₹,",",.
|
|
61
|
+
Iraqi Dinar,IQD,368,,",",.
|
|
62
|
+
Icelandic Króna,ISK,352,,",",.
|
|
63
|
+
Jamaican Dollar,JMD,388,,",",.
|
|
64
|
+
Jordanian Dinar,JOD,400,,",",.
|
|
65
|
+
Japanese Yen,JPY,392,¥,",",.
|
|
66
|
+
Kenyan Shilling,KES,404,,",",.
|
|
67
|
+
Kyrgyzstani Som,KGS,417,,",",.
|
|
68
|
+
Cambodian Riel,KHR,116,,",",.
|
|
69
|
+
Comorian Franc,KMF,174,,",",.
|
|
70
|
+
South Korean Won,KRW,410,,",",.
|
|
71
|
+
Kuwaiti Dinar,KWD,414,,",",.
|
|
72
|
+
Cayman Islands Dollar,KYD,136,,",",.
|
|
73
|
+
Kazakhstani Tenge,KZT,398,,",",.
|
|
74
|
+
Lao Kip,LAK,418,,",",.
|
|
75
|
+
Lebanese Pound,LBP,422,,",",.
|
|
76
|
+
Sri Lanka Rupee,LKR,144,Rs,",",.
|
|
77
|
+
Liberian Dollar,LRD,430,,",",.
|
|
78
|
+
Lesotho Loti,LSL,426,,",",.
|
|
79
|
+
Libyan Dinar,LYD,434,,",",.
|
|
80
|
+
Moroccan Dirham,MAD,504,,",",.
|
|
81
|
+
Moldovan Leu,MDL,498,,",",.
|
|
82
|
+
Malagasy Ariary,MGA,969,,",",.
|
|
83
|
+
Macedonian Denar,MKD,807,,",",.
|
|
84
|
+
Myanmar Kyat,MMK,104,,",",.
|
|
85
|
+
Mongolian Tögrög,MNT,496,,",",.
|
|
86
|
+
Macanese Pataca,MOP,446,,",",.
|
|
87
|
+
Mauritius Rupee,MUR,480,,",",.
|
|
88
|
+
Maldivian Rufiyaa,MVR,462,,",",.
|
|
89
|
+
Malawian Kwacha,MWK,454,,",",.
|
|
90
|
+
Mexican Peso,MXN,484,,",",.
|
|
91
|
+
Malaysian Ringgit,MYR,458,RM,",",.
|
|
92
|
+
Mozambican Metical,MZN,943,,",",.
|
|
93
|
+
Namibian Dollar,NAD,516,,",",.
|
|
94
|
+
Nigerian Naira,NGN,566,,",",.
|
|
95
|
+
Nicaraguan Córdoba,NIO,558,,",",.
|
|
96
|
+
Norwegian Krone,NOK,578,,",",.
|
|
97
|
+
Nepalese Rupee,NPR,524,,",",.
|
|
98
|
+
New Zealand Dollar,NZD,554,$,",",.
|
|
99
|
+
Omani Rial,OMR,512,,",",.
|
|
100
|
+
Panamanian Balboa,PAB,590,,",",.
|
|
101
|
+
Peruvian Sol,PEN,604,,",",.
|
|
102
|
+
Papua New Guinean Kina,PGK,598,,",",.
|
|
103
|
+
Philippine Peso,PHP,608,,",",.
|
|
104
|
+
Pakistan Rupee,PKR,586,,",",.
|
|
105
|
+
Polish Złoty,PLN,985,,",",.
|
|
106
|
+
Paraguayan Guaraní,PYG,600,,",",.
|
|
107
|
+
Qatari Rial,QAR,634,,",",.
|
|
108
|
+
Romanian Leu,RON,946,,",",.
|
|
109
|
+
Serbian Dinar,RSD,941,,",",.
|
|
110
|
+
Russian Ruble,RUB,643,,",",.
|
|
111
|
+
Rwanda Franc,RWF,646,,",",.
|
|
112
|
+
Saudi Riyal,SAR,682,,",",.
|
|
113
|
+
Solomon Islands Dollar,SBD,090,,",",.
|
|
114
|
+
Seychellois Rupee,SCR,690,,",",.
|
|
115
|
+
Swedish Krona,SEK,752,,",",.
|
|
116
|
+
Singapore Dollar,SGD,702,S$,",",.
|
|
117
|
+
Saint Helenian Pound,SHP,654,,",",.
|
|
118
|
+
Sierra Leonean Leone,SLL,694,,",",.
|
|
119
|
+
Somali Shilling,SOS,706,,",",.
|
|
120
|
+
Surinamese Dollar,SRD,968,,",",.
|
|
121
|
+
South Sudanese Pound,SSP,728,,",",.
|
|
122
|
+
São Tomé and Príncipe Dobra,STN,930,,",",.
|
|
123
|
+
Salvadoran Colon,SVC,222,,",",.
|
|
124
|
+
Swazi Lilangeni,SZL,748,,",",.
|
|
125
|
+
Thai Baht,THB,764,฿,",",.
|
|
126
|
+
Tajikistani Somoni,TJS,972,,",",.
|
|
127
|
+
Turkmenistan Manat,TMT,934,,",",.
|
|
128
|
+
Tunisian Dinar,TND,788,,",",.
|
|
129
|
+
Tongan Paʻanga,TOP,776,,",",.
|
|
130
|
+
Turkish Lira,TRY,949,,",",.
|
|
131
|
+
Trinidad and Tobago Dollar,TTD,780,,",",.
|
|
132
|
+
New Taiwan Dollar,TWD,901,,",",.
|
|
133
|
+
Tanzanian Shilling,TZS,834,,",",.
|
|
134
|
+
Ukrainian Hryvnia,UAH,980,,",",.
|
|
135
|
+
Ugandan Shilling,UGX,800,,",",.
|
|
136
|
+
United States Dollar,USD,840,$,",",.
|
|
137
|
+
Uruguayan Peso,UYU,858,,",",.
|
|
138
|
+
Uzbekistani Som,UZS,860,,",",.
|
|
139
|
+
Venezuelan Bolívar,VES,928,,",",.
|
|
140
|
+
Vietnamese Dong,VND,704,,",",.
|
|
141
|
+
Vanuatu Vatu,VUV,548,,",",.
|
|
142
|
+
Samoan Tala,WST,882,,",",.
|
|
143
|
+
Central African Cfa Franc,XAF,950,,",",.
|
|
144
|
+
East Caribbean Dollar,XCD,951,,",",.
|
|
145
|
+
West African Cfa Franc,XOF,952,,",",.
|
|
146
|
+
CFP Franc,XPF,953,,",",.
|
|
147
|
+
Yemeni Rial,YER,886,,",",.
|
|
148
|
+
South African Rand,ZAR,710,,",",.
|
|
149
|
+
Zambian Kwacha,ZMW,967,,",",.
|
|
150
|
+
Zimbabwean Dollar,ZWL,932,,",",.
|
data/moolah.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'moolah/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'moolah'
|
|
8
|
+
spec.version = Moolah::VERSION
|
|
9
|
+
spec.authors = ['Marc Lee', 'Chris Rode']
|
|
10
|
+
spec.email = ['marc.lee@hooroo.com', 'chris@hooroo.com']
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Money related gem}
|
|
13
|
+
spec.description = %q{Money money money! MONEY.}
|
|
14
|
+
spec.homepage = 'https://github.com/hooroo/moolah'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = 'exe'
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ['lib']
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency 'bundler'
|
|
23
|
+
spec.add_development_dependency 'rake'
|
|
24
|
+
spec.add_development_dependency 'rspec'
|
|
25
|
+
spec.add_development_dependency 'pry'
|
|
26
|
+
spec.add_runtime_dependency 'anemic'
|
|
27
|
+
spec.add_runtime_dependency 'activesupport'
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: moolah
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 5.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marc Lee
|
|
8
|
+
- Chris Rode
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: exe
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2022-07-14 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - ">="
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '0'
|
|
21
|
+
type: :development
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '0'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rake
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
type: :development
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: rspec
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
type: :development
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: pry
|
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
type: :development
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
- !ruby/object:Gem::Dependency
|
|
71
|
+
name: anemic
|
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
type: :runtime
|
|
78
|
+
prerelease: false
|
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
- !ruby/object:Gem::Dependency
|
|
85
|
+
name: activesupport
|
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
type: :runtime
|
|
92
|
+
prerelease: false
|
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
description: Money money money! MONEY.
|
|
99
|
+
email:
|
|
100
|
+
- marc.lee@hooroo.com
|
|
101
|
+
- chris@hooroo.com
|
|
102
|
+
executables: []
|
|
103
|
+
extensions: []
|
|
104
|
+
extra_rdoc_files: []
|
|
105
|
+
files:
|
|
106
|
+
- ".buildkite/pipeline.yml"
|
|
107
|
+
- ".gitignore"
|
|
108
|
+
- ".rbenv-gemsets"
|
|
109
|
+
- ".rspec"
|
|
110
|
+
- ".ruby-version"
|
|
111
|
+
- Gemfile
|
|
112
|
+
- LICENSE.txt
|
|
113
|
+
- README.md
|
|
114
|
+
- Rakefile
|
|
115
|
+
- bin/console
|
|
116
|
+
- bin/setup
|
|
117
|
+
- docker-compose.yml.template
|
|
118
|
+
- lib/moolah.rb
|
|
119
|
+
- lib/moolah/charge.rb
|
|
120
|
+
- lib/moolah/charge_class_builder.rb
|
|
121
|
+
- lib/moolah/currency.rb
|
|
122
|
+
- lib/moolah/currency_registry.rb
|
|
123
|
+
- lib/moolah/formatter.rb
|
|
124
|
+
- lib/moolah/money.rb
|
|
125
|
+
- lib/moolah/rounder.rb
|
|
126
|
+
- lib/moolah/version.rb
|
|
127
|
+
- lib/supported_currencies.csv
|
|
128
|
+
- moolah.gemspec
|
|
129
|
+
homepage: https://github.com/hooroo/moolah
|
|
130
|
+
licenses:
|
|
131
|
+
- MIT
|
|
132
|
+
metadata: {}
|
|
133
|
+
post_install_message:
|
|
134
|
+
rdoc_options: []
|
|
135
|
+
require_paths:
|
|
136
|
+
- lib
|
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
|
+
requirements:
|
|
139
|
+
- - ">="
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '0'
|
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
|
+
requirements:
|
|
144
|
+
- - ">="
|
|
145
|
+
- !ruby/object:Gem::Version
|
|
146
|
+
version: '0'
|
|
147
|
+
requirements: []
|
|
148
|
+
rubygems_version: 3.2.3
|
|
149
|
+
signing_key:
|
|
150
|
+
specification_version: 4
|
|
151
|
+
summary: Money related gem
|
|
152
|
+
test_files: []
|