latinum 0.4.0 → 0.5.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: 597ddf3035b21a9c9ca7cc3ab036331db974903c
4
- data.tar.gz: 2171d764b3c41e0ca975854397d8644a10d5e6d6
3
+ metadata.gz: c6565ff801afd8f7700bd6723059fae97fec8718
4
+ data.tar.gz: 3c2d9ca868c490c9ab0e35bc520ca4a6c434b620
5
5
  SHA512:
6
- metadata.gz: bd414c78914a6cd7144e4e30e90657e6cb1523662c0c9ec510e363127a47b2e18ced5c29190685260b07f0db7b69b2183caecdf41729e43cfbfb430bb02aed1f
7
- data.tar.gz: ba272f4680a3c8420649f364901887d47380776b864416bfdb92d3caeb86a01f502653e408ce693cd788fe6fa70a0c8c68fe418ef49c924f39f9a805d58b1353
6
+ metadata.gz: 0c6a80ff70d4a87dfe53404389fe86159aeb0209a5f370d26c5f201fabe114805b09cbe33fc1646426361695106c24a3a5c5f5328fa304e86ea50e8b396a3540
7
+ data.tar.gz: 4c2aed581bc0637765e498d4207aa879e833905c67cedfbd1d1c6748f14f39a8aad1a89ab7fd2306ea158882766c4c0fbfed5ca451305841e1e787a14121dd85
data/README.md CHANGED
@@ -118,7 +118,7 @@ As BitCoin has 8 decimal places, it requires an integer representation with at l
118
118
 
119
119
  ## License
120
120
 
121
- Copyright, 2011, 2012, 2014, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
121
+ Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
122
122
 
123
123
  Permission is hereby granted, free of charge, to any person obtaining a copy
124
124
  of this software and associated documentation files (the "Software"), to deal
data/lib/latinum.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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
data/lib/latinum/bank.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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
@@ -33,7 +33,9 @@ module Latinum
33
33
  attr :factor
34
34
  end
35
35
 
36
+ # A bank defines exchange rates and formatting rules for resources. It is a centralised location for resource related state.
36
37
  class Bank
38
+ # Imports all given currencies.
37
39
  def initialize(*imports)
38
40
  @rates = []
39
41
  @exchange = {}
@@ -50,6 +52,7 @@ module Latinum
50
52
  end
51
53
  end
52
54
 
55
+ # Import a list of resource templates, e.g. currencies.
53
56
  def import(resources)
54
57
  resources.each do |name, config|
55
58
  name = (config[:name] || name).to_s
@@ -67,6 +70,7 @@ module Latinum
67
70
  end
68
71
  end
69
72
 
73
+ # Look up a currency by name.
70
74
  def [] name
71
75
  @currencies[name]
72
76
  end
@@ -75,6 +79,7 @@ module Latinum
75
79
  attr :symbols
76
80
  attr :currencies
77
81
 
82
+ # Add an exchange rate to the bank.
78
83
  def << rate
79
84
  @rates << rate
80
85
 
@@ -82,6 +87,7 @@ module Latinum
82
87
  @exchange[rate.input][rate.output] = rate
83
88
  end
84
89
 
90
+ # Exchange one resource for another using internally specified rates.
85
91
  def exchange(resource, for_name)
86
92
  rate = @exchange[resource.name][for_name] rescue nil
87
93
  raise ArgumentError.new("Rate #{rate} unavailable") if rate == nil
@@ -91,14 +97,15 @@ module Latinum
91
97
  resource.exchange(rate.factor, for_name, config[:precision])
92
98
  end
93
99
 
94
- def parse(string)
100
+ # Parse a string according to the loaded currencies.
101
+ def parse(string, default_name: nil)
95
102
  parts = string.strip.split(/\s+/, 2)
96
103
 
97
104
  if parts.size == 2
98
105
  Resource.new(parts[0].gsub(/[^\.0-9]/, ''), parts[1])
99
106
  else
100
107
  # Lookup the named symbol, e.g. '$', and get the highest priority name:
101
- symbol = @symbols.fetch(string.gsub(/[\-\.,0-9]/, ''), []).last
108
+ symbol = @symbols.fetch(string.gsub(/[\-\.,0-9]/, ''), []).last || default_name
102
109
 
103
110
  if symbol
104
111
  Resource.new(string.gsub(/[^\.0-9]/, ''), symbol.last.to_s)
@@ -108,6 +115,7 @@ module Latinum
108
115
  end
109
116
  end
110
117
 
118
+ # Format a resource as a string according to the loaded currencies.
111
119
  def format(resource, *args)
112
120
  formatter = @formatters[resource.name]
113
121
  raise ArgumentError.new("No formatter found for #{resource.name}") unless formatter
@@ -115,13 +123,14 @@ module Latinum
115
123
  formatter.format(resource.amount, *args)
116
124
  end
117
125
 
118
- # Convert the resource to an integral representation based on the currency's precision
126
+ # Convert the resource to an integral representation based on the currency's precision.
119
127
  def to_integral(resource)
120
128
  formatter = @formatters[resource.name]
121
129
 
122
130
  formatter.to_integral(resource.amount)
123
131
  end
124
132
 
133
+ # Convert the resource from an integral representation based on the currency's precision.
125
134
  def from_integral(amount, resource_name)
126
135
  formatter = @formatters[resource_name]
127
136
 
@@ -1,4 +1,4 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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
@@ -22,19 +22,26 @@ require 'bigdecimal'
22
22
  require 'set'
23
23
 
24
24
  module Latinum
25
+ # Aggregates a set of resources, typically used for summing values.
25
26
  class Collection
27
+ # Initialize the collection with a given set of resource names.
26
28
  def initialize(names = Set.new)
27
29
  @names = names
28
30
  @resources = Hash.new {|hash, key| @names << key; BigDecimal.new("0")}
29
31
  end
30
32
 
33
+ # All resource names which have been added to the collection, e.g. `['NZD', 'USD']`.
31
34
  attr :names
35
+
36
+ # A map of `name` => `total` for all added resources. Totals are stored as BigDecimal instances.
32
37
  attr :resources
33
38
 
39
+ # Add a resource into the totals.
34
40
  def add resource
35
41
  @resources[resource.name] += resource.amount
36
42
  end
37
43
 
44
+ # Add a resource, an array of resources, or another collection into this one.
38
45
  def << object
39
46
  case object
40
47
  when Resource
@@ -48,21 +55,12 @@ module Latinum
48
55
  return self
49
56
  end
50
57
 
51
- def others
52
- self.dup
53
-
54
- case others
55
- when Array
56
- others.each { |resource| self << resource }
57
- when Collection
58
-
59
- end
60
- end
61
-
58
+ # Get a `Resource` for the given name:
62
59
  def [] key
63
60
  Resource.new(@resources[key], key)
64
61
  end
65
62
 
63
+ # Set a `BigDecimal` value for the given name:
66
64
  def []= key, amount
67
65
  @resources[key] = amount
68
66
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
  #
3
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2015, 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, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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,4 +1,4 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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
@@ -74,8 +74,18 @@ module Latinum
74
74
  end
75
75
  end
76
76
 
77
- def self.parse(string)
78
- self.new *string.split(/\s+/, 2)
77
+ class << self
78
+ def parse(string, default_name: nil)
79
+ amount, name = string.split(/\s+/, 2)
80
+
81
+ self.new(amount, name || default_name)
82
+ end
83
+
84
+ alias load parse
85
+
86
+ def dump(resource)
87
+ resource.to_s
88
+ end
79
89
  end
80
90
 
81
91
  def zero?
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Latinum
22
- VERSION = "0.4.0"
22
+ VERSION = "0.5.0"
23
23
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
  #
3
- # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ # Copyright, 2015, 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, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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,4 +1,4 @@
1
- # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # Copyright, 2015, 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
@@ -0,0 +1,39 @@
1
+ # Copyright, 2015, 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 'latinum/resource'
22
+
23
+ module Latinum::ResourceSpec
24
+ describe Latinum::Resource do
25
+ before(:all) do
26
+ @bank = Latinum::Bank.new
27
+ @bank.import(Latinum::Currencies::Global)
28
+ end
29
+
30
+ it "should load and dump resources" do
31
+ resource = Latinum::Resource.load("10 NZD")
32
+ string_representation = Latinum::Resource.dump(resource)
33
+
34
+ loaded_resource = Latinum::Resource.load(string_representation)
35
+
36
+ expect(loaded_resource).to be == loaded_resource
37
+ end
38
+ end
39
+ 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.0
4
+ version: 0.5.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-11-12 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,7 @@ files:
74
74
  - spec/latinum/bank_spec.rb
75
75
  - spec/latinum/collection_spec.rb
76
76
  - spec/latinum/integrals_spec.rb
77
+ - spec/latinum/resource_spec.rb
77
78
  homepage: ''
78
79
  licenses:
79
80
  - MIT
@@ -103,3 +104,4 @@ test_files:
103
104
  - spec/latinum/bank_spec.rb
104
105
  - spec/latinum/collection_spec.rb
105
106
  - spec/latinum/integrals_spec.rb
107
+ - spec/latinum/resource_spec.rb