converrency 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b4c924c56f3c0fa66d6b4b493c66a0e4d47b914
4
- data.tar.gz: 5c8059c85738b284b52bffd47df2051857f69c69
3
+ metadata.gz: 401fc5169ee4a8ea836d6c95d223174fa13f07f7
4
+ data.tar.gz: 04aa5fbfdebddedc811fc15cd74a301ad0187845
5
5
  SHA512:
6
- metadata.gz: d6f0b1614f89249c3662a81d872bda736b75928e52ac45fc108593b3fbfe1c537258a9f57de6573275d3c93226c05ddcbe6e90c9b2d4fe397341abb646cbec75
7
- data.tar.gz: de07b6236819ce36de4237387eab8539b19f44d75f7e6e924da8817be4ff2e72e108687186c0d34d892e5da4653a61b78d7e8d7bedfe8e643f710b228d1f9172
6
+ metadata.gz: 922dc3fb76649bcc5748dfc73ffe85bb45e0da5ce6ec6709627ba382dec3d3d20222ad7c804b7504449918a4f2e9d8f4e0c47d0f5619878473164002e30999f6
7
+ data.tar.gz: 6ef621411b8a0d2255fdc0560aa855409bf8f7ca8ce3c0551dee9773106adfda5a7119c65090d501d881cb8555c1c8e9ce31a74f200ee7113bcfc13e050abea0
data/lib/converrency.rb CHANGED
@@ -1,4 +1 @@
1
- require 'lib/money'
2
-
3
- module Converrency
4
- end
1
+ require 'converrency/money'
@@ -0,0 +1,72 @@
1
+ module Converrency
2
+ class Money
3
+
4
+ attr_reader :currency, :amount
5
+
6
+ class << self
7
+ attr_reader :base_currency, :currencies
8
+
9
+ def currency_rates(base_currency, currencies)
10
+ @base_currency = base_currency
11
+ @currencies = currencies.merge(base_currency => 1.0)
12
+ end
13
+
14
+ def valid_currency?(currency)
15
+ base_currency == currency || currencies.has_key?(currency)
16
+ end
17
+ end
18
+
19
+ def initialize(amount, currency)
20
+ unless self.class.valid_currency?(currency)
21
+ raise ArgumentError.new('Invalid currency')
22
+ end
23
+
24
+ @amount = amount
25
+ @currency = currency
26
+ end
27
+
28
+ def inspect
29
+ "#{print_amount} #{currency}"
30
+ end
31
+
32
+ def convert_to(new_currency)
33
+ converted_amount = value_in_base_currency * currencies[new_currency]
34
+ Money.new(converted_amount, new_currency)
35
+ end
36
+
37
+ [:+, :-, :*, :/].each do |meth|
38
+ define_method(meth) do |money|
39
+ new_amount = amount.send(meth, money.convert_to(currency).amount)
40
+ Money.new(new_amount, currency)
41
+ end
42
+ end
43
+
44
+ [:==, :<, :>, :<=, :>=].each do |meth|
45
+ define_method(meth) do |money|
46
+ rounded_amount.send(meth, money.convert_to(currency).rounded_amount)
47
+ end
48
+ end
49
+
50
+ def rounded_amount
51
+ amount.round(2)
52
+ end
53
+
54
+ private
55
+
56
+ def print_amount
57
+ "%.2f" % rounded_amount
58
+ end
59
+
60
+ def value_in_base_currency
61
+ amount / currencies[currency]
62
+ end
63
+
64
+ def base_currency
65
+ self.class.base_currency
66
+ end
67
+
68
+ def currencies
69
+ self.class.currencies
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: converrency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Raife
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-24 00:00:00.000000000 Z
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/converrency.rb
20
+ - lib/converrency/money.rb
20
21
  homepage:
21
22
  licenses:
22
23
  - MIT