ShadowBelmolve-money 2.3.5 → 2.3.7

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
@@ -28,7 +28,7 @@ Install stable releases with the following command:
28
28
  The development version (hosted on Github) can be installed with:
29
29
 
30
30
  gem sources -a http://gems.github.com
31
- gem install nofxx-money
31
+ gem install ShadowBelmolve-money
32
32
 
33
33
 
34
34
  == Usage
@@ -96,7 +96,7 @@ fields on the database.
96
96
 
97
97
  config/enviroment.rb
98
98
 
99
- require.gem 'nofxx-money', :lib => 'money'
99
+ require.gem 'ShadowBelmolve-money', :lib => 'money'
100
100
 
101
101
  app/models/product.rb
102
102
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.3.5
1
+ 2.3.7
@@ -10,10 +10,10 @@ class Numeric
10
10
  if self.is_a? Integer
11
11
  Money.new(self)
12
12
  else
13
- Money.new(self.to_s.gsub(/\./,'').to_i)
13
+ Money.new(self.to_s.gsub(/\./,'').round)
14
14
  end
15
15
  else
16
- Money.new(self * 100)
16
+ Money.new((self * 100).round)
17
17
  end
18
18
  end
19
19
  end
@@ -22,35 +22,35 @@ class String
22
22
  # Parses the current string and converts it to a Money object.
23
23
  # Excess characters will be discarded.
24
24
  #
25
- # '100'.to_money # => #<Money @cents=100>
25
+ # '100'.to_money # => #<Money @cents=10000>
26
26
  # '100.37'.to_money # => #<Money @cents=10037>
27
- # '100 USD'.to_money # => #<Money @cents=100, @currency="USD">
28
- # 'USD 100'.to_money # => #<Money @cents=100, @currency="USD">
29
- # '$100 USD'.to_money # => #<Money @cents=100, @currency="USD">
30
- # '$100 USD'.to_money(false) # => #<Money @cents=10000, @currency="USD">
31
- def to_money(with_cents = false)
27
+ # '100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
28
+ # 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD">
29
+ # '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
30
+ # 'hello 2000 world'.to_money # => #<Money @cents=200000 @currency="USD")>
31
+ def to_money
32
32
  # Get the currency.
33
33
  matches = scan /([A-Z]{2,3})/
34
34
  currency = matches[0] ? matches[0][0] : Money.default_currency
35
- cents = calculate_cents(self, with_cents)
35
+ cents = calculate_cents(self)
36
36
  Money.new(cents, currency)
37
37
  end
38
-
38
+
39
39
  private
40
-
41
- def calculate_cents(number, with_cents = true)
40
+
41
+ def calculate_cents(number)
42
42
  # remove anything that's not a number, potential delimiter, or minus sign
