latinum 0.7.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +4 -0
- data/.simplecov +9 -0
- data/.travis.yml +10 -3
- data/Gemfile +4 -1
- data/README.md +14 -1
- data/Rakefile +10 -5
- data/latinum.gemspec +2 -2
- data/lib/latinum/formatters.rb +2 -2
- data/lib/latinum/version.rb +1 -1
- data/spec/latinum/bank_spec.rb +5 -4
- data/spec/latinum/collection_spec.rb +38 -16
- data/spec/latinum/formatters_spec.rb +29 -0
- data/spec/latinum/resource_spec.rb +50 -2
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16215481d04c41e76fa7bda13e0977acbf0a3862
|
4
|
+
data.tar.gz: a192fa7dd018680858f4770e44c318cbf6b9be1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8d753a9318134a4347ab131f549b1bad9aa5970c805b0458b6357f56002b47241679c9d8194f3a41223a2e0be41112742f293162c970ae40cf76b4249ab1e10
|
7
|
+
data.tar.gz: 6c1f2c3137edbb552730b052077aa462a39e8749ef50c6d7f033c7acc536d1f20132b4d09511993f2aac035cf50ea2911a154f4e6e66220c6fbcefbcfe5ff3b6
|
data/.rspec
ADDED
data/.simplecov
ADDED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,20 @@
|
|
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.
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ioquatix/latinum.svg)](https://codeclimate.com/github/ioquatix/latinum)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ioquatix/latinum/badge.svg)](https://coveralls.io/r/ioquatix/latinum)
|
8
|
+
|
9
|
+
## Motivation
|
10
|
+
|
11
|
+
I was originally planning on using the [Money gem](https://github.com/RubyMoney/money), but it's [dependency on global state](https://github.com/RubyMoney/money/blob/39b617cca8f02c885cc8246e0aab3e9dc5f90e15/lib/money/currency.rb#L19-L21) makes it hard to use if you want to deal with money as an immutable value type.
|
12
|
+
|
13
|
+
Additionally, I wanted to support BitCoin, Japanese Yen, etc. The money gem was heavily biased towards decimal currency. It had (~2012) [fields like `dollars` and `cents`](https://github.com/RubyMoney/money/issues/197) which don't really make sense and don't really align with the real world. These days they have fixed parts of the API, but it's a bit of a mess now, supporting both decimal and non-decimal values.
|
14
|
+
|
15
|
+
Another problem I had at the time was the [concept of zero](https://github.com/RubyMoney/money/issues/195). It should be possible to have an additive (e.g. 0) and multiplicative identity (e.g. 1) do the right thing. In fact, in Latinum, you can multiply `Latinum::Resource` instances by a scalar and get a useful result (e.g. for computing discounts).
|
16
|
+
|
17
|
+
Finally, because of the above problem, it was not obvious at the time how to sum up a collection of money instances correctly. In fact, this is still a problem and a separate gem, based on the `Latinum::Collection` concept, [was made](https://github.com/lulalala/money-collection). However, this all fits together in a rather haphazard way.
|
18
|
+
|
19
|
+
Latinum addresses all these issues. It has an immutable value type `Latinum::Resource` which has a robust definition: A value (e.g. 5.0025) and a resource name (USD). The semantics of resources are well defined without the need for "Currency" state like the symbol, how many decimal places, etc. So, it suits well for serialization into a database, and for formatting to the user, there is `Latinum::Bank` which gives you the choice of how you decide to format things or exchange them, whether you want to round something off, etc.
|
7
20
|
|
8
21
|
## Installation
|
9
22
|
|
data/Rakefile
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
require "
|
2
|
+
require "rspec/core/rake_task"
|
3
3
|
|
4
|
-
|
5
|
-
|
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
|
6
12
|
end
|
7
13
|
|
8
|
-
|
9
|
-
task :default => :test
|
14
|
+
task :default => :spec
|
data/latinum.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Samuel Williams"]
|
10
10
|
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
11
11
|
spec.summary = %q{Latinum is a simple gem for managing resource computations, including money and minerals.}
|
12
|
-
spec.homepage = ""
|
12
|
+
spec.homepage = "https://github.com/ioquatix/latinum"
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
15
15
|
spec.files = `git ls-files`.split($/)
|
@@ -18,6 +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.
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.4.0"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
end
|
data/lib/latinum/formatters.rb
CHANGED
data/lib/latinum/version.rb
CHANGED
data/spec/latinum/bank_spec.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
1
|
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
2
|
#
|
5
3
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -26,8 +24,7 @@ require 'latinum/currencies/global'
|
|
26
24
|
module Latinum::BankSpec
|
27
25
|
describe Latinum::Bank do
|
28
26
|
before(:all) do
|
29
|
-
@bank = Latinum::Bank.new
|
30
|
-
@bank.import(Latinum::Currencies::Global)
|
27
|
+
@bank = Latinum::Bank.new(Latinum::Currencies::Global)
|
31
28
|
|
32
29
|
@bank << Latinum::ExchangeRate.new("NZD", "AUD", "0.5")
|
33
30
|
end
|
@@ -80,5 +77,9 @@ module Latinum::BankSpec
|
|
80
77
|
|
81
78
|
expect(@bank.parse("5 NZD")).to be == Latinum::Resource.new("5", "NZD")
|
82
79
|
end
|
80
|
+
|
81
|
+
it "should fail to parse unknown resource" do
|
82
|
+
expect{@bank.parse("B5")}.to raise_error(ArgumentError)
|
83
|
+
end
|
83
84
|
end
|
84
85
|
end
|
@@ -24,23 +24,21 @@ require 'latinum/currencies/global'
|
|
24
24
|
require 'set'
|
25
25
|
|
26
26
|
module Latinum::CollectionSpec
|
27
|
-
describe Latinum::
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
describe Latinum::Collection do
|
28
|
+
it "can set an initial value" do
|
29
|
+
subject["NZD"] = BigDecimal.new("20")
|
30
|
+
|
31
|
+
expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
|
31
32
|
end
|
32
33
|
|
33
34
|
it "should sum up currencies correctly" do
|
34
35
|
resource = Latinum::Resource.new("10", "NZD")
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
collection << resource
|
40
|
-
expect(collection["NZD"]).to be == resource
|
37
|
+
subject << resource
|
38
|
+
expect(subject["NZD"]).to be == resource
|
41
39
|
|
42
|
-
|
43
|
-
expect(
|
40
|
+
subject << resource
|
41
|
+
expect(subject["NZD"]).to be == (resource * 2)
|
44
42
|
end
|
45
43
|
|
46
44
|
it "should sum up multiple currencies correctly" do
|
@@ -50,14 +48,38 @@ module Latinum::CollectionSpec
|
|
50
48
|
Latinum::Resource.new("10", "USD"),
|
51
49
|
Latinum::Resource.new("10", "NZD"),
|
52
50
|
Latinum::Resource.new("10", "AUD"),
|
53
|
-
Latinum::Resource.new("10", "USD")
|
51
|
+
Latinum::Resource.new("10", "USD"),
|
52
|
+
]
|
53
|
+
|
54
|
+
subject = Latinum::Collection.new
|
55
|
+
subject << resources
|
56
|
+
|
57
|
+
expect(subject["NZD"]).to be == (resources[0] * 2)
|
58
|
+
expect(subject.names).to be == Set.new(["NZD", "AUD", "USD"])
|
59
|
+
end
|
60
|
+
|
61
|
+
it "can add two collections together" do
|
62
|
+
other_resources = [
|
63
|
+
Latinum::Resource.new("10", "NZD"),
|
64
|
+
Latinum::Resource.new("10", "AUD"),
|
65
|
+
Latinum::Resource.new("10", "USD"),
|
66
|
+
]
|
67
|
+
|
68
|
+
other_collection = Latinum::Collection.new
|
69
|
+
other_collection << other_resources
|
70
|
+
|
71
|
+
resources = [
|
72
|
+
Latinum::Resource.new("10", "NZD"),
|
73
|
+
Latinum::Resource.new("10", "AUD"),
|
74
|
+
Latinum::Resource.new("10", "USD"),
|
54
75
|
]
|
55
76
|
|
56
|
-
|
57
|
-
|
77
|
+
subject << resources
|
78
|
+
subject << other_collection
|
58
79
|
|
59
|
-
expect(
|
60
|
-
expect(
|
80
|
+
expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
|
81
|
+
expect(subject["AUD"]).to be == Latinum::Resource.load("20 AUD")
|
82
|
+
expect(subject["USD"]).to be == Latinum::Resource.load("20 USD")
|
61
83
|
end
|
62
84
|
end
|
63
85
|
end
|
@@ -23,6 +23,22 @@ require 'latinum/currencies/global'
|
|
23
23
|
require 'latinum/formatters'
|
24
24
|
|
25
25
|
module Latinum::FormattersSpec
|
26
|
+
describe Latinum::Formatters::PlainFormatter.new(name: "NZD") do
|
27
|
+
let(:amount) {BigDecimal.new(10)}
|
28
|
+
|
29
|
+
it "can convert to integral" do
|
30
|
+
expect(subject.to_integral(amount)).to be == 10
|
31
|
+
end
|
32
|
+
|
33
|
+
it "can convert from integral" do
|
34
|
+
expect(subject.from_integral(10)).to be == amount
|
35
|
+
end
|
36
|
+
|
37
|
+
it "can do basic formatting" do
|
38
|
+
expect(subject.format(amount)).to be == "10.0 NZD"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
26
42
|
describe Latinum::Formatters::DecimalCurrencyFormatter do
|
27
43
|
before(:all) do
|
28
44
|
@bank = Latinum::Bank.new
|
@@ -55,4 +71,17 @@ module Latinum::FormattersSpec
|
|
55
71
|
expect(@bank.format(resource, symbol: "!!")).to be == "!!10.00 NZD"
|
56
72
|
end
|
57
73
|
end
|
74
|
+
|
75
|
+
describe Latinum::Formatters::DecimalCurrencyFormatter do
|
76
|
+
before(:all) do
|
77
|
+
@bank = Latinum::Bank.new
|
78
|
+
@bank.import(Latinum::Currencies::Global)
|
79
|
+
end
|
80
|
+
|
81
|
+
let(:resource) {Latinum::Resource.load("10 JPY")}
|
82
|
+
|
83
|
+
it "should format without separator or fractional part" do
|
84
|
+
expect(@bank.format(resource)).to be == "¥10 JPY"
|
85
|
+
end
|
86
|
+
end
|
58
87
|
end
|
@@ -76,9 +76,57 @@ module Latinum::ResourceSpec
|
|
76
76
|
it "should compute quotient" do
|
77
77
|
original_price = Latinum::Resource.load("10 NZD")
|
78
78
|
|
79
|
-
quotient = original_price / 2.0
|
80
|
-
|
81
79
|
expect(original_price / 2.0).to be == Latinum::Resource.load("5 NZD")
|
82
80
|
end
|
81
|
+
|
82
|
+
it "should add two resources of the same symbol" do
|
83
|
+
a = Latinum::Resource.load("10 NZD")
|
84
|
+
b = Latinum::Resource.load("5 NZD")
|
85
|
+
c = Latinum::Resource.load("15 NZD")
|
86
|
+
|
87
|
+
expect(a+b).to be == c
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should fail two resources of different symbol" do
|
91
|
+
a = Latinum::Resource.load("10 NZD")
|
92
|
+
b = Latinum::Resource.load("5 USD")
|
93
|
+
|
94
|
+
expect{a+b}.to raise_error(Latinum::DifferentResourceNameError)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should be able to negate a value" do
|
98
|
+
a = Latinum::Resource.load("10 NZD")
|
99
|
+
b = Latinum::Resource.load("-10 NZD")
|
100
|
+
|
101
|
+
expect(-a).to be == b
|
102
|
+
end
|
103
|
+
|
104
|
+
it "can be used as a hash key" do
|
105
|
+
a = Latinum::Resource.load("10 NZD")
|
106
|
+
b = Latinum::Resource.load("0 NZD")
|
107
|
+
|
108
|
+
hash = {a => true}
|
109
|
+
|
110
|
+
expect(hash).to be_include a
|
111
|
+
expect(hash).to_not be_include b
|
112
|
+
end
|
113
|
+
|
114
|
+
it "can be zero" do
|
115
|
+
a = Latinum::Resource.load("10 NZD")
|
116
|
+
b = Latinum::Resource.load("0 NZD")
|
117
|
+
|
118
|
+
expect(a).to_not be_zero
|
119
|
+
expect(b).to be_zero
|
120
|
+
end
|
121
|
+
|
122
|
+
it "can be eql?" do
|
123
|
+
a = Latinum::Resource.load("10 NZD")
|
124
|
+
b = Latinum::Resource.load("0 NZD")
|
125
|
+
c = Latinum::Resource.load("0 NZD")
|
126
|
+
|
127
|
+
expect(a).to be_eql a
|
128
|
+
expect(a).to_not be_eql b
|
129
|
+
expect(b).to be_eql c
|
130
|
+
end
|
83
131
|
end
|
84
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: latinum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.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:
|
11
|
+
date: 2016-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 3.
|
33
|
+
version: 3.4.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 3.
|
40
|
+
version: 3.4.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,6 +59,8 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".rspec"
|
63
|
+
- ".simplecov"
|
62
64
|
- ".travis.yml"
|
63
65
|
- Gemfile
|
64
66
|
- README.md
|
@@ -77,7 +79,7 @@ files:
|
|
77
79
|
- spec/latinum/formatters_spec.rb
|
78
80
|
- spec/latinum/integrals_spec.rb
|
79
81
|
- spec/latinum/resource_spec.rb
|
80
|
-
homepage:
|
82
|
+
homepage: https://github.com/ioquatix/latinum
|
81
83
|
licenses:
|
82
84
|
- MIT
|
83
85
|
metadata: {}
|
@@ -97,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
99
|
version: '0'
|
98
100
|
requirements: []
|
99
101
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.5.2
|
101
103
|
signing_key:
|
102
104
|
specification_version: 4
|
103
105
|
summary: Latinum is a simple gem for managing resource computations, including money
|