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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2541b0bcc344983c49e0a03ea5538b30134a6e66
4
- data.tar.gz: 6e6ea43f0549ea6866fbb4fbdc8d9b79e7c1499e
3
+ metadata.gz: 597ddf3035b21a9c9ca7cc3ab036331db974903c
4
+ data.tar.gz: 2171d764b3c41e0ca975854397d8644a10d5e6d6
5
5
  SHA512:
6
- metadata.gz: 55f37f35c7eb1dc772ee10649ff1d6281016ba58d0ffaff8e1cb4c8107273d67b7194503cdbd0bca2ed3a68e302dc356a37cf0c45e3604db5b2f228bdc3a92a2
7
- data.tar.gz: 194e82ccb7da1a0a063809f1ab5393185419cad05d86148c408658d553f47bd622b2de9a0480de2b9503282f55d841b51805cb82feff771ff1eee442b7465467
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
@@ -18,5 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rspec", "~> 3.1.0"
21
22
  spec.add_development_dependency "rake"
22
23
  end
data/lib/latinum.rb CHANGED
@@ -24,7 +24,3 @@ require 'latinum/collection'
24
24
 
25
25
  require 'bigdecimal'
26
26
  require 'bigdecimal/util'
27
-
28
- if RUBY_VERSION < "1.9.3"
29
- require 'latinum/extensions/bigdecimal-1.8'
30
- end
@@ -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
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Latinum
22
- VERSION = "0.3.0"
22
+ VERSION = "0.4.0"
23
23
  end
@@ -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
- class CollectionTest < Test::Unit::TestCase
29
- def setup
30
- @bank = Latinum::Bank.new
31
- @bank.import(Latinum::Currencies::Global)
32
- end
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
- collection = Latinum::Collection.new
58
- collection << resources
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
- assert_equal resources[0] * 2, collection["NZD"]
61
- assert_equal Set.new(["NZD", "AUD", "USD"]), collection.names
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
- class IntegralTest < Test::Unit::TestCase
27
- def setup
28
- @bank = Latinum::Bank.new(Latinum::Currencies::Global)
29
- end
30
-
31
- def test_nzd_integral
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
- assert_equal 112345678, @bank.to_integral(resource)
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
- assert_equal resource, @bank.from_integral(112345678, "BTC")
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.3.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-04-04 00:00:00.000000000 Z
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
- - test/test_bank.rb
62
- - test/test_collection.rb
63
- - test/test_integrals.rb
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.0.3
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
- - test/test_bank.rb
91
- - test/test_collection.rb
92
- - test/test_integrals.rb
93
- has_rdoc:
103
+ - spec/latinum/bank_spec.rb
104
+ - spec/latinum/collection_spec.rb
105
+ - spec/latinum/integrals_spec.rb
@@ -1,6 +0,0 @@
1
-
2
- class BigDecimal
3
- def to_d
4
- self
5
- end
6
- end
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