money-convert 0.1.0 → 0.1.1
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 +10 -8
- data/lib/money-convert/money-convert.rb +59 -21
- data/lib/money-convert/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: 959345f9cce96a73647d6375e852d12329a1db3688518b36bfd26706b288ec87
|
|
4
|
+
data.tar.gz: 47feabe88d29ddb8ba9d06cb4008ee82b773e515b184f2471ddbdc29e24a77da
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1841871c04764b1a6aa43fa2ff5beac2dba0525336eea92825380eb207bd3ec0822d72571bbb9c55bf6015758d5fb4df87dbf6291cf5487693cef10e28687a9c
|
|
7
|
+
data.tar.gz: 919ab668e447e65ab758d6c03b41aee08a4cefff125508a5b7fbb0cb0218e32aa7f0a1b81c572d6d8ebd6b8a233023b1d194d943a034fde5c2a2331b4ee9c1ea
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Money::Convert
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
|
|
3
5
|
## Installation
|
|
4
6
|
|
|
5
7
|
Add this line to your application's Gemfile:
|
|
@@ -21,14 +23,14 @@ Or install it yourself as:
|
|
|
21
23
|
```ruby
|
|
22
24
|
# Configure the currency rates with respect to a base currency (here EUR):
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
MoneyConvert.conversion_rates('EUR', {
|
|
25
27
|
'USD' => 1.11,
|
|
26
28
|
'Bitcoin' => 0.0047
|
|
27
29
|
})
|
|
28
30
|
|
|
29
|
-
# Instantiate money objects:
|
|
31
|
+
# Instantiate money convert objects:
|
|
30
32
|
|
|
31
|
-
fifty_eur =
|
|
33
|
+
fifty_eur = MoneyConvert.new(50, 'EUR')
|
|
32
34
|
|
|
33
35
|
# Get amount and currency:
|
|
34
36
|
|
|
@@ -43,7 +45,7 @@ fifty_eur.convert_to('USD') # => 55.50 USD
|
|
|
43
45
|
|
|
44
46
|
# Perform operations in different currencies:
|
|
45
47
|
|
|
46
|
-
twenty_dollars =
|
|
48
|
+
twenty_dollars = MoneyConvert.new(20, 'USD')
|
|
47
49
|
|
|
48
50
|
# Arithmetics:
|
|
49
51
|
|
|
@@ -54,19 +56,19 @@ twenty_dollars * 3 # => 60 USD
|
|
|
54
56
|
|
|
55
57
|
# Comparisons (also in different currencies):
|
|
56
58
|
|
|
57
|
-
twenty_dollars ==
|
|
58
|
-
twenty_dollars ==
|
|
59
|
+
twenty_dollars == MoneyConvert.new(20, 'USD') # => true
|
|
60
|
+
twenty_dollars == MoneyConvert.new(30, 'USD') # => false
|
|
59
61
|
|
|
60
62
|
fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
|
61
63
|
fifty_eur_in_usd == fifty_eur # => true
|
|
62
64
|
|
|
63
|
-
twenty_dollars >
|
|
65
|
+
twenty_dollars > MoneyConvert.new(5, 'USD') # => true
|
|
64
66
|
twenty_dollars < fifty_eur # => true
|
|
65
67
|
```
|
|
66
68
|
|
|
67
69
|
## Contributing
|
|
68
70
|
|
|
69
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/varmad/money-convert. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/money-convert/blob/master/CODE_OF_CONDUCT.md).
|
|
70
72
|
|
|
71
73
|
|
|
72
74
|
## License
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
require_relative
|
|
1
|
+
require_relative 'conversion_rates'
|
|
2
2
|
|
|
3
|
+
# Class for converting money from one currency to another
|
|
4
|
+
# and perform some arthimetic operations on two MoneyConvert objects.
|
|
3
5
|
class MoneyConvert
|
|
4
6
|
attr_accessor :amount, :currency, :base_currency, :to_currencies
|
|
5
7
|
|
|
@@ -12,6 +14,12 @@ class MoneyConvert
|
|
|
12
14
|
"#{@amount} #{@currency}"
|
|
13
15
|
end
|
|
14
16
|
|
|
17
|
+
# Returns class object with converted currency values
|
|
18
|
+
# For example:
|
|
19
|
+
# fifty_eur = MoneyConvert.new(50, 'EUR')
|
|
20
|
+
# fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
|
21
|
+
# => fifty_eur_in_usd.currency
|
|
22
|
+
# => fifty_eur_in_usd.amount
|
|
15
23
|
def convert_to(to_currency)
|
|
16
24
|
return unless validate_currency(to_currency) && @@base_currency == currency
|
|
17
25
|
|
|
@@ -19,36 +27,47 @@ class MoneyConvert
|
|
|
19
27
|
self.class.new(toal_amount, to_currency)
|
|
20
28
|
end
|
|
21
29
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
converted_amount = convert(other.currency)
|
|
37
|
-
converted_amount - amount.to_i
|
|
30
|
+
# Return sum of two MoneyConvert object, if +other+ object has different currency
|
|
31
|
+
# it will get converted into this object currency
|
|
32
|
+
# For Example:
|
|
33
|
+
# MoneyConvert.new(50, 'EUR') + MoneyConvert.new(20, 'USD')
|
|
34
|
+
# MoneyConvert.new(50, 'EUR') - MoneyConvert.new(20, 'USD')
|
|
35
|
+
# => returns amount in EUR currency
|
|
36
|
+
%i[+, -].each do |method|
|
|
37
|
+
define_method(method) do |other|
|
|
38
|
+
return unless currency == @@base_currency
|
|
39
|
+
|
|
40
|
+
converted_amount = convert(other.currency)
|
|
41
|
+
converted_amount - amount.to_i
|
|
42
|
+
end
|
|
38
43
|
end
|
|
39
44
|
|
|
45
|
+
# Return division of two MoneyConvert object, if +other+ object has different currency
|
|
46
|
+
# it will get converted into this object currency
|
|
47
|
+
# For Example:
|
|
48
|
+
# MoneyConvert.new(50, 'EUR') / MoneyConvert.new(20, 'USD')
|
|
49
|
+
# => returns amount in EUR currency
|
|
40
50
|
def /(other)
|
|
41
51
|
return unless other.is_a?(Integer)
|
|
42
52
|
|
|
43
53
|
amount.to_i / other
|
|
44
54
|
end
|
|
45
55
|
|
|
56
|
+
# Return multiplication of two MoneyConvert object, if +other+ object has different currency
|
|
57
|
+
# it will get converted into this object currency
|
|
58
|
+
# For Example:
|
|
59
|
+
# MoneyConvert.new(50, 'EUR') * MoneyConvert.new(20, 'USD')
|
|
60
|
+
# => returns amount in EUR currency
|
|
46
61
|
def *(other)
|
|
47
62
|
return unless other.is_a?(Integer)
|
|
48
63
|
|
|
49
64
|
amount.to_i * other
|
|
50
65
|
end
|
|
51
66
|
|
|
67
|
+
# Return true if +other+ object and this object has same amount and currency
|
|
68
|
+
# For Example:
|
|
69
|
+
# MoneyConvert.new(50, 'EUR') == MoneyConvert.new(50, 'EUR')
|
|
70
|
+
# => returns boolean
|
|
52
71
|
def ==(other)
|
|
53
72
|
if amount == other.amount && currency == other.currency
|
|
54
73
|
true
|
|
@@ -57,6 +76,11 @@ class MoneyConvert
|
|
|
57
76
|
end
|
|
58
77
|
end
|
|
59
78
|
|
|
79
|
+
# Return true if +other+ object amount is gtater than this object amount,
|
|
80
|
+
# and the currency should be same
|
|
81
|
+
# For Example:
|
|
82
|
+
# MoneyConvert.new(50, 'EUR') > MoneyConvert.new(30, 'EUR')
|
|
83
|
+
# => returns boolean
|
|
60
84
|
def >(other)
|
|
61
85
|
if amount > other.amount && currency == other.currency
|
|
62
86
|
true
|
|
@@ -65,6 +89,11 @@ class MoneyConvert
|
|
|
65
89
|
end
|
|
66
90
|
end
|
|
67
91
|
|
|
92
|
+
# Return true if +other+ object amount is less than this object amount,
|
|
93
|
+
# and the currency should be same
|
|
94
|
+
# For Example:
|
|
95
|
+
# MoneyConvert.new(20, 'EUR') < MoneyConvert.new(30, 'EUR')
|
|
96
|
+
# => returns boolean
|
|
68
97
|
def <(other)
|
|
69
98
|
if amount < other.amount && currency == other.currency
|
|
70
99
|
true
|
|
@@ -77,13 +106,22 @@ class MoneyConvert
|
|
|
77
106
|
end
|
|
78
107
|
end
|
|
79
108
|
|
|
80
|
-
|
|
81
|
-
@@to_currencies&.keys&.include?(to_currency)
|
|
82
|
-
end
|
|
83
|
-
|
|
109
|
+
# Sets the +@@base_currency+ and +@@to_currencies+ class valiables
|
|
84
110
|
def self.conversion_rates(base_currency, to_currencies)
|
|
85
111
|
conversion_rates = MoneyConvert::ConversionRates.new(base_currency, to_currencies)
|
|
86
112
|
@@base_currency = conversion_rates.base_currency
|
|
87
113
|
@@to_currencies = conversion_rates.to_currencies
|
|
88
114
|
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# Returns converted currency
|
|
119
|
+
def convert(to_currency)
|
|
120
|
+
(@amount.to_i * @@to_currencies.fetch(to_currency, 1)).round(2)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Checks wether the +to_currency+ is present in the given +@@to_curencies+ hash
|
|
124
|
+
def validate_currency(to_currency)
|
|
125
|
+
@@to_currencies&.keys&.include?(to_currency) || to_currency == @@base_currency
|
|
126
|
+
end
|
|
89
127
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: money-convert
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Srinivasa Varma
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-09-
|
|
11
|
+
date: 2021-09-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email:
|