latinum 0.5.4 → 0.5.5

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: ebfa6ade645c706ea5fc877d0a2cdc386c477e2f
4
- data.tar.gz: e35cf37d0744a36bc805e5e8fba62c4e723e49b9
3
+ metadata.gz: c23ebdb9df8ce74aef6217b5b5890eb3b45a721f
4
+ data.tar.gz: 89ceffa2c42fe78bcd7e001658f14a54a5a0a627
5
5
  SHA512:
6
- metadata.gz: 56f9fc756c232955e17a5c387c4a89ffede5c9a6a415315eab19645b41568583a859b006a40b30bd485efa403e430711e1dbdd5be2118ee61fa1da75d75d176d
7
- data.tar.gz: a8764c151ca5831afd4e9ef72aa677f95547aca5d4d36bdb116a825ec3e4399c473e4148325d581ef68d3840f6842d78e17c4b45abfb8c6d56da6de29c3f1c6c
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
- ArgumentError: Cannot operate on different currencies!
51
+ DifferentResourceNameError: Cannot operate on different currencies!
52
52
 
53
53
  To add multiple currencies together, use a collection:
54
54
 
@@ -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 ArgumentError.new("Cannot operate on different currencies!") if @name != other.name
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 ArgumentError.new("Cannot operate on different currencies!") if @name != other.name
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)
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Latinum
22
- VERSION = "0.5.4"
22
+ VERSION = "0.5.5"
23
23
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: latinum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams