latinum 0.2.5 → 0.3.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2541b0bcc344983c49e0a03ea5538b30134a6e66
4
+ data.tar.gz: 6e6ea43f0549ea6866fbb4fbdc8d9b79e7c1499e
5
+ SHA512:
6
+ metadata.gz: 55f37f35c7eb1dc772ee10649ff1d6281016ba58d0ffaff8e1cb4c8107273d67b7194503cdbd0bca2ed3a68e302dc356a37cf0c45e3604db5b2f228bdc3a92a2
7
+ data.tar.gz: 194e82ccb7da1a0a063809f1ab5393185419cad05d86148c408658d553f47bd622b2de9a0480de2b9503282f55d841b51805cb82feff771ff1eee442b7465467
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9"
4
+ - "2.0"
5
+ - "2.1"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in build.gemspec
4
+ gemspec
data/README.md CHANGED
@@ -1,14 +1,24 @@
1
- Latinum
2
- =======
1
+ # Latinum
3
2
 
4
- * Author: Samuel G. D. Williams (<http://www.oriontransfer.co.nz>)
5
- * Copyright (C) 2012 Samuel G. D. Williams.
6
- * Released under the MIT license.
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.
7
4
 
8
- Latinum is a framework for resource and currency calculations.
5
+ [![Build Status](https://travis-ci.org/ioquatix/latinum.svg?branch=master)](https://travis-ci.org/ioquatix/latinum)
9
6
 
10
- Basic Usage
11
- -----------
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'latinum'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install latinum
20
+
21
+ ## Usage
12
22
 
13
23
  Latinum has several core concepts:
14
24
 
@@ -18,7 +28,7 @@ Latinum has several core concepts:
18
28
  - A `Bank` can exchange currencies explicitly with a given set of exchange rates.
19
29
  - A `Collection` is responsible for adding currencies together and is completely deterministic.
20
30
 
21
- ### Resources and Collections ###
31
+ ### Resources and Collections
22
32
 
23
33
  To create a new resource, use a string for accuracy:
24
34
 
@@ -47,7 +57,7 @@ To add multiple currencies together, use a collection:
47
57
  > currencies.collect {|currency| collection[currency]}
48
58
  => [10.0 NZD, 20.0 AUD]
49
59
 
50
- ### Banks and Exchange Rates ###
60
+ ### Banks and Exchange Rates
51
61
 
52
62
  The bank is responsible for formatting and exchange rates:
53
63
 
@@ -83,10 +93,31 @@ Currency codes take priority over symbols if specified:
83
93
  > bank.parse("€5 NZD")
84
94
  => 5.0 NZD
85
95
 
86
- License
87
- -------
96
+ ### Conversion To and From Integers
97
+
98
+ For storage in traditional databases, you may prefer to use integers. Based on the precision of the currency, you can store integer representations:
99
+
100
+ > resource = Latinum::Resource.new("1.12345678", "BTC")
101
+
102
+ > 112345678 == bank.to_integral(resource)
103
+ true
104
+
105
+ > resource == bank.from_integral(112345678, "BTC")
106
+ true
107
+
108
+ As BitCoin has 8 decimal places, it requires an integer representation with at least 10^8.
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create new Pull Request
117
+
118
+ ## License
88
119
 
89
- Copyright (c) 2010, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
120
+ Copyright, 2011, 2012, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
90
121
 
91
122
  Permission is hereby granted, free of charge, to any person obtaining a copy
92
123
  of this software and associated documentation files (the "Software"), to deal
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/latinum.gemspec ADDED
@@ -0,0 +1,22 @@
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 = ""
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 "rake"
22
+ end
data/lib/latinum.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -25,6 +25,6 @@ require 'latinum/collection'
25
25
  require 'bigdecimal'
26
26
  require 'bigdecimal/util'
27
27
 
28
- if RUBY_VERSION < "1.9"
28
+ if RUBY_VERSION < "1.9.3"
29
29
  require 'latinum/extensions/bigdecimal-1.8'
30
30
  end
data/lib/latinum/bank.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -83,8 +83,8 @@ module Latinum
83
83
  end
84
84
 
85
85
  def exchange(resource, for_name)
86
- rate = @exchange[resource.name][for_name]
87
- raise ArgumentError.new("Invalid rate specified #{rate}") if rate == nil
86
+ rate = @exchange[resource.name][for_name] rescue nil
87
+ raise ArgumentError.new("Rate #{rate} unavailable") if rate == nil
88
88
 
89
89
  config = self[for_name]
90
90
 
@@ -103,7 +103,7 @@ module Latinum
103
103
  if symbol
104
104
  Resource.new(string.gsub(/[^\.0-9]/, ''), symbol.last.to_s)
105
105
  else
106
- raise ArgumentError.new("Could not parse #{string}")
106
+ raise ArgumentError.new("Could not parse #{string}, could not determine currency!")
107
107
  end
108
108
  end
109
109
  end
@@ -114,5 +114,18 @@ module Latinum
114
114
 
115
115
  formatter.format(resource.amount, *args)
116
116
  end
117
+
118
+ # Convert the resource to an integral representation based on the currency's precision
119
+ def to_integral(resource)
120
+ formatter = @formatters[resource.name]
121
+
122
+ formatter.to_integral(resource.amount)
123
+ end
124
+
125
+ def from_integral(amount, resource_name)
126
+ formatter = @formatters[resource_name]
127
+
128
+ Resource.new(formatter.from_integral(amount), resource_name)
129
+ end
117
130
  end
118
131
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,6 +1,6 @@
1
- # encoding: utf-8
1
+ # encoding: UTF-8
2
2
  #
3
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
3
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -32,6 +32,14 @@ module Latinum
32
32
  def format(amount)
33
33
  "#{amount.to_s('F')} #{@name}"
34
34
  end
35
+
36
+ def to_integral(amount)
37
+ amount.to_i
38
+ end
39
+
40
+ def from_integral(amount)
41
+ amount.to_d
42
+ end
35
43
  end
36
44
 
37
45
  class DecimalCurrencyFormatter
@@ -52,7 +60,7 @@ module Latinum
52
60
  sign = amount.sign < 0 ? '-' : ''
53
61
 
54
62
  # Decimal places, e.g. the '.00' in '$10.00'
55
- frac = frac[0...2].ljust(@places, @zero)
63
+ frac = frac[0...@places].ljust(@places, @zero)
56
64
 
57
65
  # Grouping, e.g. the ',' in '$10,000.00'
58
66
  remainder = fix.size % 3
@@ -68,7 +76,14 @@ module Latinum
68
76
  "#{whole}#{name}"
69
77
  end
70
78
  end
79
+
80
+ def to_integral(amount)
81
+ (amount * 10**@places).to_i
82
+ end
83
+
84
+ def from_integral(amount)
85
+ (amount.to_d / 10**@places)
86
+ end
71
87
  end
72
-
73
88
  end
74
89
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -62,7 +62,7 @@ module Latinum
62
62
  self.class.new(exchanged_amount, name)
63
63
  end
64
64
 
65
- def to_s(options = {})
65
+ def to_s
66
66
  @amount.to_s('F') + ' ' + @name.to_s
67
67
  end
68
68
 
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -19,11 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Latinum
22
- module VERSION
23
- MAJOR = 0
24
- MINOR = 2
25
- TINY = 5
26
-
27
- STRING = [MAJOR, MINOR, TINY].join('.')
28
- end
22
+ VERSION = "0.3.0"
29
23
  end
data/test/test_bank.rb CHANGED
@@ -1,5 +1,26 @@
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.
1
22
 
2
- require 'helper'
23
+ require 'test/unit'
3
24
 
4
25
  require 'latinum'
5
26
  require 'latinum/currencies/global'
@@ -24,7 +45,7 @@ class BankTest < Test::Unit::TestCase
24
45
  assert_equal "-$100.00 NZD", @bank.format(resource)
25
46
 
26
47
  resource = Latinum::Resource.new("1.12345678", "BTC")
27
- assert_equal "B⃦1.12000000 BTC", @bank.format(resource)
48
+ assert_equal "B⃦1.12345678 BTC", @bank.format(resource)
28
49
  end
29
50
 
30
51
  def test_exchange
@@ -1,5 +1,24 @@
1
+ # Copyright, 2012, 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.
1
20
 
2
- require 'helper'
21
+ require 'test/unit'
3
22
 
4
23
  require 'latinum'
5
24
  require 'latinum/currencies/global'
@@ -0,0 +1,46 @@
1
+ # Copyright, 2014, 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 'test/unit'
22
+
23
+ require 'latinum'
24
+ require 'latinum/currencies/global'
25
+
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")
41
+
42
+ assert_equal 112345678, @bank.to_integral(resource)
43
+
44
+ assert_equal resource, @bank.from_integral(112345678, "BTC")
45
+ end
46
+ end
metadata CHANGED
@@ -1,22 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: latinum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Samuel Williams
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-20 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
14
41
  description:
15
- email: samuel.williams@oriontransfer.co.nz
42
+ email:
43
+ - samuel.williams@oriontransfer.co.nz
16
44
  executables: []
17
45
  extensions: []
18
46
  extra_rdoc_files: []
19
47
  files:
48
+ - .travis.yml
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - latinum.gemspec
53
+ - lib/latinum.rb
20
54
  - lib/latinum/bank.rb
21
55
  - lib/latinum/collection.rb
22
56
  - lib/latinum/currencies/global.rb
@@ -24,35 +58,36 @@ files:
24
58
  - lib/latinum/formatters.rb
25
59
  - lib/latinum/resource.rb
26
60
  - lib/latinum/version.rb
27
- - lib/latinum.rb
28
- - test/helper.rb
29
61
  - test/test_bank.rb
30
62
  - test/test_collection.rb
31
- - README.md
32
- homepage: http://www.oriontransfer.co.nz/gems/latinum
33
- licenses: []
63
+ - test/test_integrals.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
34
68
  post_install_message:
35
69
  rdoc_options: []
36
70
  require_paths:
37
71
  - lib
38
72
  required_ruby_version: !ruby/object:Gem::Requirement
39
- none: false
40
73
  requirements:
41
- - - ! '>='
74
+ - - '>='
42
75
  - !ruby/object:Gem::Version
43
76
  version: '0'
44
77
  required_rubygems_version: !ruby/object:Gem::Requirement
45
- none: false
46
78
  requirements:
47
- - - ! '>='
79
+ - - '>='
48
80
  - !ruby/object:Gem::Version
49
81
  version: '0'
50
82
  requirements: []
51
83
  rubyforge_project:
52
- rubygems_version: 1.8.23
84
+ rubygems_version: 2.0.3
53
85
  signing_key:
54
- specification_version: 3
86
+ specification_version: 4
55
87
  summary: Latinum is a simple gem for managing resource computations, including money
56
88
  and minerals.
57
- test_files: []
89
+ test_files:
90
+ - test/test_bank.rb
91
+ - test/test_collection.rb
92
+ - test/test_integrals.rb
58
93
  has_rdoc:
data/test/helper.rb DELETED
@@ -1,4 +0,0 @@
1
-
2
- $LOAD_PATH.unshift File.expand_path("../../lib/", __FILE__)
3
-
4
- require 'test/unit'