magic_money 0.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 +7 -0
- data/lib/magic_money.rb +46 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4df3aa1b760c4164aea56c4190278e5287612aea
|
4
|
+
data.tar.gz: d0cd121731127e7cb465236377ac04bba71a7a98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03da7c8f3ff0623841bf19425a45950c7f5ab0c378e5a2822fcb571e296b8b9a92e1fade95dbf9fcf7e7f567e1f68f34c1d72bb4605531e62bf01ab70f110b09
|
7
|
+
data.tar.gz: 69fa46a7433dc20c05ae64194d81b87fd8814d564a7a4df3950b98710d0160fe7c23e0412138eb9afb4b6b9f5d3193135fd3db68d125699658b6e69f936bcad6
|
data/lib/magic_money.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
class MagicMoney
|
2
|
+
attr_accessor :amount, :currency
|
3
|
+
|
4
|
+
RATES = { 'EUR' => { 'USD' => 1.11, 'Bitcoin' => 0.0047 },
|
5
|
+
'USD' => { 'EUR' => 0.9009, 'Bitcoin' => 0.0042 },
|
6
|
+
'Bitcoin' => { 'EUR' => 212.7659, 'USD' => 238.0952 } }
|
7
|
+
|
8
|
+
def initialize(amount, currency)
|
9
|
+
validate_currency(currency)
|
10
|
+
@amount = amount
|
11
|
+
@currency = currency
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"#{ amount_as_money } #{ @currency }"
|
16
|
+
end
|
17
|
+
|
18
|
+
def convert_to(new_currency)
|
19
|
+
validate_currency(new_currency)
|
20
|
+
MagicMoney.new(amount_in(new_currency), new_currency)
|
21
|
+
end
|
22
|
+
|
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
|
+
private
|
31
|
+
def amount_as_money
|
32
|
+
'%.2f' % @amount
|
33
|
+
end
|
34
|
+
|
35
|
+
def amount_in(new_currency)
|
36
|
+
new_currency == @currency ? @amount : (@amount.to_f * RATES[@currency][new_currency]).round(2)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_operation_param(value)
|
40
|
+
value.kind_of?(MagicMoney) ? value.convert_to(@currency).amount : value
|
41
|
+
end
|
42
|
+
|
43
|
+
def validate_currency(currency)
|
44
|
+
raise ArgumentError, 'Invalid currency' if RATES[currency].nil?
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magic_money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anezio Campos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Convert money values from different currencies
|
14
|
+
email: newdevas@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/magic_money.rb
|
20
|
+
homepage:
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Your favorite currency converter
|
44
|
+
test_files: []
|