latinum 1.3.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile DELETED
@@ -1,14 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec) do |task|
5
- if ENV['COVERAGE']
6
- begin
7
- require('simplecov/version')
8
- task.rspec_opts = %w{--require simplecov}
9
- rescue LoadError
10
- end
11
- end
12
- end
13
-
14
- task :default => :spec
@@ -1,23 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'latinum/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "latinum"
8
- spec.version = Latinum::VERSION
9
- spec.authors = ["Samuel Williams"]
10
- spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
- spec.summary = %q{Latinum is a simple gem for managing resource computations, including money and minerals.}
12
- spec.homepage = "https://github.com/ioquatix/latinum"
13
- spec.license = "MIT"
14
-
15
- spec.files = `git ls-files`.split($/)
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_development_dependency "bundler", "~> 1.3"
21
- spec.add_development_dependency "rspec", "~> 3.4.0"
22
- spec.add_development_dependency "rake"
23
- end
@@ -1,83 +0,0 @@
1
- # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'latinum'
22
- require 'latinum/currencies/global'
23
-
24
- RSpec.describe Latinum::Bank do
25
- before(:all) do
26
- @bank = Latinum::Bank.new(Latinum::Currencies::Global)
27
-
28
- @bank << Latinum::ExchangeRate.new("NZD", "AUD", "0.5")
29
- end
30
-
31
- it "should format the amounts correctly" do
32
- resource = Latinum::Resource.new("10", "NZD")
33
-
34
- expect(@bank.format(resource)).to be == "$10.00 NZD"
35
- expect(@bank.format(resource, name: nil)).to be == "$10.00"
36
-
37
- resource = Latinum::Resource.new("391", "AUD")
38
- expect(@bank.format(resource)).to be == "$391.00 AUD"
39
-
40
- resource = Latinum::Resource.new("-100", "NZD")
41
- expect(@bank.format(resource)).to be == "-$100.00 NZD"
42
-
43
- resource = Latinum::Resource.new("1.12345678", "BTC")
44
- expect(@bank.format(resource)).to be == "B⃦1.12345678 BTC"
45
- end
46
-
47
- it "should round up using correct precision" do
48
- resource = Latinum::Resource.new("19.9989", "NZD")
49
-
50
- expect(@bank.round(resource)).to be == Latinum::Resource.new(20, "NZD")
51
- end
52
-
53
- it "should round down using correct precision" do
54
- resource = Latinum::Resource.new("19.991", "NZD")
55
-
56
- expect(@bank.round(resource)).to be == Latinum::Resource.new("19.99", "NZD")
57
- end
58
-
59
- it "should round values when formatting" do
60
- resource = Latinum::Resource.new("19.9989", "NZD")
61
-
62
- expect(@bank.format(resource)).to be == "$20.00 NZD"
63
- end
64
-
65
- it "should exchange currencies from NZD to AUD" do
66
- nzd = Latinum::Resource.new("10", "NZD")
67
-
68
- aud = @bank.exchange nzd, "AUD"
69
- expect(aud).to be == Latinum::Resource.new("5", "AUD")
70
- end
71
-
72
- it "should parser strings into resources" do
73
- expect(@bank.parse("$5")).to be == Latinum::Resource.new("5", "USD")
74
- expect(@bank.parse("$5 NZD")).to be == Latinum::Resource.new("5", "NZD")
75
- expect(@bank.parse("€5")).to be == Latinum::Resource.new("5", "EUR")
76
-
77
- expect(@bank.parse("5 NZD")).to be == Latinum::Resource.new("5", "NZD")
78
- end
79
-
80
- it "should fail to parse unknown resource" do
81
- expect{@bank.parse("B5")}.to raise_error(ArgumentError)
82
- end
83
- end
@@ -1,104 +0,0 @@
1
- # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'latinum'
22
- require 'latinum/currencies/global'
23
-
24
- require 'set'
25
-
26
- RSpec.describe Latinum::Collection do
27
- it "can set an initial value" do
28
- subject["NZD"] = BigDecimal.new("20")
29
-
30
- expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
31
- end
32
-
33
- it "can be negated" do
34
- subject["NZD"] = BigDecimal.new("20")
35
-
36
- negated = -subject
37
-
38
- expect(negated["NZD"]).to be == BigDecimal.new("-20")
39
- end
40
-
41
- it "should sum up currencies correctly" do
42
- resource = Latinum::Resource.new("10", "NZD")
43
-
44
- subject << resource
45
- expect(subject["NZD"]).to be == resource
46
-
47
- subject << resource
48
- expect(subject["NZD"]).to be == (resource * 2)
49
- end
50
-
51
- it "should sum up multiple currencies correctly" do
52
- resources = [
53
- Latinum::Resource.new("10", "NZD"),
54
- Latinum::Resource.new("10", "AUD"),
55
- Latinum::Resource.new("10", "USD"),
56
- Latinum::Resource.new("10", "NZD"),
57
- Latinum::Resource.new("10", "AUD"),
58
- Latinum::Resource.new("10", "USD"),
59
- ]
60
-
61
- subject = Latinum::Collection.new
62
- subject << resources
63
-
64
- expect(subject["NZD"]).to be == (resources[0] * 2)
65
- expect(subject.names).to be == Set.new(["NZD", "AUD", "USD"])
66
- end
67
-
68
- it "can add two collections together" do
69
- other_resources = [
70
- Latinum::Resource.new("10", "NZD"),
71
- Latinum::Resource.new("10", "AUD"),
72
- Latinum::Resource.new("10", "USD"),
73
- ]
74
-
75
- other_collection = Latinum::Collection.new
76
- other_collection << other_resources
77
-
78
- resources = [
79
- Latinum::Resource.new("10", "NZD"),
80
- Latinum::Resource.new("10", "AUD"),
81
- Latinum::Resource.new("10", "USD"),
82
- ]
83
-
84
- subject << resources
85
- subject << other_collection
86
-
87
- expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
88
- expect(subject["AUD"]).to be == Latinum::Resource.load("20 AUD")
89
- expect(subject["USD"]).to be == Latinum::Resource.load("20 USD")
90
- end
91
-
92
- it "can enumerate resources" do
93
- resources = [
94
- Latinum::Resource.new("10", "NZD"),
95
- Latinum::Resource.new("10", "AUD"),
96
- Latinum::Resource.new("10", "USD"),
97
- ]
98
-
99
- collection = Latinum::Collection.new
100
- collection << resources
101
-
102
- expect(collection.each.to_a).to be == resources
103
- end
104
- end
@@ -1,93 +0,0 @@
1
- # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'latinum/bank'
22
- require 'latinum/currencies/global'
23
- require 'latinum/formatters'
24
-
25
- RSpec.describe Latinum::Formatters::PlainFormatter.new(name: "NZD") do
26
- let(:amount) {BigDecimal.new(10)}
27
-
28
- it "can convert to integral" do
29
- expect(subject.to_integral(amount)).to be == 10
30
- end
31
-
32
- it "can convert from integral" do
33
- expect(subject.from_integral(10)).to be == amount
34
- end
35
-
36
- it "can do basic formatting" do
37
- expect(subject.format(amount)).to be == "10.0 NZD"
38
- end
39
- end
40
-
41
- RSpec.describe Latinum::Formatters::DecimalCurrencyFormatter do
42
- before(:all) do
43
- @bank = Latinum::Bank.new
44
- @bank.import(Latinum::Currencies::Global)
45
- end
46
-
47
- let(:resource) {Latinum::Resource.load("10 NZD")}
48
-
49
- it "should format output" do
50
- expect(@bank.format(resource)).to be == "$10.00 NZD"
51
- end
52
-
53
- it "should format output without name" do
54
- expect(@bank.format(resource, name: nil)).to be == "$10.00"
55
- end
56
-
57
- it "should format output with alternative name" do
58
- expect(@bank.format(resource, name: "Foo")).to be == "$10.00 Foo"
59
- end
60
-
61
- it "should format output" do
62
- expect(@bank.format(resource)).to be == "$10.00 NZD"
63
- end
64
-
65
- it "should format output without symbol" do
66
- expect(@bank.format(resource, symbol: nil)).to be == "10.00 NZD"
67
- end
68
-
69
- it "should format output with alternative symbol" do
70
- expect(@bank.format(resource, symbol: "!!")).to be == "!!10.00 NZD"
71
- end
72
-
73
- context "negative zero" do
74
- let(:resource) {Latinum::Resource.new(BigDecimal.new("-0"), "NZD")}
75
-
76
- it "should format as (positve) zero" do
77
- expect(@bank.format(resource)).to be == "$0.00 NZD"
78
- end
79
- end
80
- end
81
-
82
- RSpec.describe Latinum::Formatters::DecimalCurrencyFormatter do
83
- before(:all) do
84
- @bank = Latinum::Bank.new
85
- @bank.import(Latinum::Currencies::Global)
86
- end
87
-
88
- let(:resource) {Latinum::Resource.load("10 JPY")}
89
-
90
- it "should format without separator or fractional part" do
91
- expect(@bank.format(resource)).to be == "¥10 JPY"
92
- end
93
- end
@@ -1,45 +0,0 @@
1
- # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'latinum'
22
- require 'latinum/currencies/global'
23
-
24
- RSpec.describe Latinum::Bank do
25
- before(:all) do
26
- @bank = Latinum::Bank.new
27
- @bank.import(Latinum::Currencies::Global)
28
- end
29
-
30
- it "should convert to NZD integral value" do
31
- resource = Latinum::Resource.new("10", "NZD")
32
-
33
- expect(@bank.to_integral(resource)).to be == 1000
34
-
35
- expect(@bank.from_integral(1000, "NZD")).to be == resource
36
- end
37
-
38
- it "should convert to BTC integral value" do
39
- resource = Latinum::Resource.new("1.12345678", "BTC")
40
-
41
- expect(@bank.to_integral(resource)).to be == 112345678
42
-
43
- expect(@bank.from_integral(112345678, "BTC")).to be == resource
44
- end
45
- end
@@ -1,130 +0,0 @@
1
- # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'latinum/resource'
22
-
23
- RSpec.describe Latinum::Resource do
24
- it "should load and dump resources" do
25
- resource = Latinum::Resource.load("10 NZD")
26
- string_representation = Latinum::Resource.dump(resource)
27
-
28
- loaded_resource = Latinum::Resource.load(string_representation)
29
-
30
- expect(loaded_resource).to be == loaded_resource
31
- end
32
-
33
- it "should load and dump nil correctly" do
34
- expect(Latinum::Resource.load(nil)).to be nil
35
- expect(Latinum::Resource.dump(nil)).to be nil
36
- end
37
-
38
- it "should handle empty strings correctly" do
39
- expect(Latinum::Resource.load("")).to be nil
40
- end
41
-
42
- it "should handle whitespace strings correctly" do
43
- expect(Latinum::Resource.load(" ")).to be nil
44
- end
45
-
46
- it "should load and dump resources correctly" do
47
- resource = Latinum::Resource.new(10, 'NZD')
48
-
49
- expect(Latinum::Resource.load("10.0 NZD")).to be == resource
50
- expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
51
- end
52
-
53
- it "should inspect nicely" do
54
- resource = Latinum::Resource.load("10 NZD")
55
-
56
- expect(resource.inspect).to be == '#<Latinum::Resource "10.0 NZD">'
57
- end
58
-
59
- it "should compute percentage difference" do
60
- original_price = Latinum::Resource.load("10 NZD")
61
- discount_price = Latinum::Resource.load("5 NZD")
62
-
63
- discount = (original_price - discount_price) / original_price
64
-
65
- expect(discount).to be == 0.5
66
- end
67
-
68
- it "should not divide" do
69
- original_price = Latinum::Resource.load("10 NZD")
70
- discount_price = Latinum::Resource.load("5 USD")
71
-
72
- expect{original_price / discount_price}.to raise_exception(Latinum::DifferentResourceNameError)
73
- end
74
-
75
- it "should compute quotient" do
76
- original_price = Latinum::Resource.load("10 NZD")
77
-
78
- expect(original_price / 2.0).to be == Latinum::Resource.load("5 NZD")
79
- end
80
-
81
- it "should add two resources of the same symbol" do
82
- a = Latinum::Resource.load("10 NZD")
83
- b = Latinum::Resource.load("5 NZD")
84
- c = Latinum::Resource.load("15 NZD")
85
-
86
- expect(a+b).to be == c
87
- end
88
-
89
- it "should fail two resources of different symbol" do
90
- a = Latinum::Resource.load("10 NZD")
91
- b = Latinum::Resource.load("5 USD")
92
-
93
- expect{a+b}.to raise_error(Latinum::DifferentResourceNameError)
94
- end
95
-
96
- it "should be able to negate a value" do
97
- a = Latinum::Resource.load("10 NZD")
98
- b = Latinum::Resource.load("-10 NZD")
99
-
100
- expect(-a).to be == b
101
- end
102
-
103
- it "can be used as a hash key" do
104
- a = Latinum::Resource.load("10 NZD")
105
- b = Latinum::Resource.load("0 NZD")
106
-
107
- hash = {a => true}
108
-
109
- expect(hash).to be_include a
110
- expect(hash).to_not be_include b
111
- end
112
-
113
- it "can be zero" do
114
- a = Latinum::Resource.load("10 NZD")
115
- b = Latinum::Resource.load("0 NZD")
116
-
117
- expect(a).to_not be_zero
118
- expect(b).to be_zero
119
- end
120
-
121
- it "can be eql?" do
122
- a = Latinum::Resource.load("10 NZD")
123
- b = Latinum::Resource.load("0 NZD")
124
- c = Latinum::Resource.load("0 NZD")
125
-
126
- expect(a).to be_eql a
127
- expect(a).to_not be_eql b
128
- expect(b).to be_eql c
129
- end
130
- end