latinum 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/latinum/resource.rb +28 -2
- data/lib/latinum/version.rb +1 -1
- data/spec/latinum/resource_spec.rb +24 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c23ebdb9df8ce74aef6217b5b5890eb3b45a721f
|
4
|
+
data.tar.gz: 89ceffa2c42fe78bcd7e001658f14a54a5a0a627
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 297be132095c971412a09434c5f2ddfa109f081891c5211e648eb3380c593ccc79cf65d0a589c1089abb76c2b3cbdf9360205e2d719d608dd2a5135d9ccfdd65
|
7
|
+
data.tar.gz: 08f5d7925c6168e22b908a641775ec06e3d538ab8fba94774afefba43feb037b863cd5d07f21101b853252c586c50cacd280d8dc8323179d64db714a9f1aa933
|
data/README.md
CHANGED
@@ -48,7 +48,7 @@ But, you can't add resources of different names together:
|
|
48
48
|
> twenty = Latinum::Resource.new("20.00", "AUD")
|
49
49
|
=> 20.0 AUD
|
50
50
|
> ten + twenty
|
51
|
-
|
51
|
+
DifferentResourceNameError: Cannot operate on different currencies!
|
52
52
|
|
53
53
|
To add multiple currencies together, use a collection:
|
54
54
|
|
data/lib/latinum/resource.rb
CHANGED
@@ -22,6 +22,12 @@ require 'bigdecimal'
|
|
22
22
|
require 'bigdecimal/util'
|
23
23
|
|
24
24
|
module Latinum
|
25
|
+
class DifferentResourceNameError < ArgumentError
|
26
|
+
def initialize
|
27
|
+
super "Cannot operate on different currencies!"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
25
31
|
# A fixed unit in a given named resource
|
26
32
|
class Resource
|
27
33
|
include Comparable
|
@@ -36,13 +42,13 @@ module Latinum
|
|
36
42
|
|
37
43
|
# By default, we can only add and subtract if the name is the same
|
38
44
|
def + other
|
39
|
-
raise
|
45
|
+
raise DifferentResourceNameError if @name != other.name
|
40
46
|
|
41
47
|
self.class.new(@amount + other.amount, @name)
|
42
48
|
end
|
43
49
|
|
44
50
|
def - other
|
45
|
-
raise
|
51
|
+
raise DifferentResourceNameError if @name != other.name
|
46
52
|
|
47
53
|
self.class.new(@amount - other.amount, @name)
|
48
54
|
end
|
@@ -55,7 +61,19 @@ module Latinum
|
|
55
61
|
self.class.new(@amount * factor, @name)
|
56
62
|
end
|
57
63
|
|
64
|
+
def / factor
|
65
|
+
if factor.is_a? self.class
|
66
|
+
raise DifferentResourceNameError if @name != factor.name
|
67
|
+
|
68
|
+
@amount / factor.amount
|
69
|
+
else
|
70
|
+
self.class.new(@amount / factor, @name)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
58
74
|
def exchange(rate, name, precision = nil)
|
75
|
+
return self if @name == name
|
76
|
+
|
59
77
|
exchanged_amount = @amount * rate
|
60
78
|
|
61
79
|
exchanged_amount = exchanged_amount.round(precision) if precision
|
@@ -79,6 +97,14 @@ module Latinum
|
|
79
97
|
end
|
80
98
|
end
|
81
99
|
|
100
|
+
def hash
|
101
|
+
[@amount, @name].hash
|
102
|
+
end
|
103
|
+
|
104
|
+
def eql? other
|
105
|
+
self.class.eql? other.class and @name.eql? other.name and @amount.eql? other.amount
|
106
|
+
end
|
107
|
+
|
82
108
|
class << self
|
83
109
|
def parse(string, default_name: nil)
|
84
110
|
amount, name = string.split(/\s+/, 2)
|
data/lib/latinum/version.rb
CHANGED
@@ -48,5 +48,29 @@ module Latinum::ResourceSpec
|
|
48
48
|
|
49
49
|
expect(resource.inspect).to be == '#<Latinum::Resource "10.0 NZD">'
|
50
50
|
end
|
51
|
+
|
52
|
+
it "should compute percentage difference" do
|
53
|
+
original_price = Latinum::Resource.load("10 NZD")
|
54
|
+
discount_price = Latinum::Resource.load("5 NZD")
|
55
|
+
|
56
|
+
discount = (original_price - discount_price) / original_price
|
57
|
+
|
58
|
+
expect(discount).to be == 0.5
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not divide" do
|
62
|
+
original_price = Latinum::Resource.load("10 NZD")
|
63
|
+
discount_price = Latinum::Resource.load("5 USD")
|
64
|
+
|
65
|
+
expect{original_price / discount_price}.to raise_exception(Latinum::DifferentResourceNameError)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should compute quotient" do
|
69
|
+
original_price = Latinum::Resource.load("10 NZD")
|
70
|
+
|
71
|
+
quotient = original_price / 2.0
|
72
|
+
|
73
|
+
expect(original_price / 2.0).to be == Latinum::Resource.load("5 NZD")
|
74
|
+
end
|
51
75
|
end
|
52
76
|
end
|