Money_DaWanda 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a618363f88504388625c83a453f78bafe5e6311d
4
+ data.tar.gz: 26a953607ef2f4bacd3ab1b7be56e1afe0b48581
5
+ SHA512:
6
+ metadata.gz: a7ac19669a59219e9c964a75256601f8e07f8d72952c1ff6251b2423891b246f73fce01f9f2e398a97219c89d850b4c7596d8afc64db12702af1abd135545819
7
+ data.tar.gz: dc173e3f3e0d3218220069158c76d34b9a6c7f09bdb06ddc8ca446133db4c677780a6baffe5381a19d8afd9f80d14f4f2c7b0228966aa82d891b7889eae961d2
@@ -0,0 +1,60 @@
1
+ require "Money_DaWanda/arithmetics"
2
+ require "Money_DaWanda/comparisons"
3
+
4
+ module Money_DaWanda
5
+ class Money
6
+
7
+ include Arithmetics
8
+ include Comparisons
9
+
10
+ attr_accessor :amount, :currency
11
+
12
+ @@conversion_rates = {}
13
+
14
+ def initialize(amount, currency)
15
+ raise ArgumentError, 'Amount is not numeric' unless amount.is_a? Numeric
16
+ raise ArgumentError, 'Currency should be a string' unless currency.is_a? String
17
+
18
+ @amount = amount
19
+ @currency = currency
20
+ end
21
+
22
+ def inspect
23
+ "#{'%.2f' % amount} #{currency}"
24
+ end
25
+
26
+ def self.add_conversion_rates(currency_base, rates)
27
+ raise ArgumentError, 'Currency should be a string' unless currency_base.is_a? String
28
+ raise ArgumentError, 'Rates should be a hash' unless rates.is_a? Hash
29
+
30
+ @@conversion_rates[currency_base] = rates
31
+
32
+ end
33
+
34
+ def self.show_conversion_rates
35
+ @@conversion_rates.each do |currency_base,rates|
36
+ puts currency_base + ' => ' + rates +'/n----------/n'
37
+ end
38
+ end
39
+
40
+ def convert_to(new_currency)
41
+ raise ArgumentError, 'Currency should be a string' unless new_currency.is_a? String
42
+
43
+ if @currency == new_currency
44
+ rate = 1
45
+ # check if conversion rate exists inside @@conversion_rate table
46
+ elsif @@conversion_rates.has_key?(@currency) && @@conversion_rates[@currency].has_key?(new_currency)
47
+ rate = @@conversion_rates[@currency][new_currency]
48
+ #if not, but reversed rate exists, use it
49
+ elsif @@conversion_rates.has_key?(new_currency) && @@conversion_rates[new_currency].has_key?(@currency)
50
+ rate = 1 / @@conversion_rates[new_currency][@currency]
51
+ else
52
+ return "This currency exchange is unfortunately not available"
53
+ end
54
+
55
+ new_amount = (@amount * rate).round(2)
56
+ return Money.new(new_amount,new_currency)
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ module Money_DaWanda
2
+ module Arithmetics
3
+
4
+ def +(money)
5
+ if money.currency != @currency && money.amount != 0
6
+ return Money.new(@amount + (money.convert_to(@currency)).amount, @currency)
7
+ elsif money.amount != 0 #if money.amount == 0 we don't even bother creating a new Money instance
8
+ return Money.new(@amount + money.amount, @currency)
9
+ end
10
+ end
11
+
12
+ def -(money)
13
+ if money.currency != @currency && money.amount != 0
14
+ return Money.new(@amount - (money.convert_to(@currency)).amount, @currency)
15
+ elsif money.amount != 0 #if money.amount == 0 we don't even bother creating a new Money instance
16
+ return Money.new(@amount - money.amount, @currency)
17
+ end
18
+ end
19
+
20
+ def *(number)
21
+ raise ArgumentError, 'Number should be a non nil numeric' unless number.is_a?(Numeric) and number != 0
22
+ return Money.new((@amount * number).round(2), @currency)
23
+ end
24
+
25
+ def /(number)
26
+ raise ArgumentError, 'Number should be a non nil numeric' unless number.is_a?(Numeric) and number != 0
27
+ return Money.new((@amount / number).round(2), @currency)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module Money_DaWanda
2
+ module Comparisons
3
+
4
+ [:<, :>, :==].each do |comparator|
5
+ define_method(comparator) do |money|
6
+ if money.currency != @currency && money.amount != 0
7
+ money = (money.convert_to(@currency))
8
+ end
9
+ return @amount.send(comparator, money.amount)
10
+ end
11
+ end
12
+
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Money_DaWanda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cesar Bourdon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby gem to perform currency conversion and arithmetics with different
14
+ currencies
15
+ email: cesar.bourdon1@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/Money_DaWanda.rb
21
+ - lib/Money_DaWanda/arithmetics.rb
22
+ - lib/Money_DaWanda/comparisons.rb
23
+ homepage: http://rubygems.org/gems/Money_DaWanda
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Currency conversion tool
47
+ test_files: []