converrency 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/lib/converrency.rb +1 -4
- data/lib/converrency/money.rb +72 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 401fc5169ee4a8ea836d6c95d223174fa13f07f7
|
|
4
|
+
data.tar.gz: 04aa5fbfdebddedc811fc15cd74a301ad0187845
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 922dc3fb76649bcc5748dfc73ffe85bb45e0da5ce6ec6709627ba382dec3d3d20222ad7c804b7504449918a4f2e9d8f4e0c47d0f5619878473164002e30999f6
|
|
7
|
+
data.tar.gz: 6ef621411b8a0d2255fdc0560aa855409bf8f7ca8ce3c0551dee9773106adfda5a7119c65090d501d881cb8555c1c8e9ce31a74f200ee7113bcfc13e050abea0
|
data/lib/converrency.rb
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module Converrency
|
|
2
|
+
class Money
|
|
3
|
+
|
|
4
|
+
attr_reader :currency, :amount
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
attr_reader :base_currency, :currencies
|
|
8
|
+
|
|
9
|
+
def currency_rates(base_currency, currencies)
|
|
10
|
+
@base_currency = base_currency
|
|
11
|
+
@currencies = currencies.merge(base_currency => 1.0)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def valid_currency?(currency)
|
|
15
|
+
base_currency == currency || currencies.has_key?(currency)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(amount, currency)
|
|
20
|
+
unless self.class.valid_currency?(currency)
|
|
21
|
+
raise ArgumentError.new('Invalid currency')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
@amount = amount
|
|
25
|
+
@currency = currency
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def inspect
|
|
29
|
+
"#{print_amount} #{currency}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def convert_to(new_currency)
|
|
33
|
+
converted_amount = value_in_base_currency * currencies[new_currency]
|
|
34
|
+
Money.new(converted_amount, new_currency)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
[:+, :-, :*, :/].each do |meth|
|
|
38
|
+
define_method(meth) do |money|
|
|
39
|
+
new_amount = amount.send(meth, money.convert_to(currency).amount)
|
|
40
|
+
Money.new(new_amount, currency)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
[:==, :<, :>, :<=, :>=].each do |meth|
|
|
45
|
+
define_method(meth) do |money|
|
|
46
|
+
rounded_amount.send(meth, money.convert_to(currency).rounded_amount)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def rounded_amount
|
|
51
|
+
amount.round(2)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def print_amount
|
|
57
|
+
"%.2f" % rounded_amount
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def value_in_base_currency
|
|
61
|
+
amount / currencies[currency]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def base_currency
|
|
65
|
+
self.class.base_currency
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def currencies
|
|
69
|
+
self.class.currencies
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: converrency
|
|
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
|
- Sam Raife
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-01-
|
|
11
|
+
date: 2018-01-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email:
|
|
@@ -17,6 +17,7 @@ extensions: []
|
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
19
|
- lib/converrency.rb
|
|
20
|
+
- lib/converrency/money.rb
|
|
20
21
|
homepage:
|
|
21
22
|
licenses:
|
|
22
23
|
- MIT
|