43
43
  num = number.gsub(/[^\d|\.|,|\'|\s|\-]/, '').strip
44
-
44
+
45
45
  # set a boolean flag for if the number is negative or not
46
46
  negative = num.split(//).first == "-"
47
-
47
+
48
48
  # if negative, remove the minus sign from the number
49
49
  num = num.gsub(/^-/, '') if negative
50
-
50
+
51
51
  # gather all separators within the result number
52
52
  used_separators = num.scan /[^\d]/
53
-
53
+
54
54
  # determine the number of unique separators within the number
55
55
  #
56
56
  # e.g.
@@ -61,8 +61,8 @@ class String
61
61
  case used_separators.uniq.length
62
62
  # no separator or delimiter; major (dollars) is the number, and minor (cents) is 0
63
63
  when 0 then major, minor = num, 0
64
-
65
- # two separators, so we know the last item in this array is the
64
+
65
+ # two separators, so we know the last item in this array is the
66
66
  # major/minor delimiter and the rest are separators
67
67
  when 2
68
68
  separator, delimiter = used_separators.uniq
@@ -78,10 +78,10 @@ class String
78
78
  # 1,000,000 - comma is a separator
79
79
  # 10000,00 - comma is a delimiter
80
80
  # 1000,000 - comma is a delimiter
81
-
81
+
82
82
  # assign first separator for reusability
83
83
  separator = used_separators.first
84
-
84
+
85
85
  # separator is used as a separator when there are multiple instances, always
86
86
  if num.scan(separator).length > 1 # multiple matches; treat as separator
87
87
  major, minor = num.gsub(separator, ''), 0
@@ -89,23 +89,25 @@ class String
89
89
  # ex: 1,000 - 1.0000 - 10001.000
90
90
  # split number into possible major (dollars) and minor (cents) values
91
91
  possible_major, possible_minor = num.split(separator)
92
-
92
+ possible_major ||= "0"
93
+ possible_minor ||= "00"
94
+
93
95
  # if the minor (cents) length isn't 3, assign major/minor from the possibles
94
96
  # e.g.
95
- # 1,00 => 1.00
96
- # 1.0000 => 1.00
97
- # 1.2 => 1.20
97
+ # 1,00 => 1.00
98
+ # 1.0000 => 1.00
99
+ # 1.2 => 1.20
98
100
  if possible_minor.length != 3 # delimiter
99
101
  major, minor = possible_major, possible_minor
100
102
  else
101
103
  # minor length is three
102
104
  # let's try to figure out intent of the delimiter
103
-
104
- # the major length is greater than three, which means
105
+
106
+ # the major length is greater than three, which means
105
107
  # the comma or period is used as a delimiter
106
108
  # e.g.
107
- # 1000,000
108
- # 100000,000
109
+ # 1000,000
110
+ # 100000,000
109
111
  if possible_major.length > 3
110
112
  major, minor = possible_major, possible_minor
111
113
  else
@@ -122,18 +124,25 @@ class String
122
124
  else
123
125
  raise ArgumentError, "Invalid currency amount"
124
126
  end
125
-
127
+
126
128
  # build the string based on major/minor since separator/delimiters have been removed
127
- # transform to a float, multiply by 100 to convert to cents
128
- if with_cents and minor == 0
129
- cents = "#{major}.#{minor}".to_f
130
- else
131
- cents = "#{major}.#{minor}".to_f * 100
129
+ # avoiding floating point arithmetic here to ensure accuracy
130
+ cents = (major.to_i * 100)
131
+ # add the minor number as well. this may have any number of digits,
132
+ # so we treat minor as a string and truncate or right-fill it with zeroes
133
+ # until it becomes a two-digit number string, which we add to cents.
134
+ minor = minor.to_s
135
+ truncated_minor = minor[0..1]
136
+ truncated_minor << "0" * (2 - truncated_minor.size) if truncated_minor.size < 2
137
+ cents += truncated_minor.to_i
138
+ # respect rounding rules
139
+ if minor.size >= 3 && minor[2..2].to_i >= 5
140
+ cents += 1
132
141
  end
133
-
134
-
142
+
135
143
  # if negative, multiply by -1; otherwise, return positive cents
136
144
  negative ? cents * -1 : cents
137
145
  end
138
-
146
+
139
147
  end
148
+
data/lib/money/money.rb CHANGED
@@ -99,6 +99,7 @@ class Money
99
99
  end
100
100
 
101
101
  def <=>(other_money)
102
+ other_money = Money.new(other_money) unless other_money.is_a? Money
102
103
  if bank.same_currency?(currency, other_money.currency)
103
104
  cents <=> other_money.cents
104
105
  else
data/money.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{money}
5
- s.version = "2.3.5"
5
+ s.version = "2.3.7"
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"]
9
- s.date = %q{2009-05-29}
9
+ s.date = %q{2009-06-01}
10
10
  s.description = %q{This library aids one in handling money and different currencies.}
11
11
  s.email = %q{see@reame}
12
12
  s.extra_rdoc_files = [
@@ -44,12 +44,12 @@ Gem::Specification.new do |s|
44
44
  s.rubygems_version = %q{1.3.3}
45
45
  s.summary = %q{This library aids one in handling money and different currencies.}
46
46
  s.test_files = [
47
- "spec/money/exchange_bank_spec.rb",
47
+ "spec/spec_helper.rb",
48
+ "spec/money/exchange_bank_spec.rb",
49
+ "spec/money/core_extensions_spec.rb",
48
50
  "spec/money/acts_as_money_spec.rb",
49
51
  "spec/money/money_spec.rb",
50
- "spec/money/core_extensions_spec.rb",
51
- "spec/db/schema.rb",
52
- "spec/spec_helper.rb"
52
+ "spec/db/schema.rb"
53
53
  ]
54
54
 
55
55
  if s.respond_to? :specification_version then
@@ -15,30 +15,47 @@ describe "Money core extensions" do
15
15
  end
16
16
  end
17
17
 
18
- describe "String#to_money works" do
19
- it { "100".to_money.should == Money.new(100_00) }
20
- it { "100.37".to_money.should == Money.new(100_37) }
21
- it { "100,37".to_money.should == Money.new(100_37) }
22
- it { "100 000".to_money.should == Money.new(100_000_00) }
23
- it { "100.000,45".to_money.should == Money.new(100_000_45) }
24
- it { "-100.100,45".to_money.should == Money.new(-100_100_45) }
25
-
26
- it { "100 USD".to_money.should == Money.new(100_00, "USD") }
27
- it { "-100 USD".to_money.should == Money.new(-100_00, "USD") }
28
- it { "100 EUR".to_money.should == Money.new(100_00, "EUR") }
29
- it { "100.37 EUR".to_money.should == Money.new(100_37, "EUR") }
30
- it { "100,37 EUR".to_money.should == Money.new(100_37, "EUR") }
31
-
32
- it { "USD 100".to_money.should == Money.new(100_00, "USD") }
33
- it { "EUR 100".to_money.should == Money.new(100_00, "EUR") }
34
- it { "EUR 100.37".to_money.should == Money.new(100_37, "EUR") }
35
- it { "CAD -100.37".to_money.should == Money.new(-100_37, "CAD") }
36
- it { "EUR 100,37".to_money.should == Money.new(100_37, "EUR") }
37
- it { "EUR -100,37".to_money.should == Money.new(-100_37, "EUR") }
38
-
39
- it { "BRL 100,37".to_money.should == Money.new(100_37, "BRL") }
40
- it { "BRL -100,37".to_money.should == Money.new(-100_37, "BRL") }
41
-
42
- it {"$100 USD".to_money.should == Money.new(100_00, "USD") }
18
+ describe "String#to_money" do
19
+ EXAMPLES = {
20
+ "20.15" => {:cents => Money.new(20_15), :no_cents => Money.new(20_15)},
21
+ "100" => {:cents => Money.new(100_00), :no_cents => Money.new(100)},
22
+ "100.37" => {:cents => Money.new(100_37), :no_cents => Money.new(100_37)},
23
+ "100,37" => {:cents => Money.new(100_37), :no_cents => Money.new(100_37)},
24
+ "100 000" => {:cents => Money.new(100_000_00), :no_cents => Money.new(100_000)},
25
+ "100.000,45" => {:cents => Money.new(100_000_45), :no_cents => Money.new(100_000_45)},
26
+ "-100.100,45" => {:cents => Money.new(-100_100_45), :no_cents => Money.new(-100_100_45)},
27
+
28
+ "100 USD" => {:cents => Money.new(100_00, "USD"), :no_cents => Money.new(100, "USD")},
29
+ "-100 USD" => {:cents => Money.new(-100_00, "USD"), :no_cents => Money.new(-100, "USD")},
30
+ "100 EUR" => {:cents => Money.new(100_00, "EUR"), :no_cents => Money.new(100, "EUR")},
31
+ "100.37 EUR" => {:cents => Money.new(100_37, "EUR"), :no_cents => Money.new(100_37, "EUR")},
32
+ "100,37 EUR" => {:cents => Money.new(100_37, "EUR"), :no_cents => Money.new(100_37, "EUR")},
33
+
34
+ "USD 100" => {:cents => Money.new(100_00, "USD"), :no_cents => Money.new(100, "USD")},
35
+ "EUR 100" => {:cents => Money.new(100_00, "EUR"), :no_cents => Money.new(100, "EUR")},
36
+ "EUR 100.37" => {:cents => Money.new(100_37, "EUR"), :no_cents => Money.new(100_37, "EUR")},
37
+ "CAD -100.37" => {:cents => Money.new(-100_37, "CAD"), :no_cents => Money.new(-100_37, "CAD")},
38
+ "EUR 100,37" => {:cents => Money.new(100_37, "EUR"), :no_cents => Money.new(100_37, "EUR")},
39
+ "EUR -100,37" => {:cents => Money.new(-100_37, "EUR"), :no_cents => Money.new(-100_37, "EUR")},
40
+
41
+ "BRL 100,37" => {:cents => Money.new(100_37, "BRL"), :no_cents => Money.new(100_37, "BRL")},
42
+ "BRL -100,37" => {:cents => Money.new(-100_37, "BRL"), :no_cents => Money.new(-100_37, "BRL")},
43
+
44
+ "$100 USD" => {:cents => Money.new(100_00, "USD"), :no_cents => Money.new(100, "USD")}
45
+ }
46
+
47
+ EXAMPLES.each_pair do |string, expected_money|
48
+ it "should convert '#{string}' with cents by default" do
49
+ string.to_money.should == expected_money[:cents]
50
+ end
51
+
52
+ it "should convert '#{string}' without cents" do
53
+ string.to_money(true).should == expected_money[:no_cents]
54
+ end
55
+
56
+ it "should convert '#{string}' with cents" do
57
+ string.to_money(false).should == expected_money[:cents]
58
+ end
59
+ end
43
60
  end
44
61
  end
@@ -100,7 +100,7 @@ describe Money::ExchangeBank do
100
100
  end
101
101
 
102
102
  it "should fetch data" do
103
- Money.default_currency = "EUR"
103
+ Money.stub!(:default_currency).and_return("EUR")
104
104
  @bank.fetch_rates
105
105
  @bank.get_rate("DKK").should be_close(7.4453, 0.0001)
106
106
  @bank.get_rate("BRL").should be_close(2.832, 0.001)
@@ -108,7 +108,7 @@ describe Money::ExchangeBank do
108
108
  end
109
109
 
110
110
  it "should fetch diff than eur" do
111
- Money.default_currency = "BRL"
111
+ Money.stub!(:default_currency).and_return("BRL")
112
112
  @bank.fetch_rates
113
113
  @bank.get_rate("DKK").should be_close(2.6289, 0.0001)
114
114
  @bank.get_rate("EEK").should be_close(5.5249, 0.0001)
@@ -116,13 +116,12 @@ describe Money::ExchangeBank do
116
116
  end
117
117
 
118
118
  it "should fetch for an unknown one" do
119
- Money.default_currency = "XXX"
119
+ Money.stub!(:default_currency).and_return("XXX")
120
120
  @bank.fetch_rates
121
121
  @bank.get_rate("DKK").should be_nil
122
122
  @bank.get_rate("EUR", "USD").should be_close(1.4098, 0.001)
123
- Money.default_currency = "USD"
124
-
125
123
  end
124
+
126
125
  end
127
126
 
128
127
  describe "Live Fetching" do
@@ -158,9 +158,8 @@ describe Money do
158
158
  end
159
159
 
160
160
  it "Money.add_rate works" do
161
- Money.add_rate("USD", 1.0)
162
- Money.add_rate("EUR", 10)
163
- Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(1_00, "USD")
161
+ Money.add_rate("BGL", 10)
162
+ Money.new(10_00, "USD").exchange_to("BGL").should == Money.new(100_00, "BGL")
164
163
  end
165
164
 
166
165
  it "Money method missing exchange" do
@@ -217,6 +216,7 @@ describe Money do
217
216
  it { Money.new(800000).format.should eql("$8,000.00") }
218
217
  it { Money.new(-8000000, "JPY").format(:no_cents => true).should eql("¥-80.000") }
219
218
  it { Money.new(87654321, "BRL").format.should eql("R$876.543,21") }
219
+ it { Money.new(87654321, "brl").format.should eql("R$876.543,21") }
220
220
  it { Money.new(800000000, "BRL").format.should eql("R$8.000.000,00") }
221
221
  it { Money.new(8000000000, "BRL").format.should eql("R$80.000.000,00") }
222
222
  it { Money.new(80000000000, "CAD").format.should eql("$800,000,000.00") }
@@ -224,9 +224,8 @@ describe Money do
224
224
  it { Money.new(8800000000088, "EUR").format.should eql("€88,000,000,000.88") }
225
225
 
226
226
  it "should fail nicely if symbol can`t be found" do
227
- Money.default_currency = "XXX"
227
+ Money.stub!(:default_currency).and_return("XXX")
228
228
  Money.new(800).format.should eql("$8.00")
229
- Money.default_currency = "USD"
230
229
  end
231
230
 
232
231
  describe "Actions involving two Money objects" 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.5
4
+ version: 2.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Money Team
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-29 00:00:00 -07:00
12
+ date: 2009-06-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -73,9 +73,9 @@ signing_key:
73
73
  specification_version: 3
74
74
  summary: This library aids one in handling money and different currencies.
75
75
  test_files:
76
+ - spec/spec_helper.rb
76
77
  - spec/money/exchange_bank_spec.rb
78
+ - spec/money/core_extensions_spec.rb
77
79
  - spec/money/acts_as_money_spec.rb
78
80
  - spec/money/money_spec.rb
79
- - spec/money/core_extensions_spec.rb
80
81
  - spec/db/schema.rb
81
- - spec/spec_helper.rb