mint-money 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +4 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/README.md +88 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/circle.yml +3 -0
- data/lib/mint/money.rb +4 -0
- data/lib/mint/money/currency.rb +68 -0
- data/lib/mint/money/exceptions.rb +24 -0
- data/lib/mint/money/money.rb +126 -0
- data/lib/mint/money/utils.rb +62 -0
- data/lib/mint/money/version.rb +7 -0
- data/mint-money.gemspec +30 -0
- metadata +218 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 546ffe1b3832a048fcfc298bbc6c0ced7b78675b
|
4
|
+
data.tar.gz: c4c8af84ee084d3706aebb8bc0e8a1582109e44d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 240fe427f6e3ab74a4940733bbd49690e3bb1dc71e02fcd1e16b1dfb2e79d5568533d35a8ade563464ff0d8fea6279660929982af20642f5c3b248cfe80a42b2
|
7
|
+
data.tar.gz: ad8d81cea51deb5750030a4dca9189cd6a3bea5204e6dd7fd939111f77b9446a7c4ec115f3660f4286badebbf5937cd3c556ab0f27b15a487b5602351a0f948b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Mint::Money
|
2
|
+
|
3
|
+
Mint::Money perform currency conversion and arithmetics with different currencies.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mint-money'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mint-money
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
# Configure the currency rates with respect to a base currency (here EUR):
|
25
|
+
|
26
|
+
Money.conversion_rates('EUR', {
|
27
|
+
'USD' => 1.11,
|
28
|
+
'Bitcoin' => 0.0047
|
29
|
+
})
|
30
|
+
```
|
31
|
+
|
32
|
+
```
|
33
|
+
# Instantiate money objects:
|
34
|
+
|
35
|
+
fifty_eur = Money.new(50, 'EUR')
|
36
|
+
|
37
|
+
# Get amount and currency:
|
38
|
+
|
39
|
+
fifty_eur.amount # => 50
|
40
|
+
fifty_eur.currency # => "EUR"
|
41
|
+
fifty_eur.inspect # => "50.00 EUR"
|
42
|
+
```
|
43
|
+
|
44
|
+
```
|
45
|
+
# Convert to a different currency (should return a Money
|
46
|
+
# instance, not a String):
|
47
|
+
|
48
|
+
fifty_eur.convert_to('USD') # => 55.50 USD
|
49
|
+
```
|
50
|
+
|
51
|
+
```
|
52
|
+
# Perform operations in different currencies:
|
53
|
+
|
54
|
+
twenty_dollars = Money.new(20, 'USD')
|
55
|
+
|
56
|
+
# Arithmetics:
|
57
|
+
|
58
|
+
fifty_eur + twenty_dollars # => 68.02 EUR
|
59
|
+
fifty_eur - twenty_dollars # => 31.98 EUR
|
60
|
+
fifty_eur / 2 # => 25 EUR
|
61
|
+
twenty_dollars * 3 # => 60 USD
|
62
|
+
```
|
63
|
+
|
64
|
+
```
|
65
|
+
# Comparisons (also in different currencies):
|
66
|
+
|
67
|
+
twenty_dollars == Money.new(20, 'USD') # => true
|
68
|
+
twenty_dollars == Money.new(30, 'USD') # => false
|
69
|
+
|
70
|
+
fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
71
|
+
fifty_eur_in_usd == fifty_eur # => true
|
72
|
+
|
73
|
+
twenty_dollars > Money.new(5, 'USD') # => true
|
74
|
+
twenty_dollars < fifty_eur # => true
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
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.
|
80
|
+
|
81
|
+
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).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mpakus/mint-money.
|
86
|
+
|
87
|
+
[![CircleCI](https://circleci.com/gh/mpakus/mint-money.svg?style=svg)](https://circleci.com/gh/mpakus/mint-money)
|
88
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'mint/money'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/circle.yml
ADDED
data/lib/mint/money.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './utils'
|
4
|
+
require_relative './exceptions'
|
5
|
+
|
6
|
+
module Mint
|
7
|
+
# Global Currencies state
|
8
|
+
class Currency
|
9
|
+
class << self
|
10
|
+
attr_accessor :base, :rates, :precisions
|
11
|
+
|
12
|
+
# Setup currency base and conversion rates
|
13
|
+
def conversion_rates(base, rates)
|
14
|
+
@base = Mint::Utils.to_key(base)
|
15
|
+
@rates = Hash[rates.map { |k, v| [Mint::Utils.to_key(k), Mint::Utils.to_amount(v, Mint::Utils.to_key(k))] }]
|
16
|
+
@rates[@base] = Mint::Utils.to_amount(1.0, @base)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Set round precisions for exceptional currencies like BTC
|
20
|
+
# @params precisions [Hash] of currency exception and number of precision e.g. {BTC: 8}
|
21
|
+
def round_precisions(precisions)
|
22
|
+
@precisions = Hash[precisions.map { |k, v| [Mint::Utils.to_key(k), v.to_i] }]
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [String]
|
26
|
+
def base
|
27
|
+
@base.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
# Checks if currency exists in our conversion rates hash
|
31
|
+
# @return [Boolean]
|
32
|
+
def valid?(currency)
|
33
|
+
@rates.key?(currency)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert Mint::Money object's amount to another currency
|
37
|
+
# @TODO: use `use_base` flag try to deep conversion through the currency base
|
38
|
+
# @params money [Mint::Money]
|
39
|
+
# @params currency [String, Symbol]
|
40
|
+
# @params _use_base [Boolean]
|
41
|
+
# @return [Mint::Money]
|
42
|
+
def convert_to(money, currency, _use_base = false)
|
43
|
+
currency_need = Mint::Utils.to_key(currency)
|
44
|
+
raise WrongCurrencyError, currency unless Mint::Currency.valid?(currency_need)
|
45
|
+
return money if money.currency_sym == currency_need
|
46
|
+
|
47
|
+
rate = calculate_rate(money, currency_need)
|
48
|
+
Mint::Money.new(money.amount * rate, currency_need)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# Find exchange rate
|
54
|
+
# @param money [Mint::Money]
|
55
|
+
# @param currency_needed [Symbol]
|
56
|
+
# @return [BigDecimal]
|
57
|
+
def calculate_rate(money, currency_need)
|
58
|
+
# when they are not the same currency
|
59
|
+
if @base != money.currency_sym
|
60
|
+
# but base currency equal to what we need take reverse currency e.g. 1/currency
|
61
|
+
return 1 / @rates[money.currency_sym] if @base == currency_need
|
62
|
+
raise(WrongConversionError, "#{money.inspect} from #{money.currency_sym} to #{currency_need}")
|
63
|
+
end
|
64
|
+
@rates[currency_need]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mint
|
4
|
+
# When currency type is unrecognized
|
5
|
+
class WrongCurrencyError < ArgumentError
|
6
|
+
def initialize(value)
|
7
|
+
super "Can't find #{value} currency, please setup it with Mint::Money.conversion.rates"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# When money type is unrecognized
|
12
|
+
class WrongMoneyError < TypeError
|
13
|
+
def initialize(value)
|
14
|
+
super "Wrong amount's type #{value.inspect}, should be Float, BigDecimal or Mint::Money"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# When a conversion need to setup currency rate
|
19
|
+
class WrongConversionError < ArgumentError
|
20
|
+
def initialize(msg)
|
21
|
+
super "Can't convert #{msg} without conversion rates"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'bigdecimal/util'
|
5
|
+
require 'forwardable'
|
6
|
+
require_relative './currency'
|
7
|
+
require_relative './exceptions'
|
8
|
+
require_relative './utils'
|
9
|
+
|
10
|
+
module Mint
|
11
|
+
# Minty Money Management
|
12
|
+
#
|
13
|
+
# @author Renat Ibragimov <renat@aomega.co>
|
14
|
+
#
|
15
|
+
# @see https://github.com/mpakus/mint-money
|
16
|
+
class Money
|
17
|
+
include Comparable
|
18
|
+
|
19
|
+
attr_reader :value, :currency, :currency_sym
|
20
|
+
alias amount value
|
21
|
+
|
22
|
+
# @param value [Integer]
|
23
|
+
# @param currency [String,Symbol]
|
24
|
+
# @return [Mint::Money]
|
25
|
+
def initialize(value = 0.00, currency = nil)
|
26
|
+
@currency_sym = normalize_currency(currency)
|
27
|
+
@currency = currency.to_s.upcase
|
28
|
+
@value = Mint::Utils.to_amount(value, @currency_sym)
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
extend Forwardable
|
33
|
+
# Delegate .conversion_rates class method to Mint::Currency class
|
34
|
+
def_delegator :'Mint::Currency', :conversion_rates
|
35
|
+
def_delegator :'Mint::Currency', :round_precisions
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns formatted value and currency name
|
39
|
+
# @return [String]
|
40
|
+
def inspect
|
41
|
+
"#{self} #{currency}"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Auto/Stringify instance
|
45
|
+
# @return [String]
|
46
|
+
def to_s
|
47
|
+
Mint::Utils.to_format(value, currency_sym)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Create new Mint::Money with amount converted to another currency
|
51
|
+
# @param currency [Symbol]
|
52
|
+
# @param use_base [Boolean]
|
53
|
+
# @return [Mint::Money]
|
54
|
+
def convert_to(currency, use_base = false)
|
55
|
+
Mint::Currency.convert_to(self, currency, use_base)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Plus operation
|
59
|
+
# @return [Mint::Money]
|
60
|
+
def +(other)
|
61
|
+
other = self.class.new(other, @currency_sym) unless other.is_a? self.class
|
62
|
+
self.class.new(amount + cast_type(other).amount, @currency_sym)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Minus operation
|
66
|
+
# @return [Mint::Money]
|
67
|
+
def -(other)
|
68
|
+
other = self.class.new(other, @currency_sym) unless other.is_a? self.class
|
69
|
+
self.class.new(amount - cast_type(other).amount, @currency_sym)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Equal operation
|
73
|
+
# Two Mint::Money objects are equal or one of them is a String and looks like .inspect results
|
74
|
+
# @return [Boolean]
|
75
|
+
def ==(other)
|
76
|
+
eql?(other)
|
77
|
+
end
|
78
|
+
|
79
|
+
# rubocop:disable all
|
80
|
+
# @return [Boolean]
|
81
|
+
def eql?(other)
|
82
|
+
return inspect == other if other.class == String
|
83
|
+
# what if they are same class just different currencies
|
84
|
+
other = cast_type(other) if other.is_a?(self.class) && currency_sym != other.currency_sym
|
85
|
+
self.class == other.class && amount == other.amount && currency_sym == other.currency_sym
|
86
|
+
end
|
87
|
+
# rubocop:enable all
|
88
|
+
|
89
|
+
# Divide operation
|
90
|
+
# @return [Mint::Money]
|
91
|
+
def /(other)
|
92
|
+
other = self.class.new(other, @currency_sym) unless other.is_a? self.class
|
93
|
+
self.class.new(amount / other.amount, @currency_sym)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Multiplication operation
|
97
|
+
# @return [Mint::Money]
|
98
|
+
def *(other)
|
99
|
+
other = self.class.new(other, @currency_sym) unless other.is_a? self.class
|
100
|
+
self.class.new(amount * other.amount, @currency_sym)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Lower and bigger (sort, spaceship) operation
|
104
|
+
# @return [boolean]
|
105
|
+
def <=>(other)
|
106
|
+
other = self.class.new(other, @currency_sym) unless other.is_a? self.class
|
107
|
+
amount <=> other.amount
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
# Convert other Money to the same currency
|
113
|
+
# @return [Mint::Money]
|
114
|
+
def cast_type(other)
|
115
|
+
return Mint::Currency.convert_to(other, @currency_sym) if @currency_sym != other.currency_sym
|
116
|
+
other
|
117
|
+
end
|
118
|
+
|
119
|
+
# @return [Symbol]
|
120
|
+
def normalize_currency(currency = nil)
|
121
|
+
currency_sym = Mint::Utils.to_key(currency)
|
122
|
+
raise WrongCurrencyError, currency unless Mint::Currency.valid?(currency_sym)
|
123
|
+
currency_sym
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'bigdecimal/util'
|
5
|
+
require_relative './exceptions'
|
6
|
+
|
7
|
+
module Mint
|
8
|
+
# Special utilities
|
9
|
+
class Utils
|
10
|
+
class << self
|
11
|
+
# Normalize string to our keys format
|
12
|
+
# @example
|
13
|
+
# 'usd' -> :USD
|
14
|
+
# @param [String,Symbol]
|
15
|
+
# @return [Symbol]
|
16
|
+
def to_key(key)
|
17
|
+
key.to_s.upcase.to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
# Normalize and guess amount's type
|
21
|
+
# @example
|
22
|
+
# '1.178' -> <BigDecimal: 1.18>
|
23
|
+
# @param value [BigDecimal]
|
24
|
+
# @param currency [Symbol]
|
25
|
+
# @return [BigDecimal]
|
26
|
+
def to_amount(value, currency)
|
27
|
+
to_bigdecimal(value).round(precision(currency))
|
28
|
+
end
|
29
|
+
|
30
|
+
# Format number as string with correct precision
|
31
|
+
# @param value [BigDecimal]
|
32
|
+
# @param currency [Symbol]
|
33
|
+
# @return [String]
|
34
|
+
def to_format(value, currency)
|
35
|
+
format("%.#{precision(currency)}f", value.to_f)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# Return precision for round or default is 2
|
41
|
+
# @param currency [String,Symbol]
|
42
|
+
# @return [Integer]
|
43
|
+
def precision(currency)
|
44
|
+
(Mint::Currency.precisions || {}).fetch(to_key(currency), 2)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Trying to guess type
|
48
|
+
# @param value [Mixed]
|
49
|
+
# @return [BigDecimal]
|
50
|
+
def to_bigdecimal(value)
|
51
|
+
case value
|
52
|
+
when Mint::Money
|
53
|
+
value.value
|
54
|
+
when Rational
|
55
|
+
value.to_d(16)
|
56
|
+
else
|
57
|
+
value.respond_to?(:to_d) ? value.to_d : raise(WrongMoneyError, value)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/mint-money.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'mint/money/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'mint-money'
|
10
|
+
spec.version = Mint::Money::VERSION
|
11
|
+
spec.authors = ['Renat Ibragimov']
|
12
|
+
spec.email = ['mrak69@gmail.com']
|
13
|
+
|
14
|
+
spec.summary = 'Manage money with Mint::Money'
|
15
|
+
spec.description = File.read(File.expand_path('../README.md', __FILE__))
|
16
|
+
spec.homepage = 'https://github.com/mpakus/mint-money'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.49'
|
29
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mint-money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renat Ibragimov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.49'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.49'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.10'
|
83
|
+
description: |+
|
84
|
+
# Mint::Money
|
85
|
+
|
86
|
+
Mint::Money perform currency conversion and arithmetics with different currencies.
|
87
|
+
|
88
|
+
## Installation
|
89
|
+
|
90
|
+
Add this line to your application's Gemfile:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
gem 'mint-money'
|
94
|
+
```
|
95
|
+
|
96
|
+
And then execute:
|
97
|
+
|
98
|
+
$ bundle
|
99
|
+
|
100
|
+
Or install it yourself as:
|
101
|
+
|
102
|
+
$ gem install mint-money
|
103
|
+
|
104
|
+
## Usage
|
105
|
+
|
106
|
+
```
|
107
|
+
# Configure the currency rates with respect to a base currency (here EUR):
|
108
|
+
|
109
|
+
Money.conversion_rates('EUR', {
|
110
|
+
'USD' => 1.11,
|
111
|
+
'Bitcoin' => 0.0047
|
112
|
+
})
|
113
|
+
```
|
114
|
+
|
115
|
+
```
|
116
|
+
# Instantiate money objects:
|
117
|
+
|
118
|
+
fifty_eur = Money.new(50, 'EUR')
|
119
|
+
|
120
|
+
# Get amount and currency:
|
121
|
+
|
122
|
+
fifty_eur.amount # => 50
|
123
|
+
fifty_eur.currency # => "EUR"
|
124
|
+
fifty_eur.inspect # => "50.00 EUR"
|
125
|
+
```
|
126
|
+
|
127
|
+
```
|
128
|
+
# Convert to a different currency (should return a Money
|
129
|
+
# instance, not a String):
|
130
|
+
|
131
|
+
fifty_eur.convert_to('USD') # => 55.50 USD
|
132
|
+
```
|
133
|
+
|
134
|
+
```
|
135
|
+
# Perform operations in different currencies:
|
136
|
+
|
137
|
+
twenty_dollars = Money.new(20, 'USD')
|
138
|
+
|
139
|
+
# Arithmetics:
|
140
|
+
|
141
|
+
fifty_eur + twenty_dollars # => 68.02 EUR
|
142
|
+
fifty_eur - twenty_dollars # => 31.98 EUR
|
143
|
+
fifty_eur / 2 # => 25 EUR
|
144
|
+
twenty_dollars * 3 # => 60 USD
|
145
|
+
```
|
146
|
+
|
147
|
+
```
|
148
|
+
# Comparisons (also in different currencies):
|
149
|
+
|
150
|
+
twenty_dollars == Money.new(20, 'USD') # => true
|
151
|
+
twenty_dollars == Money.new(30, 'USD') # => false
|
152
|
+
|
153
|
+
fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
154
|
+
fifty_eur_in_usd == fifty_eur # => true
|
155
|
+
|
156
|
+
twenty_dollars > Money.new(5, 'USD') # => true
|
157
|
+
twenty_dollars < fifty_eur # => true
|
158
|
+
```
|
159
|
+
|
160
|
+
## Development
|
161
|
+
|
162
|
+
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.
|
163
|
+
|
164
|
+
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).
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mpakus/mint-money.
|
169
|
+
|
170
|
+
[![CircleCI](https://circleci.com/gh/mpakus/mint-money.svg?style=svg)](https://circleci.com/gh/mpakus/mint-money)
|
171
|
+
|
172
|
+
email:
|
173
|
+
- mrak69@gmail.com
|
174
|
+
executables: []
|
175
|
+
extensions: []
|
176
|
+
extra_rdoc_files: []
|
177
|
+
files:
|
178
|
+
- ".gitignore"
|
179
|
+
- ".rspec"
|
180
|
+
- ".rubocop.yml"
|
181
|
+
- ".travis.yml"
|
182
|
+
- Gemfile
|
183
|
+
- README.md
|
184
|
+
- Rakefile
|
185
|
+
- bin/console
|
186
|
+
- bin/setup
|
187
|
+
- circle.yml
|
188
|
+
- lib/mint/money.rb
|
189
|
+
- lib/mint/money/currency.rb
|
190
|
+
- lib/mint/money/exceptions.rb
|
191
|
+
- lib/mint/money/money.rb
|
192
|
+
- lib/mint/money/utils.rb
|
193
|
+
- lib/mint/money/version.rb
|
194
|
+
- mint-money.gemspec
|
195
|
+
homepage: https://github.com/mpakus/mint-money
|
196
|
+
licenses: []
|
197
|
+
metadata: {}
|
198
|
+
post_install_message:
|
199
|
+
rdoc_options: []
|
200
|
+
require_paths:
|
201
|
+
- lib
|
202
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
|
+
requirements:
|
209
|
+
- - ">="
|
210
|
+
- !ruby/object:Gem::Version
|
211
|
+
version: '0'
|
212
|
+
requirements: []
|
213
|
+
rubyforge_project:
|
214
|
+
rubygems_version: 2.6.10
|
215
|
+
signing_key:
|
216
|
+
specification_version: 4
|
217
|
+
summary: Manage money with Mint::Money
|
218
|
+
test_files: []
|