easy_rails_money 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -5
- data/CHANGELOG.md +3 -2
- data/README.md +114 -20
- data/lib/easy_rails_money/active_record/money_dsl.rb +85 -5
- data/lib/easy_rails_money/configuration.rb +6 -1
- data/lib/easy_rails_money/money_dsl_helper.rb +9 -0
- data/lib/easy_rails_money/version.rb +1 -1
- data/spec/active_record/migration_spec.rb +235 -271
- data/spec/active_record/money_dsl_spec.rb +207 -5
- data/spec/active_record_spec_helper.rb +29 -0
- data/spec/configuration_spec.rb +30 -13
- data/spec/loan_model_spec_helper.rb +6 -0
- data/spec/loan_with_currency_model_spec_helper.rb +10 -0
- metadata +9 -4
@@ -1,11 +1,213 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'active_record_spec_helper'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
describe "Money DSL" do
|
5
|
+
subject {
|
6
|
+
require 'loan_model_spec_helper'
|
7
|
+
Loan.new
|
8
|
+
}
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
currencies = [:inr, # Indian rupee
|
12
|
+
:usd, # USA dollars
|
13
|
+
:aud, # Australian dollar
|
14
|
+
:eur, # Euro
|
15
|
+
:gbp # British pound
|
16
|
+
]
|
17
|
+
|
18
|
+
# TODO: how to couple this choice with rspec seed
|
19
|
+
# take a random currency so that we do not depend on any one
|
20
|
+
# default_currency while testing
|
21
|
+
EasyRailsMoney.configure do |c|
|
22
|
+
c.default_currency = Money::Currency.new(currencies.shuffle[0])
|
9
23
|
end
|
10
24
|
end
|
25
|
+
|
26
|
+
describe "#money" do
|
27
|
+
# validations
|
28
|
+
|
29
|
+
context "individual currency columns" do
|
30
|
+
before(:each) do
|
31
|
+
migrate CreateTableDefinition::CreateLoanWithoutCurrency
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#getter" do
|
35
|
+
it "defines a getter" do
|
36
|
+
expect(subject).to respond_to(:principal)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "getter returns nil if nothing is set" do
|
40
|
+
expect(subject.principal).to eq nil
|
41
|
+
expect(subject.principal).to_not eq Money.new(0)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "gets the same value as set" do
|
45
|
+
money = Money.new(100, "INR")
|
46
|
+
expect { subject.principal = money }.to change { subject.principal }.
|
47
|
+
from(nil).
|
48
|
+
to(money)
|
49
|
+
end
|
50
|
+
end # describe "#getter"
|
51
|
+
|
52
|
+
describe "#setter=" do
|
53
|
+
it "defines a setter" do
|
54
|
+
expect(subject).to respond_to(:principal=).with(1).argument
|
55
|
+
end
|
56
|
+
|
57
|
+
it "throws an error if we try to set anything other than a Money object or nil" do
|
58
|
+
expect { subject.principal = nil }.to_not raise_error
|
59
|
+
expect { subject.principal = Money.new(100) }.to_not raise_error
|
60
|
+
expect { subject.principal = Money.new(100, :usd) }.to_not raise_error
|
61
|
+
|
62
|
+
expect { subject.principal = "100" }.to raise_error(ArgumentError)
|
63
|
+
expect { subject.principal = 100.10 }.to raise_error(ArgumentError)
|
64
|
+
expect { subject.principal = 100 }.to raise_error(ArgumentError)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns the same value which is set" do
|
68
|
+
money = Money.new(100, "inr")
|
69
|
+
expect(subject.principal = money).to eq money
|
70
|
+
end
|
71
|
+
|
72
|
+
it "returns a value with the default currency when not given" do
|
73
|
+
expect(subject.principal = Money.new(100)).to eq Money.new(100, EasyRailsMoney.default_currency)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "can set a value with a different currency" do
|
77
|
+
money = Money.new(100, "JPY")
|
78
|
+
expect { subject.principal = money }.to change { subject.principal }.
|
79
|
+
from(nil).
|
80
|
+
to(money)
|
81
|
+
end
|
82
|
+
|
83
|
+
context "actually sets the lower-level database columns" do
|
84
|
+
it "sets the money column" do
|
85
|
+
expect { subject.principal = Money.new(100, :inr) }.to change { subject.principal_money }.
|
86
|
+
from(nil).
|
87
|
+
to(100)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "sets the money column when nil" do
|
91
|
+
subject.principal = nil
|
92
|
+
expect(subject.principal_money).to eq nil
|
93
|
+
end
|
94
|
+
|
95
|
+
it "sets the currency column" do
|
96
|
+
expect { subject.principal = Money.new(100, :inr) }.to change { subject.principal_currency }.
|
97
|
+
from(nil).
|
98
|
+
to("inr")
|
99
|
+
end
|
100
|
+
|
101
|
+
it "sets the currency column when nil" do
|
102
|
+
subject.principal = nil
|
103
|
+
expect(subject.principal_currency).to eq nil
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when nil is set" do
|
108
|
+
it "can set to nil" do
|
109
|
+
subject.principal = nil
|
110
|
+
expect(subject.principal).to eq nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it "cannot set an integer value" do
|
115
|
+
expect { subject.principal = 100 }.to raise_error
|
116
|
+
end
|
117
|
+
end # describe "#setter="
|
118
|
+
end # context "individual currency columns"
|
119
|
+
|
120
|
+
context "single currency", :single_currency do
|
121
|
+
subject {
|
122
|
+
require 'loan_with_currency_model_spec_helper'
|
123
|
+
LoanWithCurrency.new
|
124
|
+
}
|
125
|
+
|
126
|
+
before(:each) do
|
127
|
+
migrate CreateTableDefinition::CreateLoanWithCurrency
|
128
|
+
end
|
129
|
+
|
130
|
+
it "sets currency column" do
|
131
|
+
expect(subject.currency).to eq ::Money::Currency.new(:inr)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "can change single currency on an instance" do
|
135
|
+
loan = LoanWithCurrency.new(currency: :usd)
|
136
|
+
expect(loan.currency).to eq ::Money::Currency.new(:usd)
|
137
|
+
expect(loan.class.single_currency).to eq ::Money::Currency.new(:inr)
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#getter" do
|
141
|
+
it "defines a getter" do
|
142
|
+
expect(subject).to respond_to(:principal)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "getter returns nil if nothing is set" do
|
146
|
+
expect(subject.principal).to eq nil
|
147
|
+
expect(subject.principal).to_not eq Money.new(0)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "gets the same value as set" do
|
151
|
+
money = Money.new(100, "INR")
|
152
|
+
expect { subject.principal = 100 }.to change { subject.principal }.
|
153
|
+
from(nil).
|
154
|
+
to(money)
|
155
|
+
end
|
156
|
+
end # describe "#getter"
|
157
|
+
|
158
|
+
describe "#setter=" do
|
159
|
+
it "defines a setter" do
|
160
|
+
expect(subject).to respond_to(:principal=).with(1).argument
|
161
|
+
end
|
162
|
+
|
163
|
+
it "throws an error if we try to set anything other than a Integer object or nil" do
|
164
|
+
expect { subject.principal = nil }.to_not raise_error
|
165
|
+
expect { subject.principal = 100 }.to_not raise_error
|
166
|
+
|
167
|
+
expect { subject.principal = Money.new(100, "INR") }.to raise_error
|
168
|
+
expect { subject.principal = "100" }.to raise_error(ArgumentError)
|
169
|
+
expect { subject.principal = 100.10 }.to raise_error(ArgumentError)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "returns the same value which is set" do
|
173
|
+
expect(subject.principal = 100).to eq 100
|
174
|
+
end
|
175
|
+
|
176
|
+
context "actually sets the lower-level database columns" do
|
177
|
+
it "sets the money column" do
|
178
|
+
expect { subject.principal = 100 }.to change { subject.principal_money }.
|
179
|
+
from(nil).
|
180
|
+
to(100)
|
181
|
+
end
|
182
|
+
|
183
|
+
it "sets the money column when nil" do
|
184
|
+
subject.principal = nil
|
185
|
+
expect(subject.principal_money).to eq nil
|
186
|
+
end
|
187
|
+
|
188
|
+
it "does not set the currency column as it is a common column" do
|
189
|
+
expect { subject.principal = 100 }.not_to change { subject.currency }
|
190
|
+
end
|
191
|
+
|
192
|
+
it "does not set the currency column when nil as it is a common column" do
|
193
|
+
expect { subject.principal = nil }.not_to change { subject.currency }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context "when nil is set" do
|
198
|
+
it "can set to nil" do
|
199
|
+
subject.principal = nil
|
200
|
+
expect(subject.principal).to eq nil
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end # describe "#setter="
|
204
|
+
|
205
|
+
end # context "single currency"
|
206
|
+
|
207
|
+
pending "validations"
|
208
|
+
pending "support defining multiple fields at once"
|
209
|
+
pending "test the return value of #money. pretty useless though"
|
210
|
+
pending "patch create and attributes like #new"
|
211
|
+
|
212
|
+
end # describe "#money"
|
11
213
|
end
|
@@ -1,3 +1,32 @@
|
|
1
|
+
module CreateTableDefinition
|
2
|
+
class CreateLoanWithCurrency < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
suppress_messages do
|
5
|
+
create_table :loans, force: true do |t|
|
6
|
+
t.string :name
|
7
|
+
t.money :principal
|
8
|
+
t.money :repaid
|
9
|
+
t.money :npa
|
10
|
+
t.currency
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class CreateLoanWithoutCurrency < ActiveRecord::Migration
|
17
|
+
def change
|
18
|
+
suppress_messages do
|
19
|
+
create_table :loans, force: true do |t|
|
20
|
+
t.string :name
|
21
|
+
t.money :principal
|
22
|
+
t.money :repaid
|
23
|
+
t.money :npa
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
1
30
|
class String
|
2
31
|
def strip_spaces
|
3
32
|
strip.gsub(/\s+/, ' ')
|
data/spec/configuration_spec.rb
CHANGED
@@ -6,22 +6,39 @@ describe "Configuration" do
|
|
6
6
|
let(:existing_currency) { Money::Currency.new(:usd) }
|
7
7
|
let(:new_currency) { Money::Currency.new(:inr) }
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
it "has a default currency" do
|
10
|
+
expect(EasyRailsMoney.default_currency).not_to be_nil
|
11
11
|
end
|
12
|
-
|
13
|
-
it "
|
14
|
-
expect
|
15
|
-
to change { Money.default_currency }.
|
16
|
-
from(existing_currency).
|
17
|
-
to(new_currency)
|
12
|
+
|
13
|
+
it "default currency is equal to money's default_currency" do
|
14
|
+
expect(EasyRailsMoney.default_currency).to eq Money.default_currency
|
18
15
|
end
|
16
|
+
|
17
|
+
context "can set and read back the config" do
|
18
|
+
before(:each) do
|
19
|
+
Money.default_currency = existing_currency
|
20
|
+
end
|
21
|
+
|
22
|
+
it "sets default currency" do
|
23
|
+
expect { EasyRailsMoney.default_currency = new_currency }.
|
24
|
+
to change { Money.default_currency }.
|
25
|
+
from(existing_currency).
|
26
|
+
to(new_currency)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "reads default currency" do
|
30
|
+
expect { EasyRailsMoney.default_currency = new_currency }.
|
31
|
+
to change { EasyRailsMoney.default_currency }.
|
32
|
+
from(existing_currency).
|
33
|
+
to(new_currency)
|
34
|
+
end
|
19
35
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
36
|
+
it "reads back a currency object even when a symbol is set" do
|
37
|
+
expect { EasyRailsMoney.default_currency = new_currency.id }.
|
38
|
+
to change { EasyRailsMoney.default_currency }.
|
39
|
+
from(existing_currency).
|
40
|
+
to(new_currency)
|
41
|
+
end
|
25
42
|
end
|
26
43
|
end
|
27
44
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_rails_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|
@@ -194,11 +194,14 @@ files:
|
|
194
194
|
- lib/easy_rails_money/active_record/migration/table_definition.rb
|
195
195
|
- lib/easy_rails_money/active_record/money_dsl.rb
|
196
196
|
- lib/easy_rails_money/configuration.rb
|
197
|
+
- lib/easy_rails_money/money_dsl_helper.rb
|
197
198
|
- lib/easy_rails_money/version.rb
|
198
199
|
- spec/active_record/migration_spec.rb
|
199
200
|
- spec/active_record/money_dsl_spec.rb
|
200
201
|
- spec/active_record_spec_helper.rb
|
201
202
|
- spec/configuration_spec.rb
|
203
|
+
- spec/loan_model_spec_helper.rb
|
204
|
+
- spec/loan_with_currency_model_spec_helper.rb
|
202
205
|
- spec/simplecov_helper.rb
|
203
206
|
- spec/spec_helper.rb
|
204
207
|
- vendor/cache/activemodel-3.2.12.gem
|
@@ -239,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
239
242
|
version: '0'
|
240
243
|
segments:
|
241
244
|
- 0
|
242
|
-
hash: -
|
245
|
+
hash: -2090962941235077945
|
243
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
244
247
|
none: false
|
245
248
|
requirements:
|
@@ -248,7 +251,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
248
251
|
version: '0'
|
249
252
|
segments:
|
250
253
|
- 0
|
251
|
-
hash: -
|
254
|
+
hash: -2090962941235077945
|
252
255
|
requirements: []
|
253
256
|
rubyforge_project:
|
254
257
|
rubygems_version: 1.8.24
|
@@ -260,6 +263,8 @@ test_files:
|
|
260
263
|
- spec/active_record/money_dsl_spec.rb
|
261
264
|
- spec/active_record_spec_helper.rb
|
262
265
|
- spec/configuration_spec.rb
|
266
|
+
- spec/loan_model_spec_helper.rb
|
267
|
+
- spec/loan_with_currency_model_spec_helper.rb
|
263
268
|
- spec/simplecov_helper.rb
|
264
269
|
- spec/spec_helper.rb
|
265
270
|
has_rdoc:
|