currency_shushugah 1.0.1 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 460d9b74715a54631d64b673616bbbb03e4cb8c7
4
- data.tar.gz: a9cef6316c80c3bbc122f9bc569998170ee4e432
3
+ metadata.gz: 025a361a07ad5469e9f3b19f285c1882d4d2b510
4
+ data.tar.gz: b0a579adf6581e8926192437148bf965d0e400a3
5
5
  SHA512:
6
- metadata.gz: 5a5f9027b8d08eceb31d0903fc291ce7d0fc9c4f8f631070ac12939cf582024c0c7d59d1615e465e7a671cf465cb76c120df13a7f7076170fb4b1942c7bfe40b
7
- data.tar.gz: bed757796f2c981c17a6ed0f8cf65fda43b9c0fb73400beccff135ba9c47d0fd59e97ed4f721e7ce1a633ff8887f618cf70dbacc827a6f6924112b2511990885
6
+ metadata.gz: c3c2f0ecb2deb87d8a6abc4910b6d20bb04c38a45a0d0cd6e9d1b19db6ee22b527b8f01b34574460f4dc7c4d6008cb2c7230e30a13c850338e867b3a00b64be2
7
+ data.tar.gz: 76e6665f5ee08b7f089aae84707d4f5079fa7cb73ecd2595d435386df310d500457f4ec31c0b77bd68c751b3d682622a50438b96f16564653b622e59d0948a10
@@ -6,10 +6,10 @@ require 'currency_shushugah/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "currency_shushugah"
8
8
  spec.version = CurrencyShushugah::VERSION
9
- spec.authors = ["yonatan miller"]
9
+ spec.authors = ["Yonatan Miller"]
10
10
  spec.email = ["brombacher1@gmail.com"]
11
11
 
12
- spec.summary = "Converts currencies after being fed a conversion rate, either manually or from an API"
12
+ spec.summary = "Converts currencies and accepts conversion rates"
13
13
  spec.homepage = "https://github.com/shushugah/currency_shushugah"
14
14
  spec.license = "MIT"
15
15
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "bundler", "~> 1.12"
20
+ spec.add_development_dependency "bundler", "~> 1.15"
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
22
  spec.add_development_dependency "rspec", "~> 3.0"
23
23
  end
@@ -1,3 +1,3 @@
1
1
  class CurrencyShushugah
2
- VERSION = "1.0.1"
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -1,55 +1,74 @@
1
- require "currency_shushugah/version"
1
+ require 'currency_shushugah/version'
2
2
 
3
+ # Understands currency and quantity
3
4
  class Money
4
- attr_accessor :currency, :amount
5
- @@currencies = {}
6
- @@base_currency = ""
7
-
8
- def self.conversion_rates(currency, hash)
9
- @@currencies[currency] = hash
10
- @@base_currency = currency
11
- end
12
-
13
- def initialize(amount, currency)
14
- @amount = amount.round(2)
15
- @currency = currency
16
- end
17
-
18
- def convert_to(currency_code)
19
- return self if currency == currency_code
20
- if @@currencies[currency] && @@currencies[currency].has_key?(currency_code)
21
- Money.new(@@currencies[currency][currency_code]*amount, currency_code)
22
- else
23
- Money.new(amount/@@currencies[currency_code][currency], currency_code)
24
- end
25
- end
26
-
27
- def inspect
28
- "#{'%0.2f' % @amount} #{@currency}"
29
- end
30
-
31
- [:+, :-].each do |method_name|
32
- define_method method_name do |arg|
33
- if currency == arg.currency
34
- normalized_amount, normalized_arg_amount, currency_param = amount, arg.amount, currency
35
- elsif [currency, arg.currency].include?(@@base_currency)
36
- normalized_amount, normalized_arg_amount, currency_param = self.convert_to(@@base_currency).amount, arg.convert_to(@@base_currency).amount, @@base_currency
37
- else
38
- normalized_arg_amount, currency_param = arg.convert_to(currency).amount, currency
39
- end
40
- Money.new(normalized_amount.send(method_name, normalized_arg_amount), currency_param)
41
- end
42
- end
43
-
44
- [:*, :/].each do |method_name|
45
- define_method method_name do |arg|
46
- Money.new(amount.send(method_name, arg), currency)
47
- end
48
- end
49
-
50
- [:>,:<,:==].each do |method_name|
51
- define_method method_name do |arg|
52
- amount.send(method_name, arg.convert_to(currency).amount)
53
- end
5
+ include Comparable
6
+
7
+ attr_reader :quantity, :currency
8
+ protected :currency, :quantity
9
+
10
+ def self.conversion_rates(base_currency, hash_rates)
11
+ @rates = hash_rates.merge(base_currency => 1)
12
+ end
13
+
14
+ def initialize(quantity, currency)
15
+ raise StandardError.new('Not supported currency') unless rates[currency]
16
+ @quantity = quantity
17
+ @currency = currency
18
+ end
19
+
20
+ def <=>(other)
21
+ return unless other.class == self.class
22
+ quantity <=> other.convert_to(currency).quantity
23
+ end
24
+
25
+ def inspect
26
+ "#{'%0.2f' % quantity} #{currency}"
27
+ end
28
+
29
+ def convert_to(other_currency)
30
+ raise StandardError.new('InvalidCurrency') unless rates[other_currency]
31
+ return self if currency == other_currency
32
+ new_quantity = @quantity * rates[other_currency] / rates[currency]
33
+ new_money(new_quantity, other_currency)
34
+ end
35
+
36
+ def +(other)
37
+ new_money(quantity + other.convert_to(currency).quantity)
38
+ end
39
+
40
+ def -(other)
41
+ self + -other
42
+ end
43
+
44
+ def -@
45
+ new_money(-quantity)
46
+ end
47
+
48
+ def coerce(other)
49
+ return self, other
50
+ end
51
+
52
+ def *(scalar)
53
+ new_money(quantity * scalar)
54
+ end
55
+
56
+ def /(scalar)
57
+ raise StandardError.new 'InvalidDivision' if scalar.zero?
58
+ self * (1.0 / scalar)
59
+ end
60
+
61
+ def new_money(quantity, currency = @currency)
62
+ Money.send :new, quantity.round(2), currency
63
+ end
64
+
65
+ def self.rates
66
+ Money.instance_variable_get(:@rates)
67
+ end
68
+
69
+ private
70
+
71
+ def rates
72
+ self.class.rates
54
73
  end
55
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency_shushugah
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - yonatan miller
7
+ - Yonatan Miller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-11 00:00:00.000000000 Z
11
+ date: 2017-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.12'
19
+ version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.12'
26
+ version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -98,6 +98,5 @@ rubyforge_project:
98
98
  rubygems_version: 2.5.1
99
99
  signing_key:
100
100
  specification_version: 4
101
- summary: Converts currencies after being fed a conversion rate, either manually or
102
- from an API
101
+ summary: Converts currencies and accepts conversion rates
103
102
  test_files: []