latinum 0.5.6 → 0.6.0
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 +4 -4
- data/README.md +128 -3
- data/lib/latinum/bank.rb +7 -0
- data/lib/latinum/formatters.rb +4 -0
- data/lib/latinum/version.rb +1 -1
- data/spec/latinum/bank_spec.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4a1959c3b1c833751fdc2536ce1db9c040e48be
|
4
|
+
data.tar.gz: a870124602130bfcfc036ba61abdbcc529af9645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85efa5c7e88b2a0e5b41f039054daae59f55848060d24f9d6d0d8a88e406391a77309febdf727c10db505e4bd2de76e656de1e6e996eed485c3f396fdcc72027
|
7
|
+
data.tar.gz: c620f6a689945b472cee3290a01f89c4fef33d192c6600fbe5f14346b0071b92c46cb0c5d865b38ffce630d2f4b38825edbca8c9a098692f565c6d50c3a3f496
|
data/README.md
CHANGED
@@ -9,15 +9,15 @@ Latinum is a library for resource and currency calculations. It provides immutab
|
|
9
9
|
|
10
10
|
Add this line to your application's Gemfile:
|
11
11
|
|
12
|
-
|
12
|
+
gem 'latinum'
|
13
13
|
|
14
14
|
And then execute:
|
15
15
|
|
16
|
-
|
16
|
+
$ bundle
|
17
17
|
|
18
18
|
Or install it yourself as:
|
19
19
|
|
20
|
-
|
20
|
+
$ gem install latinum
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
@@ -58,6 +58,64 @@ To add multiple currencies together, use a collection:
|
|
58
58
|
> currencies.collect {|currency| collection[currency]}
|
59
59
|
=> [10.0 NZD, 20.0 AUD]
|
60
60
|
|
61
|
+
#### Calculating Totals
|
62
|
+
|
63
|
+
The `Latinum::Collection` is the correct way to sum up a list of transactions or other items with an
|
64
|
+
associated `Latinum::Resource`. Here is an example:
|
65
|
+
|
66
|
+
<table class="listing transactions" data-model="Transaction">
|
67
|
+
<thead>
|
68
|
+
<tr>
|
69
|
+
<th class="name">Name</th>
|
70
|
+
<th class="date">Date</th>
|
71
|
+
<th class="price">Price</th>
|
72
|
+
<th class="quantity">Quantity</th>
|
73
|
+
<th class="subtotal">Sub-total</th>
|
74
|
+
<th class="tax_rate">Tax</th>
|
75
|
+
<th class="total">Total</th>
|
76
|
+
</tr>
|
77
|
+
</thead>
|
78
|
+
<tbody>
|
79
|
+
<?r
|
80
|
+
currencies = Set.new
|
81
|
+
|
82
|
+
summary = {
|
83
|
+
:subtotal => Latinum::Collection.new(currencies),
|
84
|
+
:tax => Latinum::Collection.new(currencies),
|
85
|
+
:total => Latinum::Collection.new(currencies)
|
86
|
+
}
|
87
|
+
|
88
|
+
invoice.transactions.each do |transaction|
|
89
|
+
subtotal = transaction.subtotal
|
90
|
+
summary[:subtotal] << subtotal
|
91
|
+
summary[:tax] << subtotal * transaction.tax_rate.to_d
|
92
|
+
summary[:total] << transaction.total
|
93
|
+
|
94
|
+
?>
|
95
|
+
<tr data-id="#{transaction.id}" data-rev="#{transaction.rev}">
|
96
|
+
<th class="name">#{f.text transaction.name}</th>
|
97
|
+
<td class="date">#{f.text transaction.date}</td>
|
98
|
+
<td class="price">#{f.text transaction.price}</td>
|
99
|
+
<td class="quantity">#{f.quantity transaction}</td>
|
100
|
+
<td class="subtotal">#{f.text subtotal}</td>
|
101
|
+
<td class="tax_rate">#{f.tax transaction}</td>
|
102
|
+
<td class="total">#{f.text transaction.total}</td>
|
103
|
+
</tr>
|
104
|
+
<?r end ?>
|
105
|
+
</tbody>
|
106
|
+
<tfoot>
|
107
|
+
<?r currencies.each do |currency| ?>
|
108
|
+
<tr>
|
109
|
+
<td colspan="5">#{currency} Summary:</td>
|
110
|
+
<td class="subtotal">#{f.text summary[:subtotal][currency]}</td>
|
111
|
+
<td class="tax_rate">#{f.text summary[:tax][currency]}</td>
|
112
|
+
<td class="total">#{f.text summary[:total][currency]}</td>
|
113
|
+
<td></td>
|
114
|
+
</tr>
|
115
|
+
<?r end ?>
|
116
|
+
</tfoot>
|
117
|
+
</table>
|
118
|
+
|
61
119
|
### Banks and Exchange Rates
|
62
120
|
|
63
121
|
The bank is responsible for formatting and exchange rates:
|
@@ -108,6 +166,73 @@ For storage in traditional databases, you may prefer to use integers. Based on t
|
|
108
166
|
|
109
167
|
As BitCoin has 8 decimal places, it requires an integer representation with at least 10^8.
|
110
168
|
|
169
|
+
### ActiveRecord Serialization
|
170
|
+
|
171
|
+
Latinum can be easily used in a [ActiveRecord](https://github.com/rails/rails/tree/master/activerecord) model simply by declaring a serialized data-type for a string or text column, e.g.
|
172
|
+
|
173
|
+
class Transaction < ActiveRecord::Base
|
174
|
+
serialize :total, Latinum::Resource
|
175
|
+
end
|
176
|
+
|
177
|
+
It can be used like so:
|
178
|
+
|
179
|
+
> transaction = Transaction.new(:total => "10 NZD")
|
180
|
+
> transaction.total * 2
|
181
|
+
=> "20.0 NZD"
|
182
|
+
|
183
|
+
To format the output, use a `Latinum::Bank`, e.g. assuming the bank is set up correctly:
|
184
|
+
|
185
|
+
> bank.format(transaction.total)
|
186
|
+
=> "$20.00 NZD"
|
187
|
+
|
188
|
+
> bank.format(transaction.total, name: nil)
|
189
|
+
=> "$20.00"
|
190
|
+
|
191
|
+
> bank.format(transaction.total, symbol: nil)
|
192
|
+
=> "20.00 NZD"
|
193
|
+
|
194
|
+
### Relaxo Serialization
|
195
|
+
|
196
|
+
Latinum is natively supported by [Relaxo](https://github.com/ioquatix/relaxo) (CouchDB) and as such can be used in [Relaxo models](https://github.com/ioquatix/relaxo-model) easily.
|
197
|
+
|
198
|
+
require 'latinum'
|
199
|
+
require 'relaxo/model'
|
200
|
+
require 'relaxo/model/properties/latinum'
|
201
|
+
|
202
|
+
class Transaction
|
203
|
+
include Relaxo::Model
|
204
|
+
|
205
|
+
property :name
|
206
|
+
property :price, Attribute[Latinum::Resource]
|
207
|
+
end
|
208
|
+
|
209
|
+
db = Relaxo.connect('test')
|
210
|
+
db.create!
|
211
|
+
|
212
|
+
t = Transaction.create(db, price: Latinum::Resource.load("50 NZD"))
|
213
|
+
|
214
|
+
t.price
|
215
|
+
# => <Latinum::Resource "50.0 NZD">
|
216
|
+
|
217
|
+
# Save and reload from database server:
|
218
|
+
t.save
|
219
|
+
t = Transaction.fetch(db, t.id)
|
220
|
+
|
221
|
+
t.price
|
222
|
+
# => <Latinum::Resource "50.0 NZD">
|
223
|
+
|
224
|
+
It gets stored in the database like so:
|
225
|
+
|
226
|
+
{
|
227
|
+
"_id": "740f4728fc9a571d826688db2f004771",
|
228
|
+
"_rev": "1-45a29c63311cfa0d5a765707184b2b3b",
|
229
|
+
"type": "transaction",
|
230
|
+
"price": [
|
231
|
+
"50.0",
|
232
|
+
"NZD"
|
233
|
+
]
|
234
|
+
}
|
235
|
+
|
111
236
|
## Contributing
|
112
237
|
|
113
238
|
1. Fork it
|
data/lib/latinum/bank.rb
CHANGED
@@ -115,6 +115,13 @@ module Latinum
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
def round(resource)
|
119
|
+
formatter = @formatters[resource.name]
|
120
|
+
raise ArgumentError.new("No formatter found for #{resource.name}") unless formatter
|
121
|
+
|
122
|
+
Latinum::Resource.new(formatter.round(resource.amount), resource.name)
|
123
|
+
end
|
124
|
+
|
118
125
|
# Format a resource as a string according to the loaded currencies.
|
119
126
|
def format(resource, *args)
|
120
127
|
formatter = @formatters[resource.name]
|
data/lib/latinum/formatters.rb
CHANGED
@@ -53,6 +53,10 @@ module Latinum
|
|
53
53
|
@name = options[:name]
|
54
54
|
end
|
55
55
|
|
56
|
+
def round(amount)
|
57
|
+
return amount.round(@places)
|
58
|
+
end
|
59
|
+
|
56
60
|
def format(amount, options = DEFAULT_OPTIONS)
|
57
61
|
# Round to the desired number of places. Truncation used to be the default.
|
58
62
|
amount = amount.round(@places)
|
data/lib/latinum/version.rb
CHANGED
data/spec/latinum/bank_spec.rb
CHANGED
@@ -48,6 +48,18 @@ module Latinum::BankSpec
|
|
48
48
|
expect(@bank.format(resource)).to be == "B⃦1.12345678 BTC"
|
49
49
|
end
|
50
50
|
|
51
|
+
it "should round up using correct precision" do
|
52
|
+
resource = Latinum::Resource.new("19.9989", "NZD")
|
53
|
+
|
54
|
+
expect(@bank.round(resource)).to be == Latinum::Resource.new(20, "NZD")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should round down using correct precision" do
|
58
|
+
resource = Latinum::Resource.new("19.991", "NZD")
|
59
|
+
|
60
|
+
expect(@bank.round(resource)).to be == Latinum::Resource.new("19.99", "NZD")
|
61
|
+
end
|
62
|
+
|
51
63
|
it "should round values when formatting" do
|
52
64
|
resource = Latinum::Resource.new("19.9989", "NZD")
|
53
65
|
|
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: 0.6.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: 2015-08-
|
11
|
+
date: 2015-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|