sevenwire-money 2.2.0 → 2.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.
- data/lib/money/core_extensions.rb +24 -4
- data/money.gemspec +1 -1
- data/test/core_extensions_spec.rb +35 -3
- metadata +1 -1
|
@@ -6,14 +6,32 @@ class Numeric
|
|
|
6
6
|
# 100.to_money => #<Money @cents=10000>
|
|
7
7
|
# 100.37.to_money => #<Money @cents=10037>
|
|
8
8
|
# 1000.to_money(:millicents) => #<Money @cents=1>
|
|
9
|
-
def to_money(units=:
|
|
9
|
+
def to_money(units=:dollars)
|
|
10
10
|
if units == :millicents
|
|
11
|
-
|
|
11
|
+
MicroMoney.new(self)
|
|
12
|
+
elsif units == :cents
|
|
13
|
+
Money.new(self)
|
|
12
14
|
else
|
|
13
15
|
Money.new(self * 100)
|
|
14
16
|
end
|
|
15
17
|
end
|
|
16
18
|
|
|
19
|
+
|
|
20
|
+
def millicents
|
|
21
|
+
MicroMoney.new(self).to_money
|
|
22
|
+
end
|
|
23
|
+
alias :millicent :millicents
|
|
24
|
+
|
|
25
|
+
def cents
|
|
26
|
+
Money.new(self)
|
|
27
|
+
end
|
|
28
|
+
alias :cent :cents
|
|
29
|
+
|
|
30
|
+
def dollars
|
|
31
|
+
Money.new(self * 100)
|
|
32
|
+
end
|
|
33
|
+
alias :dollar :dollars
|
|
34
|
+
|
|
17
35
|
end
|
|
18
36
|
|
|
19
37
|
|
|
@@ -28,7 +46,7 @@ class String
|
|
|
28
46
|
# 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD">
|
|
29
47
|
# '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
|
|
30
48
|
# '1000'.to_money(:millicents) # => #<Money @cents=1, @currency="USD">
|
|
31
|
-
def to_money(units=:
|
|
49
|
+
def to_money(units=:dollars)
|
|
32
50
|
# Get the currency.
|
|
33
51
|
matches = scan /([A-Z]{2,3})/
|
|
34
52
|
currency = matches[0] ? matches[0][0] : Money.default_currency
|
|
@@ -44,7 +62,9 @@ class String
|
|
|
44
62
|
end
|
|
45
63
|
|
|
46
64
|
if units == :millicents
|
|
47
|
-
|
|
65
|
+
MicroMoney.new(money, currency)
|
|
66
|
+
elsif units == :cents
|
|
67
|
+
Money.new(money, currency)
|
|
48
68
|
else
|
|
49
69
|
Money.new(money * 100, currency)
|
|
50
70
|
end
|
data/money.gemspec
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../lib")
|
|
2
2
|
require 'money/core_extensions'
|
|
3
|
+
require 'money/variable_exchange_bank'
|
|
4
|
+
require 'money/money'
|
|
5
|
+
require 'money/micro_money'
|
|
3
6
|
|
|
4
|
-
describe "
|
|
7
|
+
describe "to_money core extensions for" do
|
|
5
8
|
|
|
6
|
-
specify "Numeric
|
|
9
|
+
specify "Numeric works" do
|
|
7
10
|
money = 1234.to_money
|
|
8
11
|
money.cents.should == 1234_00
|
|
9
12
|
money.currency.should == Money.default_currency
|
|
@@ -13,7 +16,7 @@ describe "Money core extensions" do
|
|
|
13
16
|
money.currency.should == Money.default_currency
|
|
14
17
|
end
|
|
15
18
|
|
|
16
|
-
specify "String
|
|
19
|
+
specify "String works" do
|
|
17
20
|
"100".to_money.should == Money.new(100_00)
|
|
18
21
|
"100.37".to_money.should == Money.new(100_37)
|
|
19
22
|
"100,37".to_money.should == Money.new(100_37)
|
|
@@ -36,3 +39,32 @@ describe "Money core extensions" do
|
|
|
36
39
|
end
|
|
37
40
|
|
|
38
41
|
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
describe "Convenience methods on Numeric and String should return" do
|
|
45
|
+
|
|
46
|
+
specify "1.millicent" do
|
|
47
|
+
1.millicent.should == MicroMoney.new(1)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
specify "100.millcents" do
|
|
51
|
+
100.millicents.should == MicroMoney.new(100)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
specify "1.cent" do
|
|
55
|
+
1.cent.should == Money.new(1)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
specify "100.cents" do
|
|
59
|
+
100.cents.should == Money.new(100)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
specify "1.dollar" do
|
|
63
|
+
1.dollar.should == Money.new(100)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
specify "100.dollars" do
|
|
67
|
+
100.dollars.should == Money.new(10000)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|