magic_money 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/magic_money.rb +4 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0701335975bac02888478471c5e13be5032e4e51
|
4
|
+
data.tar.gz: fee872e86645ea2a69bbb80a06c79c96d08c04c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 063570c93d3d8c274ad23188cad3c49ee45f6aaa50c379ac9ce85b43fb5f51558af046fda80e78788b80e51e9c133653c4d05b7dcd99bdea24ef4deabbf2168d
|
7
|
+
data.tar.gz: 9f1a4b57b67768e2355e4de5aa861a2fe3e2fd6a952999bc1ab75225efdfe2fa9d435f4d0822556a545528b228e2d6541e8584d2f91641f755c912cc46e83175
|
data/lib/magic_money.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
require 'magic_money/operators'
|
2
|
+
|
1
3
|
class MagicMoney
|
4
|
+
include Operators
|
5
|
+
|
2
6
|
attr_accessor :amount, :currency
|
3
7
|
|
4
8
|
RATES = { 'EUR' => { 'USD' => 1.11, 'Bitcoin' => 0.0047 },
|
@@ -20,13 +24,6 @@ class MagicMoney
|
|
20
24
|
MagicMoney.new(amount_in(new_currency), new_currency)
|
21
25
|
end
|
22
26
|
|
23
|
-
[:/, :*, :+, :-, :==, :>, :<].each do |operator|
|
24
|
-
define_method(operator) do |value|
|
25
|
-
result = @amount.send(operator, parse_operation_param(value))
|
26
|
-
[true, false].include?(result) ? result : "#{ result } #{ @currency }"
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
27
|
private
|
31
28
|
def amount_as_money
|
32
29
|
'%.2f' % @amount
|
@@ -36,10 +33,6 @@ class MagicMoney
|
|
36
33
|
new_currency == @currency ? @amount : (@amount.to_f * RATES[@currency][new_currency]).round(2)
|
37
34
|
end
|
38
35
|
|
39
|
-
def parse_operation_param(value)
|
40
|
-
value.kind_of?(MagicMoney) ? value.convert_to(@currency).amount : value
|
41
|
-
end
|
42
|
-
|
43
36
|
def validate_currency(currency)
|
44
37
|
raise ArgumentError, 'Invalid currency' if RATES[currency].nil?
|
45
38
|
end
|