money_test 0.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/exceptions.rb +7 -0
- data/lib/money_test.rb +117 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f7da3fd00e59d2e414d41c3d50d46c9265ea1063
|
4
|
+
data.tar.gz: 5f5ac5948a0936f72b9c65c693c81bd0c9b261ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abb336a681351a594264662c1b56678173c3837a73eb27c0efdf71fbd6b4e45110e6a25cc4231f62085d1ee9b61001e2c77b00ca1aadf7b6b94b7f16241efd1e
|
7
|
+
data.tar.gz: f64a7c502371c5b218e1936c30c211e7a3816587f5db0eba1622ec7f49f7e55d078b5c201634787c262fcb585160b094e4e7f22e22986be26d1423d66c0370cb
|
data/lib/exceptions.rb
ADDED
data/lib/money_test.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# This is the main class for saving the monetary value and handling
|
2
|
+
# different operations
|
3
|
+
|
4
|
+
require 'exceptions'
|
5
|
+
|
6
|
+
class Money
|
7
|
+
attr_accessor :amount, :currency
|
8
|
+
|
9
|
+
# Setting Euro as the default base currency
|
10
|
+
@@base_currency = 'EUR'
|
11
|
+
@@conversion_rates = {}
|
12
|
+
|
13
|
+
# Constructor taking the money amount and the currency
|
14
|
+
def initialize(amount, currency)
|
15
|
+
@amount = amount.to_f
|
16
|
+
@currency = currency
|
17
|
+
end
|
18
|
+
|
19
|
+
# Setting the different conversion rates with respect to
|
20
|
+
# the base currency
|
21
|
+
# == Parameters:
|
22
|
+
# base_currency::
|
23
|
+
# The base currency upon which the conversion rates should
|
24
|
+
# calculate, example: 'EUR', 'USD'
|
25
|
+
# conversion_rates::
|
26
|
+
# A hash containing the different exchange rates' currencies
|
27
|
+
# and values with respect to the base currency, example:
|
28
|
+
# {
|
29
|
+
# 'USD' => 1.11,
|
30
|
+
# 'Bitcoin' => 0.0047
|
31
|
+
# }
|
32
|
+
def self.conversion_rates(base_currency, conversion_rates)
|
33
|
+
@@base_currency = base_currency
|
34
|
+
@@conversion_rates = conversion_rates
|
35
|
+
end
|
36
|
+
|
37
|
+
# Converting the amount in the calling object upon the currency
|
38
|
+
# entered, with respect to the base currency
|
39
|
+
# == Returns:
|
40
|
+
# A money object with the new amount and currency
|
41
|
+
def convert_to(to_currency)
|
42
|
+
from_currency = self.currency
|
43
|
+
rate = Money.getRate(from_currency, to_currency)
|
44
|
+
|
45
|
+
converted_amount = self.amount * rate
|
46
|
+
|
47
|
+
Money.new(converted_amount, to_currency)
|
48
|
+
end
|
49
|
+
|
50
|
+
# overriding arithmetic operators
|
51
|
+
def +(other)
|
52
|
+
rate = Money.getRate(other.currency, self.currency)
|
53
|
+
new_other_amount = other.amount * rate
|
54
|
+
Money.new(self.amount + new_other_amount, self.currency)
|
55
|
+
end
|
56
|
+
|
57
|
+
def -(other)
|
58
|
+
rate = Money.getRate(other.currency, self.currency)
|
59
|
+
new_other_amount = other.amount * rate
|
60
|
+
Money.new(self.amount - new_other_amount, self.currency)
|
61
|
+
end
|
62
|
+
|
63
|
+
def *(value)
|
64
|
+
Money.new(self.amount * value.to_f, self.currency)
|
65
|
+
end
|
66
|
+
|
67
|
+
def /(value)
|
68
|
+
Money.new(self.amount / value.to_f, self.currency)
|
69
|
+
end
|
70
|
+
# --------------------------------
|
71
|
+
|
72
|
+
# overriding comparisons operators, to the nearest 2 decimal places
|
73
|
+
def ==(other)
|
74
|
+
temp = other.convert_to(self.currency)
|
75
|
+
return self.amount.round(2) == temp.amount.round(2)
|
76
|
+
end
|
77
|
+
|
78
|
+
def >(other)
|
79
|
+
temp = other.convert_to(self.currency)
|
80
|
+
return self.amount.round(2) > temp.amount.round(2)
|
81
|
+
end
|
82
|
+
|
83
|
+
def <(other)
|
84
|
+
temp = other.convert_to(self.currency)
|
85
|
+
return self.amount.round(2) < temp.amount.round(2)
|
86
|
+
end
|
87
|
+
# --------------------------------
|
88
|
+
|
89
|
+
def inspect
|
90
|
+
return sprintf('%.2f %s', amount, currency)
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
return sprintf('%.2f %s', amount, currency)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Getting the exchange rate between 2 currencies
|
98
|
+
# == Returns:
|
99
|
+
# - 1 if the 2 currencies are the same
|
100
|
+
# - the required rate, even if the 2 currencies are swaped
|
101
|
+
# (return inverse the value)
|
102
|
+
# == Raises:
|
103
|
+
# - RateNotFound if no rates are set for those 2 currencies
|
104
|
+
def self.getRate(from_currency, to_currency)
|
105
|
+
return 1 if to_currency == from_currency
|
106
|
+
|
107
|
+
if !@@conversion_rates[to_currency].nil? && from_currency == @@base_currency
|
108
|
+
return @@conversion_rates[to_currency]
|
109
|
+
end
|
110
|
+
|
111
|
+
if !@@conversion_rates[from_currency].nil? && to_currency == @@base_currency
|
112
|
+
return 1.0 / @@conversion_rates[from_currency]
|
113
|
+
end
|
114
|
+
|
115
|
+
raise Exceptions::RateNotFound
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: money_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hussein Abu Maash
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Gem for finding the exchange rate between 2 monetary values along with
|
28
|
+
arithmetic manipulations
|
29
|
+
email: hussein.abumaash@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/exceptions.rb
|
35
|
+
- lib/money_test.rb
|
36
|
+
homepage: http://github.com/HusseinReda/MoneyTest
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.6.12
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Monetary
|
60
|
+
test_files: []
|