latinum 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/latinum.gemspec +1 -0
- data/lib/latinum.rb +0 -4
- data/lib/latinum/formatters.rb +3 -0
- data/lib/latinum/version.rb +1 -1
- data/spec/latinum/bank_spec.rb +72 -0
- data/{test/test_collection.rb → spec/latinum/collection_spec.rb} +34 -34
- data/{test/test_integrals.rb → spec/latinum/integrals_spec.rb} +20 -19
- metadata +30 -18
- data/lib/latinum/extensions/bigdecimal-1.8.rb +0 -6
- data/test/test_bank.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 597ddf3035b21a9c9ca7cc3ab036331db974903c
|
4
|
+
data.tar.gz: 2171d764b3c41e0ca975854397d8644a10d5e6d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd414c78914a6cd7144e4e30e90657e6cb1523662c0c9ec510e363127a47b2e18ced5c29190685260b07f0db7b69b2183caecdf41729e43cfbfb430bb02aed1f
|
7
|
+
data.tar.gz: ba272f4680a3c8420649f364901887d47380776b864416bfdb92d3caeb86a01f502653e408ce693cd788fe6fa70a0c8c68fe418ef49c924f39f9a805d58b1353
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
Latinum is a library for resource and currency calculations. It provides immutable `Resource` objects for dealing with quantities of named resources with an arbitrary number of decimal places, and `Bank` objects for converting resources and formatting them for output. Latinum doesn't include any global state by default and thus is ideal for integration with other frameworks/libraries.
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/ioquatix/latinum.svg?branch=master)](https://travis-ci.org/ioquatix/latinum)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ioquatix/latinum.png)](https://codeclimate.com/github/ioquatix/latinum)
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -117,7 +118,7 @@ As BitCoin has 8 decimal places, it requires an integer representation with at l
|
|
117
118
|
|
118
119
|
## License
|
119
120
|
|
120
|
-
Copyright, 2011, 2012, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
121
|
+
Copyright, 2011, 2012, 2014, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
121
122
|
|
122
123
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
123
124
|
of this software and associated documentation files (the "Software"), to deal
|
data/latinum.gemspec
CHANGED
data/lib/latinum.rb
CHANGED
data/lib/latinum/formatters.rb
CHANGED
@@ -54,6 +54,9 @@ module Latinum
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def format(amount, options = DEFAULT_OPTIONS)
|
57
|
+
# Round to the desired number of places. Truncation used to be the default.
|
58
|
+
amount = amount.round(@places)
|
59
|
+
|
57
60
|
fix, frac = amount.abs.to_s('F').split(/\./, 2)
|
58
61
|
|
59
62
|
# The sign of the number
|
data/lib/latinum/version.rb
CHANGED
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'latinum'
|
24
|
+
require 'latinum/currencies/global'
|
25
|
+
|
26
|
+
module Latinum::BankSpec
|
27
|
+
describe Latinum::Bank do
|
28
|
+
before(:all) do
|
29
|
+
@bank = Latinum::Bank.new
|
30
|
+
@bank.import(Latinum::Currencies::Global)
|
31
|
+
|
32
|
+
@bank << Latinum::ExchangeRate.new("NZD", "AUD", "0.5")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should format the amounts correctly" do
|
36
|
+
resource = Latinum::Resource.new("10", "NZD")
|
37
|
+
|
38
|
+
expect(@bank.format(resource, :format => :full)).to be == "$10.00 NZD"
|
39
|
+
expect(@bank.format(resource, :format => :compact)).to be == "$10.00"
|
40
|
+
|
41
|
+
resource = Latinum::Resource.new("391", "AUD")
|
42
|
+
expect(@bank.format(resource)).to be == "$391.00 AUD"
|
43
|
+
|
44
|
+
resource = Latinum::Resource.new("-100", "NZD")
|
45
|
+
expect(@bank.format(resource)).to be == "-$100.00 NZD"
|
46
|
+
|
47
|
+
resource = Latinum::Resource.new("1.12345678", "BTC")
|
48
|
+
expect(@bank.format(resource)).to be == "B⃦1.12345678 BTC"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should round values when formatting" do
|
52
|
+
resource = Latinum::Resource.new("19.9989", "NZD")
|
53
|
+
|
54
|
+
expect(@bank.format(resource, :format => :full)).to be == "$20.00 NZD"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should exchange currencies from NZD to AUD" do
|
58
|
+
nzd = Latinum::Resource.new("10", "NZD")
|
59
|
+
|
60
|
+
aud = @bank.exchange nzd, "AUD"
|
61
|
+
expect(aud).to be == Latinum::Resource.new("5", "AUD")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should parser strings into resources" do
|
65
|
+
expect(@bank.parse("$5")).to be == Latinum::Resource.new("5", "USD")
|
66
|
+
expect(@bank.parse("$5 NZD")).to be == Latinum::Resource.new("5", "NZD")
|
67
|
+
expect(@bank.parse("€5")).to be == Latinum::Resource.new("5", "EUR")
|
68
|
+
|
69
|
+
expect(@bank.parse("5 NZD")).to be == Latinum::Resource.new("5", "NZD")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -18,46 +18,46 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
require 'test/unit'
|
22
|
-
|
23
21
|
require 'latinum'
|
24
22
|
require 'latinum/currencies/global'
|
25
23
|
|
26
24
|
require 'set'
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
def test_collections
|
35
|
-
resource = Latinum::Resource.new("10", "NZD")
|
36
|
-
|
37
|
-
currencies = Set.new
|
38
|
-
collection = Latinum::Collection.new(currencies)
|
39
|
-
|
40
|
-
collection << resource
|
41
|
-
assert_equal resource, collection["NZD"]
|
42
|
-
|
43
|
-
collection << resource
|
44
|
-
assert_equal resource * 2, collection["NZD"]
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_additions
|
48
|
-
resources = [
|
49
|
-
Latinum::Resource.new("10", "NZD"),
|
50
|
-
Latinum::Resource.new("10", "AUD"),
|
51
|
-
Latinum::Resource.new("10", "USD"),
|
52
|
-
Latinum::Resource.new("10", "NZD"),
|
53
|
-
Latinum::Resource.new("10", "AUD"),
|
54
|
-
Latinum::Resource.new("10", "USD")
|
55
|
-
]
|
26
|
+
module Latinum::CollectionSpec
|
27
|
+
describe Latinum::Bank do
|
28
|
+
before(:all) do
|
29
|
+
@bank = Latinum::Bank.new
|
30
|
+
@bank.import(Latinum::Currencies::Global)
|
31
|
+
end
|
56
32
|
|
57
|
-
|
58
|
-
|
33
|
+
it "should sum up currencies correctly" do
|
34
|
+
resource = Latinum::Resource.new("10", "NZD")
|
35
|
+
|
36
|
+
currencies = Set.new
|
37
|
+
collection = Latinum::Collection.new(currencies)
|
38
|
+
|
39
|
+
collection << resource
|
40
|
+
expect(collection["NZD"]).to be == resource
|
41
|
+
|
42
|
+
collection << resource
|
43
|
+
expect(collection["NZD"]).to be == (resource * 2)
|
44
|
+
end
|
59
45
|
|
60
|
-
|
61
|
-
|
46
|
+
it "should sum up multiple currencies correctly" do
|
47
|
+
resources = [
|
48
|
+
Latinum::Resource.new("10", "NZD"),
|
49
|
+
Latinum::Resource.new("10", "AUD"),
|
50
|
+
Latinum::Resource.new("10", "USD"),
|
51
|
+
Latinum::Resource.new("10", "NZD"),
|
52
|
+
Latinum::Resource.new("10", "AUD"),
|
53
|
+
Latinum::Resource.new("10", "USD")
|
54
|
+
]
|
55
|
+
|
56
|
+
collection = Latinum::Collection.new
|
57
|
+
collection << resources
|
58
|
+
|
59
|
+
expect(collection["NZD"]).to be == (resources[0] * 2)
|
60
|
+
expect(collection.names).to be == Set.new(["NZD", "AUD", "USD"])
|
61
|
+
end
|
62
62
|
end
|
63
63
|
end
|
@@ -18,29 +18,30 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
require 'test/unit'
|
22
|
-
|
23
21
|
require 'latinum'
|
24
22
|
require 'latinum/currencies/global'
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
resource = Latinum::Resource.new("10", "NZD")
|
33
|
-
|
34
|
-
assert_equal 1000, @bank.to_integral(resource)
|
35
|
-
|
36
|
-
assert_equal resource, @bank.from_integral(1000, "NZD")
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_btc_integral
|
40
|
-
resource = Latinum::Resource.new("1.12345678", "BTC")
|
24
|
+
module Latinum::CollectionSpec
|
25
|
+
describe Latinum::Bank do
|
26
|
+
before(:all) do
|
27
|
+
@bank = Latinum::Bank.new
|
28
|
+
@bank.import(Latinum::Currencies::Global)
|
29
|
+
end
|
41
30
|
|
42
|
-
|
31
|
+
it "should convert to NZD integral value" do
|
32
|
+
resource = Latinum::Resource.new("10", "NZD")
|
33
|
+
|
34
|
+
expect(@bank.to_integral(resource)).to be == 1000
|
35
|
+
|
36
|
+
expect(@bank.from_integral(1000, "NZD")).to be == resource
|
37
|
+
end
|
43
38
|
|
44
|
-
|
39
|
+
it "should convert to BTC integral value" do
|
40
|
+
resource = Latinum::Resource.new("1.12345678", "BTC")
|
41
|
+
|
42
|
+
expect(@bank.to_integral(resource)).to be == 112345678
|
43
|
+
|
44
|
+
expect(@bank.from_integral(112345678, "BTC")).to be == resource
|
45
|
+
end
|
45
46
|
end
|
46
47
|
end
|
metadata
CHANGED
@@ -1,41 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: latinum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
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
26
|
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.1.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- -
|
45
|
+
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- -
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
description:
|
@@ -45,7 +59,7 @@ executables: []
|
|
45
59
|
extensions: []
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
|
-
- .travis.yml
|
62
|
+
- ".travis.yml"
|
49
63
|
- Gemfile
|
50
64
|
- README.md
|
51
65
|
- Rakefile
|
@@ -54,13 +68,12 @@ files:
|
|
54
68
|
- lib/latinum/bank.rb
|
55
69
|
- lib/latinum/collection.rb
|
56
70
|
- lib/latinum/currencies/global.rb
|
57
|
-
- lib/latinum/extensions/bigdecimal-1.8.rb
|
58
71
|
- lib/latinum/formatters.rb
|
59
72
|
- lib/latinum/resource.rb
|
60
73
|
- lib/latinum/version.rb
|
61
|
-
-
|
62
|
-
-
|
63
|
-
-
|
74
|
+
- spec/latinum/bank_spec.rb
|
75
|
+
- spec/latinum/collection_spec.rb
|
76
|
+
- spec/latinum/integrals_spec.rb
|
64
77
|
homepage: ''
|
65
78
|
licenses:
|
66
79
|
- MIT
|
@@ -71,23 +84,22 @@ require_paths:
|
|
71
84
|
- lib
|
72
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
73
86
|
requirements:
|
74
|
-
- -
|
87
|
+
- - ">="
|
75
88
|
- !ruby/object:Gem::Version
|
76
89
|
version: '0'
|
77
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
91
|
requirements:
|
79
|
-
- -
|
92
|
+
- - ">="
|
80
93
|
- !ruby/object:Gem::Version
|
81
94
|
version: '0'
|
82
95
|
requirements: []
|
83
96
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.2.2
|
85
98
|
signing_key:
|
86
99
|
specification_version: 4
|
87
100
|
summary: Latinum is a simple gem for managing resource computations, including money
|
88
101
|
and minerals.
|
89
102
|
test_files:
|
90
|
-
-
|
91
|
-
-
|
92
|
-
-
|
93
|
-
has_rdoc:
|
103
|
+
- spec/latinum/bank_spec.rb
|
104
|
+
- spec/latinum/collection_spec.rb
|
105
|
+
- spec/latinum/integrals_spec.rb
|
data/test/test_bank.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'test/unit'
|
24
|
-
|
25
|
-
require 'latinum'
|
26
|
-
require 'latinum/currencies/global'
|
27
|
-
|
28
|
-
class BankTest < Test::Unit::TestCase
|
29
|
-
def setup
|
30
|
-
@bank = Latinum::Bank.new
|
31
|
-
@bank.import(Latinum::Currencies::Global)
|
32
|
-
|
33
|
-
@bank << Latinum::ExchangeRate.new("NZD", "AUD", "0.5")
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_formatting
|
37
|
-
resource = Latinum::Resource.new("10", "NZD")
|
38
|
-
assert_equal "$10.00 NZD", @bank.format(resource, :format => :full)
|
39
|
-
assert_equal "$10.00", @bank.format(resource, :format => :compact)
|
40
|
-
|
41
|
-
resource = Latinum::Resource.new("391", "AUD")
|
42
|
-
assert_equal "$391.00 AUD", @bank.format(resource)
|
43
|
-
|
44
|
-
resource = Latinum::Resource.new("-100", "NZD")
|
45
|
-
assert_equal "-$100.00 NZD", @bank.format(resource)
|
46
|
-
|
47
|
-
resource = Latinum::Resource.new("1.12345678", "BTC")
|
48
|
-
assert_equal "B⃦1.12345678 BTC", @bank.format(resource)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_exchange
|
52
|
-
nzd = Latinum::Resource.new("10", "NZD")
|
53
|
-
|
54
|
-
aud = @bank.exchange nzd, "AUD"
|
55
|
-
assert_equal Latinum::Resource.new("5", "AUD"), aud
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_parsing
|
59
|
-
assert_equal Latinum::Resource.new("5", "USD"), @bank.parse("$5")
|
60
|
-
assert_equal Latinum::Resource.new("5", "NZD"), @bank.parse("$5 NZD")
|
61
|
-
assert_equal Latinum::Resource.new("5", "EUR"), @bank.parse("€5")
|
62
|
-
|
63
|
-
assert_equal Latinum::Resource.new("5", "NZD"), @bank.parse("5 NZD")
|
64
|
-
end
|
65
|
-
end
|