meplato-money 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/lib/money.rb +8 -7
- data/lib/money/money.rb +34 -23
- data/lib/money/rails.rb +9 -0
- metadata +25 -9
data/CHANGELOG.md
CHANGED
data/lib/money.rb
CHANGED
@@ -6,10 +6,11 @@
|
|
6
6
|
|
7
7
|
# This library is for parsing and formatting monetary values.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
require 'money/core_ext'
|
10
|
+
require 'money/currencies'
|
11
|
+
require 'money/formats'
|
12
|
+
require 'money/money_formatter'
|
13
|
+
require 'money/fixed_bank'
|
14
|
+
require 'money/meplato_bank'
|
15
|
+
require 'money/money'
|
16
|
+
require 'money/rails' if defined?(::Rails::Engine)
|
data/lib/money/money.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'money/currencies'
|
4
|
-
require 'money/formats'
|
5
3
|
|
6
4
|
module Money
|
7
5
|
|
@@ -26,23 +24,35 @@ module Money
|
|
26
24
|
|
27
25
|
attr_reader :amount, :currency, :bank
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
def initialize(amount, currency = nil, bank = nil)
|
28
|
+
@amount = amount
|
29
|
+
@currency = currency || self.class.default_currency
|
30
|
+
@bank = bank || self.class.default_bank
|
31
|
+
raise UnknownCurrency unless CURRENCIES[@currency]
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
def self.default_bank
|
35
|
+
Thread.current[:money_default_bank] || MeplatoBank.instance
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.default_bank=(bank)
|
39
|
+
Thread.current[:money_default_bank] = bank
|
37
40
|
end
|
38
41
|
|
39
|
-
self.
|
40
|
-
|
41
|
-
|
42
|
+
def self.default_currency
|
43
|
+
Thread.current[:money_default_currency] || "USD"
|
44
|
+
end
|
42
45
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
+
def self.default_currency=(currency)
|
47
|
+
Thread.current[:money_default_currency] = currency
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.default_locale
|
51
|
+
Thread.current[:money_default_locale] || "en"
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.default_locale=(locale)
|
55
|
+
Thread.current[:money_default_locale] = locale
|
46
56
|
end
|
47
57
|
|
48
58
|
def positive?
|
@@ -62,11 +72,11 @@ module Money
|
|
62
72
|
end
|
63
73
|
|
64
74
|
def ==(other)
|
65
|
-
self.amount == other.amount &&
|
75
|
+
self.amount == other.amount && self.class.same_currency?(self.currency, other.currency)
|
66
76
|
end
|
67
77
|
|
68
78
|
def <=>(other)
|
69
|
-
if
|
79
|
+
if self.class.same_currency?(@currency, other.currency)
|
70
80
|
@amount <=> other.amount
|
71
81
|
else
|
72
82
|
@amount <=> other.exchange_to(@currency).amount
|
@@ -74,7 +84,7 @@ module Money
|
|
74
84
|
end
|
75
85
|
|
76
86
|
def +(other)
|
77
|
-
if
|
87
|
+
if self.class.same_currency?(@currency, other.currency)
|
78
88
|
Money.new(@amount + other.amount, @currency)
|
79
89
|
else
|
80
90
|
Money.new(@amount + other.exchange_to(@currency).amount, @currency)
|
@@ -82,7 +92,7 @@ module Money
|
|
82
92
|
end
|
83
93
|
|
84
94
|
def -(other)
|
85
|
-
if
|
95
|
+
if self.class.same_currency?(@currency, other.currency)
|
86
96
|
Money.new(@amount - other.amount, @currency)
|
87
97
|
else
|
88
98
|
Money.new(@amount - other.exchange_to(@currency).amount, @currency)
|
@@ -90,7 +100,7 @@ module Money
|
|
90
100
|
end
|
91
101
|
|
92
102
|
def exchange_to(target)
|
93
|
-
unless
|
103
|
+
unless self.class.same_currency?(@currency, target)
|
94
104
|
rate = self.bank.get_rate(@currency, target)
|
95
105
|
if !rate
|
96
106
|
raise UnknownRate, "No conversion from #{@currency} to #{target}"
|
@@ -118,6 +128,10 @@ module Money
|
|
118
128
|
self.format(options)
|
119
129
|
end
|
120
130
|
|
131
|
+
def self.same_currency?(source, target)
|
132
|
+
source.upcase == target.upcase
|
133
|
+
end
|
134
|
+
|
121
135
|
def self.parse(value, currency, options = {})
|
122
136
|
return Money.new(0, currency, options) if value.nil?
|
123
137
|
opts = {
|
@@ -131,8 +145,5 @@ module Money
|
|
131
145
|
s.gsub!(format[:decimal_sep], '.')
|
132
146
|
Money.new(s.to_f, opts[:currency], opts)
|
133
147
|
end
|
134
|
-
|
135
|
-
private
|
136
|
-
|
137
148
|
end
|
138
149
|
end
|
data/lib/money/rails.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meplato-money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '1.0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rdoc
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '2.5'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.5'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rake
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,7 +53,12 @@ dependencies:
|
|
43
53
|
version: '0.8'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
47
62
|
description: Working with monetary values as well as performing currency exchange.
|
48
63
|
email:
|
49
64
|
- oliver.eilhard@gmail.com
|
@@ -64,6 +79,7 @@ files:
|
|
64
79
|
- lib/money/meplato_bank.rb
|
65
80
|
- lib/money/money.rb
|
66
81
|
- lib/money/money_formatter.rb
|
82
|
+
- lib/money/rails.rb
|
67
83
|
- CHANGELOG.md
|
68
84
|
- LICENSE
|
69
85
|
- README.md
|
@@ -88,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
104
|
version: 1.3.6
|
89
105
|
requirements: []
|
90
106
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.23
|
92
108
|
signing_key:
|
93
109
|
specification_version: 3
|
94
110
|
summary: Working with monetary values as well as performing currency exchange.
|