changer 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/changer.rb +24 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f3bc668b3b0b06f59c8f02aaf742340dd7cc08e7c43c41d0b85ee839ff9f15d
|
4
|
+
data.tar.gz: cd3e7d832d5f40dce55573aaf4c45ccac66cf5250058cb21104e2c8d0686b87f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f2405a0740383d2a3f5d46772d5357799418bbf0df7a8b922dfd2c53ed03a53180de3cdc7e9a3f82a9b3b6aa614b394ab19e73efaa77f1de71542e99a458b34
|
7
|
+
data.tar.gz: 7e01731d16e09ed01a8f3f68f3c10f3e9541dfd70e80b1d88e28a345cb493b78498e97efc8d61c3f99d9dd845f9c7f2ca1e7c537ceeb326deeaf8d6c68ed19c2
|
data/lib/changer.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require_relative 'currency'
|
2
2
|
class Currency
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
attr_reader :type, :value
|
4
|
+
currency_symbols = [:usd, :eur, :jpy, :gbp, :aud, :cad, :cny, :hkd, :nzd, :chf]
|
5
|
+
|
6
|
+
currency_strings = currency_symbols.map { |sym| sym.to_s.upcase }
|
7
|
+
|
8
|
+
TYPES = currency_symbols + currency_strings
|
9
|
+
|
6
10
|
# Create a new Currency object
|
7
11
|
#
|
8
12
|
#
|
@@ -26,25 +30,26 @@ class Currency
|
|
26
30
|
# @param to_type [Symbol]
|
27
31
|
# @return [Currency] || nil if invalid
|
28
32
|
def convert(to_type)
|
29
|
-
return nil unless
|
30
|
-
|
33
|
+
return nil unless valid_type?(to_type)
|
31
34
|
new_value = value * Currency.fetch_rate(type, to_type)
|
32
35
|
Currency.new(to_type, new_value)
|
33
36
|
end
|
34
37
|
# Convert to a different currency at a specified rate
|
35
38
|
# @param to_type [Symbol]
|
36
39
|
# @param rate [Numeric]
|
40
|
+
# @return [Currency] || nil if invalid
|
37
41
|
# @note rate is set based on the calling object as a base
|
38
42
|
def convert_at(to_type, rate)
|
39
|
-
return nil unless
|
43
|
+
return nil unless valid_type?(to_type)
|
40
44
|
|
41
45
|
new_value = value * rate
|
42
46
|
Currency.new(to_type, new_value)
|
43
47
|
end
|
44
48
|
# Convert the calling object to the currency specified by the to_type param
|
45
49
|
# @param to_type [Symbol]
|
50
|
+
# @return [self]
|
46
51
|
def convert!(to_type)
|
47
|
-
return nil unless
|
52
|
+
return nil unless valid_type?(to_type)
|
48
53
|
|
49
54
|
self.value = value * Currency.fetch_rate(type, to_type)
|
50
55
|
@type = to_type
|
@@ -53,8 +58,9 @@ class Currency
|
|
53
58
|
# Convert the calling object to the currency specified by the to_type param
|
54
59
|
# at the rate specified by the rate parameter
|
55
60
|
# @param rate [Numeric]
|
61
|
+
# @return [self]
|
56
62
|
def convert_at!(to_type, rate)
|
57
|
-
return nil unless
|
63
|
+
return nil unless valid_type?(to_type)
|
58
64
|
|
59
65
|
self.value = value * rate
|
60
66
|
@type = to_type
|
@@ -73,24 +79,28 @@ class Currency
|
|
73
79
|
|
74
80
|
# All of the math operations return values in the type of currency
|
75
81
|
# calling the method ie: USD + GBP = USD, but GBP + USD = GBP
|
82
|
+
# @return [Currency]
|
76
83
|
def +(other)
|
77
84
|
first_type = self.type
|
78
85
|
new_value = (value + other.convert(first_type).value)
|
79
86
|
Currency.new(first_type, new_value)
|
80
87
|
end
|
81
88
|
|
89
|
+
# @return [Currency]
|
82
90
|
def -(other)
|
83
91
|
first_type = self.type
|
84
92
|
new_value = (value - other.convert(first_type).value)
|
85
93
|
Currency.new(first_type, new_value)
|
86
94
|
end
|
87
95
|
|
96
|
+
# @return [Currency]
|
88
97
|
def *(other)
|
89
98
|
first_type = self.type
|
90
99
|
new_value = (value * other.convert(first_type).value)
|
91
100
|
Currency.new(first_type, new_value)
|
92
101
|
end
|
93
102
|
|
103
|
+
# @return [Currency]
|
94
104
|
def /(other)
|
95
105
|
first_type = self.type
|
96
106
|
new_value = (value / other.convert(first_type).value)
|
@@ -99,6 +109,7 @@ class Currency
|
|
99
109
|
# Fetch the current exchange rate using the from_type as the base
|
100
110
|
# @param from_type [Symbol]
|
101
111
|
# @param to_type [Symbol]
|
112
|
+
# @return [Float]
|
102
113
|
def self.fetch_rate(from_type, to_type)
|
103
114
|
changer = Changer.new(from_type, to_type)
|
104
115
|
changer.rate
|
@@ -106,6 +117,10 @@ class Currency
|
|
106
117
|
|
107
118
|
private
|
108
119
|
|
120
|
+
def valid_type?(type)
|
121
|
+
TYPES.include?(type.to_s.upcase)
|
122
|
+
end
|
123
|
+
|
109
124
|
def format_string
|
110
125
|
amount = add_commas(format("%.2f",value))
|
111
126
|
"#{type.to_s.upcase} #{amount}"
|
@@ -116,9 +131,7 @@ class Currency
|
|
116
131
|
end
|
117
132
|
|
118
133
|
def verify_valid?(to_type, amount)
|
119
|
-
|
120
|
-
|
121
|
-
amount >= 0.0 && TYPES.include?(to_type)
|
134
|
+
amount.is_a?(Numeric) && valid_type?(to_type) && amount >= 0.0
|
122
135
|
end
|
123
136
|
|
124
137
|
def add_commas(number)
|