money_handler 0.0.0
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/money.rb +95 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4f16cc0dfebe036bd69686724681376754f7da84
|
|
4
|
+
data.tar.gz: 4bbae35f71e8d0be7a20d2f363a0ca4a41ad3472
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4181bddadc7a267c2ac49c098ad24d73f4b9ee06a1b7b8877fa528524775433757814165fde4119ba738e1058f795ed1be0874a9b32fa53549662e6d40f3c4a5
|
|
7
|
+
data.tar.gz: 4879f1ce4e8f7324eb21d3223c8ffbfbfa9e2397ce130018edf83c2f694b86ffba3cfd56169df8c10ceef106d01d59d7f2d1e724fdee1979c82575546bc8a4f5
|
data/lib/money.rb
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
attr_accessor :amount, :currency
|
|
3
|
+
|
|
4
|
+
#Set defaults
|
|
5
|
+
@@base_currency = 'EUR'
|
|
6
|
+
@@conversion_rates = {
|
|
7
|
+
'EUR' => 1,
|
|
8
|
+
'USD' => 1.11,
|
|
9
|
+
'Bitcoin' => 0.0047
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#Constructor
|
|
13
|
+
def initialize(amount, currency)
|
|
14
|
+
@amount = amount.to_f
|
|
15
|
+
@currency = currency
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.conversion_rates(base_currency, conversion_rates)
|
|
19
|
+
@@base_currency = base_currency
|
|
20
|
+
@@conversion_rates = conversion_rates
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
#Convert method
|
|
24
|
+
def convert_to(to_currency)
|
|
25
|
+
if @@conversion_rates[to_currency].nil?
|
|
26
|
+
puts 'Rate not found'
|
|
27
|
+
else
|
|
28
|
+
from_currency = self.currency
|
|
29
|
+
rate = Money.getRate(from_currency, to_currency)
|
|
30
|
+
|
|
31
|
+
converted_amount = self.amount * rate
|
|
32
|
+
|
|
33
|
+
Money.new(converted_amount, to_currency)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
#Arithmetics
|
|
38
|
+
def other_amount(other)
|
|
39
|
+
rate = Money.getRate(other.currency, self.currency)
|
|
40
|
+
other.amount * rate
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def +(other)
|
|
44
|
+
Money.new(self.amount + other_amount(other), self.currency)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def -(other)
|
|
48
|
+
Money.new(self.amount - other_amount(other), self.currency)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def *(number)
|
|
52
|
+
Money.new(self.amount * number.to_f, self.currency)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def /(number)
|
|
56
|
+
Money.new(self.amount / number.to_f, self.currency)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#Comparisons
|
|
60
|
+
def ==(other)
|
|
61
|
+
temp = other.convert_to(self.currency)
|
|
62
|
+
self.amount.round(2) == temp.amount.round(2)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def >(other)
|
|
66
|
+
temp = other.convert_to(self.currency)
|
|
67
|
+
self.amount.round(2) > temp.amount.round(2)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def <(other)
|
|
71
|
+
temp = other.convert_to(self.currency)
|
|
72
|
+
self.amount.round(2) < temp.amount.round(2)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def inspect
|
|
76
|
+
sprintf('%.2f %s', amount, currency)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def to_s
|
|
80
|
+
sprintf('%.2f %s', amount, currency)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.getRate(from_currency, to_currency)
|
|
84
|
+
return 1 if to_currency == from_currency
|
|
85
|
+
|
|
86
|
+
if !@@conversion_rates[to_currency].nil? && from_currency == @@base_currency
|
|
87
|
+
return @@conversion_rates[to_currency]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
if !@@conversion_rates[from_currency].nil? && to_currency == @@base_currency
|
|
91
|
+
return 1.0 / @@conversion_rates[from_currency]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: money_handler
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Francisco Di Persia
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-01-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email: fdipersia@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/money.rb
|
|
20
|
+
homepage: https://github.com/Fdipersia/money
|
|
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.6.14.1
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: This gem handles different currencies
|
|
44
|
+
test_files: []
|