latinum 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ Latinum
2
+ =======
3
+
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.
7
+
8
+ Latinum is a framework for resource and currency calculations. *It is currently a work in progress and not designed to be taken seriously at this time*.
9
+
10
+ License
11
+ -------
12
+
13
+ Copyright (c) 2010, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in
23
+ all copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
+ THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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 'latinum/resource'
22
+ require 'latinum/collection'
@@ -0,0 +1,112 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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 'latinum/resource'
22
+
23
+ module Latinum
24
+ class ExchangeRate
25
+ def initialize(input, output, factor)
26
+ @input = input
27
+ @output = output
28
+ @factor = BigDecimal(factor)
29
+ end
30
+
31
+ attr :input
32
+ attr :output
33
+ attr :factor
34
+ end
35
+
36
+ class Bank
37
+ def initialize(*imports)
38
+ @rates = []
39
+ @exchange = {}
40
+
41
+ @formatters = {}
42
+
43
+ # Symbols and their associated priorities
44
+ @symbols = {}
45
+
46
+ imports.each do |resources|
47
+ import(resources)
48
+ end
49
+ end
50
+
51
+ def import(resources)
52
+ resources.each do |name, config|
53
+ name = (config[:name] || name).to_s
54
+
55
+ # Create a formatter:
56
+ self[name] = config[:formatter].new(config)
57
+
58
+ if config[:symbol]
59
+ symbols = (@symbols[config[:symbol]] ||= [])
60
+ symbols << [config.fetch(:priority, -1), name.to_s]
61
+ symbols.sort!.uniq!
62
+ end
63
+ end
64
+ end
65
+
66
+ attr :rates
67
+ attr :symbols
68
+ attr :formatters
69
+
70
+ def << object
71
+ @rates << rate
72
+
73
+ @exchange[rate.input] ||= {}
74
+ @exchange[rate.input][rate.output] = rate
75
+ end
76
+
77
+ def []= name, formatter
78
+ @formatters[name] = formatter
79
+ end
80
+
81
+ def exchange(resource, for_name)
82
+ rate = @exchange[resource.name][for_name]
83
+ raise ArgumentError.new("Invalid rate specified #{rate}") if rate == nil
84
+
85
+ Resource.new(resource.amount * rate.factor, for_name)
86
+ end
87
+
88
+ def parse(string)
89
+ parts = string.strip.split(/\s+, 2/)
90
+
91
+ if parts.size == 2
92
+ Resource.new(parts[0].gsub(/[^\.0-9]/, ''), parts[1])
93
+ else
94
+ # Lookup the named symbol, e.g. '$', and get the highest priority name:
95
+ symbol = @symbols.fetch(string.gsub(/[\-\.,0-9]/, ''), []).last
96
+
97
+ if symbol
98
+ Resource.new(string.gsub(/[^\.0-9]/, ''), symbol.last.to_s)
99
+ else
100
+ raise ArgumentError.new("Could not parse #{string}")
101
+ end
102
+ end
103
+ end
104
+
105
+ def format(resource, *args)
106
+ formatter = @formatters[resource.name]
107
+ raise ArgumentError.new("No formatter found for #{resource.name}") unless formatter
108
+
109
+ formatter.format(resource.amount, *args)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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 'bigdecimal'
22
+ require 'set'
23
+
24
+ module Latinum
25
+ class Collection
26
+ def initialize(names = Set.new)
27
+ @names = names
28
+ @resources = Hash.new {|hash, key| @names << key; BigDecimal.new(0)}
29
+ end
30
+
31
+ attr :names
32
+ attr :resources
33
+
34
+ def << resource
35
+ @resources[resource.name] += resource.amount
36
+ end
37
+
38
+ def [] key
39
+ Resource.new(@resources[key], key)
40
+ end
41
+
42
+ def []= key, amount
43
+ @resources[key] = amount
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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/formatters'
24
+
25
+ module Latinum
26
+ module Currencies
27
+ Global = {}
28
+
29
+ Global[:NZD] = {
30
+ :symbol => '$',
31
+ :name => 'NZD',
32
+ :description => 'New Zealand Dollar',
33
+ :formatter => Formatters::DecimalCurrencyFormatter,
34
+ }
35
+
36
+ Global[:GBP] = {
37
+ :symbol => '£',
38
+ :name => 'GBP',
39
+ :description => 'Pound Sterling',
40
+ :formatter => Formatters::DecimalCurrencyFormatter,
41
+ }
42
+
43
+ Global[:AUD] = {
44
+ :symbol => '$',
45
+ :name => 'AUD',
46
+ :description => 'Australian Dollar',
47
+ :formatter => Formatters::DecimalCurrencyFormatter,
48
+ }
49
+
50
+ Global[:USD] = {
51
+ :symbol => '$',
52
+ :name => 'USD',
53
+ :description => 'United States Dollar',
54
+ :formatter => Formatters::DecimalCurrencyFormatter,
55
+ }
56
+
57
+ Global[:EUR] = {
58
+ :symbol => '€',
59
+ :name => 'EUR',
60
+ :description => 'Euro',
61
+ :formatter => Formatters::DecimalCurrencyFormatter,
62
+ #:delimeter => '.',
63
+ #:separator => ','
64
+ }
65
+ end
66
+ end
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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
+ module Latinum
22
+ module Formatters
23
+ DEFAULT_OPTIONS = {
24
+ :format => :full
25
+ }
26
+
27
+ class PlainFormatter
28
+ def initialize(options = {})
29
+ @name = options[:name]
30
+ end
31
+
32
+ def format(amount)
33
+ "#{amount.to_s('F')} #{@name}"
34
+ end
35
+ end
36
+
37
+ class DecimalCurrencyFormatter
38
+ def initialize(options = {})
39
+ @symbol = options[:symbol] || '$'
40
+ @separator = options[:separator] || '.'
41
+ @delimeter = options[:delimter] || ','
42
+ @places = options[:places] || 2
43
+ @zero = options[:zero] || '0'
44
+
45
+ @name = options[:name]
46
+ end
47
+
48
+ def format(amount, options = DEFAULT_OPTIONS)
49
+ fix, frac = amount.to_s('F').split(/\./, 2)
50
+
51
+ # The sign of the number
52
+ sign = amount.sign < 0 ? '-' : ''
53
+
54
+ # Decimal places, e.g. the '.00' in '$10.00'
55
+ frac = frac[0...2].ljust(@places, @zero)
56
+
57
+ # Grouping, e.g. the ',' in '$10,000.00'
58
+ remainder = fix.size % 3
59
+ groups = fix[remainder..-1].scan(/.{3}/).to_a
60
+ groups.unshift(fix[0...remainder]) if remainder > 0
61
+
62
+ whole = "#{sign}#{@symbol}#{groups.join(@delimeter)}"
63
+ name = (@name && options[:format] == :full) ? " #{@name}" : ''
64
+
65
+ if @places > 0
66
+ "#{whole}#{@separator}#{frac}#{name}"
67
+ else
68
+ "#{whole}#{name}"
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,77 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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 'bigdecimal'
22
+
23
+ module Latinum
24
+ # A fixed unit in a given named resource
25
+ class Resource
26
+ include Comparable
27
+
28
+ attr :amount
29
+ attr :name
30
+
31
+ def initialize(amount, name)
32
+ @amount = BigDecimal(amount)
33
+ @name = name
34
+ end
35
+
36
+ # By default, we can only add and subtract if the name is the same
37
+ def + other
38
+ throw ArgumentError.new("Cannot operate on different currencies!") if @name != other.name
39
+
40
+ self.class.new(@amount + other.amount, @name)
41
+ end
42
+
43
+ def - other
44
+ throw ArgumentError.new("Cannot operate on different currencies!") if @name != other.name
45
+
46
+ self.class.new(@amount - other.amount, @name)
47
+ end
48
+
49
+ def -@
50
+ self.class.new(-@amount, @name)
51
+ end
52
+
53
+ def * factor
54
+ self.class.new(@amount * factor, @name)
55
+ end
56
+
57
+ def to_s(options = {})
58
+ @amount.to_s('F') + ' ' + @name.to_s
59
+ end
60
+
61
+ def <=> other
62
+ if @name == other.name
63
+ @amount <=> other.amount
64
+ else
65
+ @name <=> other.name
66
+ end
67
+ end
68
+
69
+ def self.parse(string)
70
+ self.new *string.split(/\s+/, 2)
71
+ end
72
+
73
+ def zero?
74
+ @amount.zero?
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
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
+ module Latinum
22
+ module VERSION
23
+ MAJOR = 0
24
+ MINOR = 2
25
+ TINY = 0
26
+
27
+ STRING = [MAJOR, MINOR, TINY].join('.')
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+
2
+ $LOAD_PATH.unshift File.expand_path("../../lib/", __FILE__)
3
+
4
+ require 'test/unit'
@@ -0,0 +1,21 @@
1
+
2
+ require 'helper'
3
+
4
+ require 'latinum/bank'
5
+ require 'latinum/currencies/global'
6
+
7
+ class BankTest < Test::Unit::TestCase
8
+ def setup
9
+ @bank = Latinum::Bank.new
10
+ @bank.import(Latinum::Currencies::Global)
11
+ end
12
+
13
+ def test_formatting
14
+ resource = Latinum::Resource.new("10", "NZD")
15
+ assert_equal "$10.00 NZD", @bank.format(resource, :format => :full)
16
+ assert_equal "$10.00", @bank.format(resource, :format => :compact)
17
+
18
+ resource = Latinum::Resource.new("391", "AUD")
19
+ assert_equal "$391.00 AUD", @bank.format(resource)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: latinum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: samuel.williams@oriontransfer.co.nz
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/latinum/bank.rb
21
+ - lib/latinum/collection.rb
22
+ - lib/latinum/currencies/global.rb
23
+ - lib/latinum/formatters.rb
24
+ - lib/latinum/resource.rb
25
+ - lib/latinum/version.rb
26
+ - lib/latinum.rb
27
+ - test/helper.rb
28
+ - test/test_bank.rb
29
+ - README.md
30
+ homepage: http://www.oriontransfer.co.nz/gems/latinum
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.23
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Latinum is a simple gem for managing resource computations, including money
54
+ and minerals.
55
+ test_files: []
56
+ has_rdoc: