ShadowBelmolve-money 2.3.3 → 2.3.4

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.
data/README.rdoc CHANGED
@@ -12,9 +12,10 @@ This library aids one in handling money and different currencies. Features:
12
12
 
13
13
  Resources:
14
14
 
15
- - This fork: http://github.com/ShadowBelmolve/money
16
15
  - Website: http://money.rubyforge.org
17
16
  - RDoc API: http://money.rubyforge.org
17
+ - This fork: http://github.com/ShadowBelmolve/money
18
+ - Forked from: http://github.com/nofxx/money
18
19
  - Git repository: http://github.com/FooBarWidget/money/tree/master
19
20
 
20
21
 
data/lib/money/money.rb CHANGED
@@ -84,7 +84,8 @@ class Money
84
84
 
85
85
  # Do two money objects equal? Only works if both objects are of the same currency
86
86
  def ==(other_money)
87
- cents == other_money.cents && bank.same_currency?(currency, other_money.currency)
87
+ other_money.respond_to?(:cents) && cents == other_money.cents &&
88
+ other_money.respond_to?(:currency) && bank.same_currency?(currency, other_money.currency)
88
89
  end
89
90
 
90
91
  def <=>(other_money)
@@ -149,6 +150,40 @@ class Money
149
150
  Money.new(rate / 100 / period * cents * count)
150
151
  end
151
152
 
153
+ #Round to nearest coin value
154
+ # basically, we don't have coins for cents in CZK,
155
+ # our smallest fraction is 0.50CZK
156
+ #
157
+ #Money.new(14_58).round_to_coin(50) => 14.50
158
+ def round_to_coin(coin)
159
+ coef = 1.0/coin
160
+ val = (cents * coef).round / coef
161
+ Money.new(val, currency)
162
+ end
163
+
164
+ #Returns array a where
165
+ # a[0] is price _after_ applying tax (tax base)
166
+ # a[1] is tax
167
+ def tax_brakedown(tax)
168
+ _tax = (cents * (tax / 100.0)).round
169
+ [Money.new(cents + _tax, currency), Money.new(_tax, currency)]
170
+ end
171
+
172
+ #Returns array a where
173
+ # a[0] is price _before_ applying tax (tax base)
174
+ # a[1] is tax
175
+ def tax_reverse_brakedown(tax)
176
+ coef = tax/100.0
177
+ [Money.new((cents / (1+coef)).round, currency),
178
+ Money.new((cents*coef/(1+coef)).round, currency) ]
179
+ end
180
+
181
+ # Just a helper if you got tax inputs in percentage.
182
+ # Ie. add_tax(20) => cents * 1.20
183
+ def add_tax(tax)
184
+ tax_brakedown(tax)[0]
185
+ end
186
+
152
187
  # Split money in number of installments
153
188
  #
154
189
  # Money.new(10_00).split_in_installments(3)
@@ -171,12 +206,6 @@ class Money
171
206
  split_in_installments(cents/other_money.cents, order)
172
207
  end
173
208
 
174
- # Just a helper if you got tax inputs in percentage.
175
- # Ie. add_tax(20) => cents * 1.20
176
- def add_tax(tax)
177
- Money.new(cents + cents / 100 * tax)
178
- end
179
-
180
209
  # Format the price according to several rules
181
210
  # Currently supported are :with_currency, :no_cents, :symbol and :html
182
211
  #
@@ -31,15 +31,20 @@ class Money
31
31
  def initialize
32
32
  @rates = {}
33
33
  @rates["USD"] = 1.0
34
+ @mutex = Mutex.new
34
35
  end
35
36
 
36
37
  def add_rate(currency, rate)
37
- @rates[currency.upcase] = (currency.upcase != Money.default_currency) ? (rate * @rates[Money.default_currency]) : rate
38
+ @mutex.synchronize do
39
+ @rates[currency.upcase] = (currency.upcase != Money.default_currency) ? (rate * @rates[Money.default_currency]) : rate
40
+ end
38
41
  end
39
42
 
40
43
  def get_rate(currency = nil)
41
- return nil unless @rates[currency]
42
- (currency != Money.default_currency) ? @rates[currency.upcase] / @rates[Money.default_currency] : @rates[currency.upcase]
44
+ @mutex.synchronize do
45
+ return nil unless @rates[currency]
46
+ (currency != Money.default_currency) ? @rates[currency.upcase] / @rates[Money.default_currency] : @rates[currency.upcase]
47
+ end
43
48
  end
44
49
 
45
50
  # Given two currency names, checks whether they're both the same currency.
data/money.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{money}
5
- s.version = "2.3.3"
5
+ s.version = "2.3.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Money Team"]
@@ -114,8 +114,16 @@ describe Money do
114
114
  end
115
115
 
116
116
  it "should calculate tax" do
117
- Money.new(100).add_tax(20).cents.should eql(120)
118
- Money.new(100).add_tax(-20).cents.should eql(80)
117
+ Money.new(1218).add_tax(19).cents.should eql(1449)
118
+ Money.new(1218).add_tax(-19).cents.should eql(987)
119
+ end
120
+
121
+ it "should provide tax brakedown" do
122
+ Money.new(1225).tax_brakedown(19).map{|c| c.to_s}.should eql(['14.58','2.33'])
123
+ end
124
+
125
+ it "should provide reverse tax brakedown" do
126
+ Money.new(-8).tax_reverse_brakedown(19).map{|c| c.to_s}.should eql(['-0.07','-0.01'])
119
127
  end
120
128
 
121
129
  it "shuld to_s wallet" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ShadowBelmolve-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.3
4
+ version: 2.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Money Team