memory_model 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -4
- data/Appraisals +9 -0
- data/Guardfile +7 -1
- data/README.md +5 -1
- data/Rakefile +12 -0
- data/gemfiles/rails_3.gemfile +8 -0
- data/gemfiles/rails_3.gemfile.lock +109 -0
- data/gemfiles/rails_4.gemfile +8 -0
- data/gemfiles/rails_4.gemfile.lock +117 -0
- data/lib/memory_model/base/actions/class_methods.rb +27 -0
- data/lib/memory_model/base/actions.rb +75 -0
- data/lib/memory_model/base/attributes.rb +87 -0
- data/lib/memory_model/base/auto_increment.rb +47 -0
- data/lib/memory_model/base/collectible.rb +26 -0
- data/lib/memory_model/base/conversion.rb +11 -0
- data/lib/memory_model/base/fields/field.rb +57 -0
- data/lib/memory_model/base/fields/field_set.rb +87 -0
- data/lib/memory_model/base/fields.rb +49 -0
- data/lib/memory_model/base/operations/comparisons.rb +25 -0
- data/lib/memory_model/base/operations.rb +10 -0
- data/lib/memory_model/base/persistence.rb +13 -11
- data/lib/memory_model/base.rb +58 -43
- data/lib/memory_model/collection/finders.rb +75 -0
- data/lib/memory_model/collection/index/multi.rb +61 -0
- data/lib/memory_model/collection/index/unique.rb +79 -0
- data/lib/memory_model/collection/index.rb +86 -0
- data/lib/memory_model/collection/initializers.rb +48 -0
- data/lib/memory_model/collection/loader_delegate.rb +63 -0
- data/lib/memory_model/collection/marshaled_record.rb +23 -0
- data/lib/memory_model/collection/operations.rb +82 -0
- data/lib/memory_model/collection.rb +18 -73
- data/lib/memory_model/version.rb +1 -1
- data/lib/memory_model.rb +7 -7
- data/memory_model.gemspec +8 -3
- data/spec/benchmark/benchmark.rb +126 -0
- data/spec/memory_model/base/{actionable_spec.rb → actions_spec.rb} +34 -103
- data/spec/memory_model/base/{attributable_spec.rb → attributes_spec.rb} +4 -6
- data/spec/memory_model/base/{collectable_spec.rb → collectible_spec.rb} +1 -1
- data/spec/memory_model/base/fieldable/field_set_spec.rb +23 -37
- data/spec/memory_model/base/fieldable/field_spec.rb +16 -16
- data/spec/memory_model/base/{fieldable_spec.rb → fields_spec.rb} +1 -1
- data/spec/memory_model/base/{comparable_spec.rb → operations/comparisons_spec.rb} +4 -4
- data/spec/memory_model/base/persistence_spec.rb +2 -2
- data/spec/memory_model/base_spec.rb +10 -9
- data/spec/memory_model/collection_spec.rb +24 -146
- data/spec/spec_helper.rb +11 -0
- data/spec/support/delegate_matcher.rb +40 -0
- metadata +120 -65
- data/.idea/.rakeTasks +0 -7
- data/.idea/dictionaries/jwaldrip.xml +0 -3
- data/.idea/encodings.xml +0 -5
- data/.idea/memory_model.iml +0 -47
- data/.idea/misc.xml +0 -8
- data/.idea/modules.xml +0 -9
- data/.idea/scopes/scope_settings.xml +0 -5
- data/.idea/vcs.xml +0 -7
- data/.idea/workspace.xml +0 -701
- data/lib/memory_model/base/actionable.rb +0 -95
- data/lib/memory_model/base/attributable.rb +0 -76
- data/lib/memory_model/base/collectable.rb +0 -22
- data/lib/memory_model/base/comparable.rb +0 -16
- data/lib/memory_model/base/fieldable/field.rb +0 -35
- data/lib/memory_model/base/fieldable/field_set.rb +0 -74
- data/lib/memory_model/base/fieldable.rb +0 -45
- data/lib/memory_model/base/versionable.rb +0 -17
- data/lib/memory_model/core_ext/object.rb +0 -5
- data/spec/memory_model/base/versionable_spec.rb +0 -31
- data/spec/memory_model/core_ext/object_spec.rb +0 -12
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MemoryModel::Base::
|
3
|
+
describe MemoryModel::Base::Actions do
|
4
4
|
let(:model) do
|
5
5
|
Class.new(MemoryModel::Base) do
|
6
|
+
set_primary_key :id
|
6
7
|
field :foo
|
7
8
|
end
|
8
9
|
end
|
@@ -14,8 +15,8 @@ describe MemoryModel::Base::Actionable do
|
|
14
15
|
|
15
16
|
describe '.create' do
|
16
17
|
it 'should be persisted' do
|
17
|
-
|
18
|
-
|
18
|
+
instance = model.create
|
19
|
+
instance.should be_persisted
|
19
20
|
end
|
20
21
|
|
21
22
|
it 'should call the save method' do
|
@@ -26,12 +27,9 @@ describe MemoryModel::Base::Actionable do
|
|
26
27
|
|
27
28
|
describe '.delete_all' do
|
28
29
|
it 'should call delete on each item' do
|
29
|
-
collection_mock = 10.times.map {
|
30
|
-
|
31
|
-
model.
|
32
|
-
instance.should_receive(:delete).and_return(instance)
|
33
|
-
instance.should_receive(:deleted?).and_return(true)
|
34
|
-
end
|
30
|
+
collection_mock = 10.times.map { double }
|
31
|
+
collection_mock.should_receive(:clear)
|
32
|
+
allow(model).to receive(:collection).and_return(collection_mock)
|
35
33
|
model.delete_all
|
36
34
|
end
|
37
35
|
it 'should return true' do
|
@@ -43,11 +41,10 @@ describe MemoryModel::Base::Actionable do
|
|
43
41
|
|
44
42
|
describe '.destroy_all' do
|
45
43
|
it 'should call delete on each item' do
|
46
|
-
collection_mock = 10.times.map {
|
47
|
-
model.
|
44
|
+
collection_mock = 10.times.map { double }
|
45
|
+
allow(model).to receive(:all).and_return(collection_mock)
|
48
46
|
model.all.each do |instance|
|
49
47
|
instance.should_receive(:destroy).and_return(instance)
|
50
|
-
instance.should_receive(:deleted?).and_return(true)
|
51
48
|
end
|
52
49
|
model.destroy_all
|
53
50
|
end
|
@@ -60,44 +57,13 @@ describe MemoryModel::Base::Actionable do
|
|
60
57
|
|
61
58
|
describe '#commit' do
|
62
59
|
it 'should save to the collection' do
|
63
|
-
expect { instance.commit }.to change { model.all }
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should always be the latest record' do
|
67
|
-
instance.commit
|
68
|
-
instance.commit.timestamp.should == model.find(instance.id).timestamp
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should have a timestamp' do
|
72
|
-
instance.commit
|
73
|
-
instance.timestamp.should be_present
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'should have unfrozen attributes' do
|
77
|
-
instance.commit
|
78
|
-
instance.instance_variable_get(:@attributes).should_not be_frozen
|
60
|
+
expect { instance.send(:commit) }.to change { model.all.to_a }
|
79
61
|
end
|
80
62
|
end
|
81
63
|
|
82
64
|
describe '#delete' do
|
83
65
|
it 'should be frozen' do
|
84
|
-
instance.commit.delete.should be_frozen
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
describe '#deleted_at' do
|
89
|
-
context 'when deleted' do
|
90
|
-
it 'should have a timestamp' do
|
91
|
-
deleted_instance = instance.commit.delete
|
92
|
-
instance.deleted_at.should == deleted_instance.timestamp
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context 'when not deleted' do
|
97
|
-
it 'should be nil' do
|
98
|
-
instance.commit
|
99
|
-
instance.deleted_at.should be_nil
|
100
|
-
end
|
66
|
+
instance.send(:commit).delete.should be_frozen
|
101
67
|
end
|
102
68
|
end
|
103
69
|
|
@@ -152,59 +118,23 @@ describe MemoryModel::Base::Actionable do
|
|
152
118
|
end
|
153
119
|
end
|
154
120
|
|
155
|
-
describe '#dup' do
|
156
|
-
it 'should perform a deep_dup' do
|
157
|
-
instance.should_receive(:deep_dup)
|
158
|
-
instance.dup
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
describe '#deep_dup' do
|
163
|
-
it 'should not be frozen' do
|
164
|
-
dup = instance.freeze.deep_dup
|
165
|
-
instance.should be_frozen
|
166
|
-
dup.should_not be_frozen
|
167
|
-
end
|
168
|
-
|
169
|
-
it 'should return a new object' do
|
170
|
-
dup = instance.deep_dup
|
171
|
-
dup.object_id.should_not == instance.object_id
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe '#freeze' do
|
176
|
-
it 'should remove invalid ivars' do
|
177
|
-
ivar = :@foo
|
178
|
-
instance.instance_variable_set ivar, 'bar'
|
179
|
-
instance.freeze
|
180
|
-
instance.instance_variables.should_not include ivar
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'should be frozen' do
|
184
|
-
instance.freeze
|
185
|
-
instance.should be_frozen
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
describe '#restore' do
|
190
|
-
it 'should not be deleted' do
|
191
|
-
instance.commit.delete
|
192
|
-
restored_instance = instance.restore
|
193
|
-
restored_instance.should_not be_deleted
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
121
|
describe '#save' do
|
198
|
-
it 'should call
|
122
|
+
it 'should call commit' do
|
199
123
|
instance.should_receive(:commit)
|
200
124
|
instance.save
|
201
125
|
end
|
126
|
+
end
|
202
127
|
|
128
|
+
describe '#save!' do
|
129
|
+
pending
|
130
|
+
end
|
131
|
+
|
132
|
+
describe 'ActiveModel::Callbacks' do
|
203
133
|
context 'with a before_save callback' do
|
204
134
|
it 'should run the callback' do
|
205
135
|
model.before_save :test_method
|
206
136
|
instance.should_receive(:test_method) do
|
207
|
-
|
137
|
+
expect(model.collection).to receive(:transact)
|
208
138
|
end
|
209
139
|
instance.save
|
210
140
|
end
|
@@ -212,7 +142,7 @@ describe MemoryModel::Base::Actionable do
|
|
212
142
|
it 'should execution if the callback returns false' do
|
213
143
|
model.before_save :test_method
|
214
144
|
instance.should_receive(:test_method).and_return(false)
|
215
|
-
|
145
|
+
expect(model.collection).to_not receive(:transact)
|
216
146
|
instance.save
|
217
147
|
end
|
218
148
|
end
|
@@ -220,8 +150,8 @@ describe MemoryModel::Base::Actionable do
|
|
220
150
|
context 'with an after_save callback' do
|
221
151
|
it 'should run the callback' do
|
222
152
|
model.after_save :test_method
|
223
|
-
|
224
|
-
instance.
|
153
|
+
expect(model.collection).to receive(:transact) do
|
154
|
+
expect(instance).to receive(:test_method)
|
225
155
|
end
|
226
156
|
instance.save
|
227
157
|
end
|
@@ -236,7 +166,7 @@ describe MemoryModel::Base::Actionable do
|
|
236
166
|
test_method_b
|
237
167
|
end
|
238
168
|
instance.should_receive(:test_method_a) do
|
239
|
-
|
169
|
+
expect(model.collection).to receive(:transact) do
|
240
170
|
instance.should_receive(:test_method_b)
|
241
171
|
end
|
242
172
|
end
|
@@ -249,7 +179,7 @@ describe MemoryModel::Base::Actionable do
|
|
249
179
|
it 'should run the callback' do
|
250
180
|
model.before_create :test_method
|
251
181
|
instance.should_receive(:test_method) do
|
252
|
-
|
182
|
+
expect(model.collection).to receive(:transact)
|
253
183
|
end
|
254
184
|
instance.save
|
255
185
|
end
|
@@ -257,7 +187,7 @@ describe MemoryModel::Base::Actionable do
|
|
257
187
|
it 'should execution if the callback returns false' do
|
258
188
|
model.before_create :test_method
|
259
189
|
instance.should_receive(:test_method).and_return(false)
|
260
|
-
instance.should_not_receive(:
|
190
|
+
instance.class.should_not_receive(:transact)
|
261
191
|
instance.save
|
262
192
|
end
|
263
193
|
end
|
@@ -265,7 +195,7 @@ describe MemoryModel::Base::Actionable do
|
|
265
195
|
context 'with an after_create callback' do
|
266
196
|
it 'should run the callback' do
|
267
197
|
model.after_create :test_method
|
268
|
-
|
198
|
+
expect(model.collection).to receive(:transact) do
|
269
199
|
instance.should_receive(:test_method)
|
270
200
|
end
|
271
201
|
instance.save
|
@@ -281,7 +211,7 @@ describe MemoryModel::Base::Actionable do
|
|
281
211
|
test_method_b
|
282
212
|
end
|
283
213
|
instance.should_receive(:test_method_a) do
|
284
|
-
|
214
|
+
expect(model.collection).to receive(:transact) do
|
285
215
|
instance.should_receive(:test_method_b)
|
286
216
|
end
|
287
217
|
end
|
@@ -300,12 +230,12 @@ describe MemoryModel::Base::Actionable do
|
|
300
230
|
end
|
301
231
|
|
302
232
|
context 'with an existing record' do
|
303
|
-
before(:each) { instance.
|
233
|
+
before(:each) { instance.save }
|
304
234
|
context 'with a before_update callback' do
|
305
235
|
it 'should run the callback' do
|
306
236
|
model.before_update :test_method
|
307
237
|
instance.should_receive(:test_method) do
|
308
|
-
|
238
|
+
expect(model.collection).to receive(:transact)
|
309
239
|
end
|
310
240
|
instance.save
|
311
241
|
end
|
@@ -313,7 +243,7 @@ describe MemoryModel::Base::Actionable do
|
|
313
243
|
it 'should execution if the callback returns false' do
|
314
244
|
model.before_update :test_method
|
315
245
|
instance.should_receive(:test_method).and_return(false)
|
316
|
-
instance.should_not_receive(:
|
246
|
+
instance.class.should_not_receive(:transact)
|
317
247
|
instance.save
|
318
248
|
end
|
319
249
|
end
|
@@ -321,7 +251,7 @@ describe MemoryModel::Base::Actionable do
|
|
321
251
|
context 'with an after_update callback' do
|
322
252
|
it 'should run the callback' do
|
323
253
|
model.after_update :test_method
|
324
|
-
|
254
|
+
expect(model.collection).to receive(:transact) do
|
325
255
|
instance.should_receive(:test_method)
|
326
256
|
end
|
327
257
|
instance.save
|
@@ -337,7 +267,7 @@ describe MemoryModel::Base::Actionable do
|
|
337
267
|
test_method_b
|
338
268
|
end
|
339
269
|
instance.should_receive(:test_method_a) do
|
340
|
-
|
270
|
+
expect(model.collection).to receive(:transact) do
|
341
271
|
instance.should_receive(:test_method_b)
|
342
272
|
end
|
343
273
|
end
|
@@ -348,11 +278,12 @@ describe MemoryModel::Base::Actionable do
|
|
348
278
|
context 'it should not call an create callback' do
|
349
279
|
it 'should run the callback' do
|
350
280
|
model.before_create :test_method
|
351
|
-
|
281
|
+
expect(model.collection).to receive(:transact)
|
352
282
|
instance.should_not_receive(:test_method)
|
353
283
|
instance.save
|
354
284
|
end
|
355
285
|
end
|
286
|
+
|
356
287
|
end
|
357
288
|
end
|
358
289
|
|
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MemoryModel::Base::
|
3
|
+
describe MemoryModel::Base::Attributes do
|
4
4
|
|
5
5
|
let(:model) do
|
6
|
-
Class.new(MemoryModel::Base) do
|
6
|
+
klass = Class.new(MemoryModel::Base) do
|
7
7
|
field :foo
|
8
8
|
field :bar
|
9
9
|
end
|
10
|
+
stub_const 'MyModel', klass
|
10
11
|
end
|
11
12
|
let(:instance) do
|
12
13
|
model.new
|
@@ -48,7 +49,7 @@ describe MemoryModel::Base::Attributable do
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
51
|
-
context 'given an
|
52
|
+
context 'given an empty Hash' do
|
52
53
|
it 'should be true' do
|
53
54
|
instance.foo = {}
|
54
55
|
instance.has_attribute?(:foo).should be_false
|
@@ -68,9 +69,6 @@ describe MemoryModel::Base::Attributable do
|
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
71
|
-
it 'should read not initialized' do
|
72
|
-
model.allocate.inspect.should match /not initialized/
|
73
|
-
end
|
74
72
|
end
|
75
73
|
|
76
74
|
describe '#read_attribute' do
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MemoryModel::Base::
|
3
|
+
describe MemoryModel::Base::Fields::FieldSet do
|
4
4
|
|
5
|
-
let(:klass) { MemoryModel::Base::
|
5
|
+
let(:klass) { MemoryModel::Base::Fields::FieldSet }
|
6
6
|
subject(:field_set) { klass.new }
|
7
7
|
|
8
8
|
describe '.new' do
|
9
|
-
it "should have an empty
|
10
|
-
field_set.
|
9
|
+
it "should have an empty set of fields" do
|
10
|
+
field_set.should be_a Set
|
11
11
|
field_set.size.should == 0
|
12
12
|
end
|
13
13
|
end
|
@@ -20,20 +20,14 @@ describe MemoryModel::Base::Fieldable::FieldSet do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
describe '#<<' do
|
24
|
-
it 'should add a field with the symbol' do
|
25
|
-
expect { field_set << :foo }.to change { field_set.fields }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
23
|
describe '#add' do
|
30
24
|
it "should add a field" do
|
31
|
-
expect { field_set.add(:foo) }.to change { field_set
|
25
|
+
expect { field_set.add(:foo) }.to change { field_set }
|
32
26
|
end
|
33
27
|
|
34
28
|
it "should add a field with options" do
|
35
29
|
options = { foo: :bar }
|
36
|
-
expect { field_set.add(:foo, options) }.to change { field_set
|
30
|
+
expect { field_set.add(:foo, options) }.to change { field_set }
|
37
31
|
field_set[:foo].options[:foo].should == :bar
|
38
32
|
end
|
39
33
|
end
|
@@ -48,30 +42,22 @@ describe MemoryModel::Base::Fieldable::FieldSet do
|
|
48
42
|
end
|
49
43
|
end
|
50
44
|
|
51
|
-
describe '#
|
52
|
-
|
53
|
-
|
54
|
-
names_mock.should_receive(:inspect)
|
55
|
-
field_set.stub(:to_a).and_return(names_mock)
|
56
|
-
field_set.inspect
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe '#default_values' do
|
61
|
-
let(:mock_model) { mock }
|
45
|
+
describe '#set_default_values' do
|
46
|
+
let(:mock_model) { double }
|
47
|
+
before { allow(mock_model).to receive(:attributes=) { |hash| hash.with_indifferent_access } }
|
62
48
|
|
63
49
|
context 'with a symbol' do
|
64
50
|
it 'should call the method on the model' do
|
65
|
-
mock_model.should_receive
|
51
|
+
mock_model.should_receive(:foo_val).and_return('some_value')
|
66
52
|
field_set.add :foo, default: :foo_val
|
67
|
-
field_set.
|
53
|
+
field_set.set_default_values(mock_model)
|
68
54
|
end
|
69
55
|
end
|
70
56
|
|
71
57
|
context 'with a string' do
|
72
58
|
it 'should set the string' do
|
73
59
|
field_set.add :foo, default: 'foo_val'
|
74
|
-
defaults = field_set.
|
60
|
+
defaults = field_set.set_default_values(mock_model)
|
75
61
|
defaults[:foo].should == 'foo_val'
|
76
62
|
end
|
77
63
|
end
|
@@ -79,22 +65,22 @@ describe MemoryModel::Base::Fieldable::FieldSet do
|
|
79
65
|
context 'with a lambda with an arity of 0' do
|
80
66
|
it 'should evaluate the block' do
|
81
67
|
field_set.add :foo, default: -> { 5 + 5 }
|
82
|
-
field_set.
|
68
|
+
field_set.set_default_values(mock_model)[:foo].should == 10
|
83
69
|
end
|
84
70
|
end
|
85
71
|
|
86
72
|
context 'with a lambda with an arity of 1' do
|
87
73
|
it 'should evaluate the block' do
|
88
|
-
mock_model.should_receive
|
74
|
+
mock_model.should_receive(:foo_val).and_return('some value')
|
89
75
|
field_set.add :foo, default: ->(model) { model.foo_val }
|
90
|
-
field_set.
|
76
|
+
field_set.set_default_values(mock_model)
|
91
77
|
end
|
92
78
|
end
|
93
79
|
|
94
80
|
context 'with a lambda with an arity of 2' do
|
95
81
|
it 'should raise an error' do
|
96
82
|
field_set.add :foo, default: ->(model, other_var) { nil }
|
97
|
-
expect { field_set.
|
83
|
+
expect { field_set.set_default_values(mock_model) }.to raise_error ArgumentError
|
98
84
|
end
|
99
85
|
end
|
100
86
|
|
@@ -102,36 +88,36 @@ describe MemoryModel::Base::Fieldable::FieldSet do
|
|
102
88
|
it 'should evaluate the block' do
|
103
89
|
mock_model.should_receive :foo_val
|
104
90
|
field_set.add :foo, default: proc { foo_val }
|
105
|
-
field_set.
|
91
|
+
field_set.set_default_values(mock_model)
|
106
92
|
end
|
107
93
|
end
|
108
94
|
|
109
95
|
context 'with a proc with an arity of 1' do
|
110
96
|
it 'should evaluate the block' do
|
111
|
-
mock_model.should_receive
|
97
|
+
mock_model.should_receive(:foo_val).and_return('Foo')
|
112
98
|
field_set.add :foo, default: proc { |model| model.foo_val }
|
113
|
-
field_set.
|
99
|
+
field_set.set_default_values(mock_model)
|
114
100
|
end
|
115
101
|
end
|
116
102
|
|
117
103
|
context 'with a proc with an arity of 2' do
|
118
104
|
it 'should raise an error' do
|
119
105
|
field_set.add :foo, default: proc { |model, other_var| model.foo_val }
|
120
|
-
expect { field_set.
|
106
|
+
expect { field_set.set_default_values(mock_model) }.to raise_error ArgumentError
|
121
107
|
end
|
122
108
|
end
|
123
109
|
|
124
110
|
context 'with a nil value' do
|
125
111
|
it 'should return nil' do
|
126
112
|
field_set.add :foo
|
127
|
-
field_set.
|
113
|
+
field_set.set_default_values(mock_model)[:foo].should be_nil
|
128
114
|
end
|
129
115
|
end
|
130
116
|
|
131
117
|
context 'when an invalid object' do
|
132
118
|
it 'should raise an error' do
|
133
119
|
field_set.add :foo, default: Object.new
|
134
|
-
expect { field_set.
|
120
|
+
expect { field_set.set_default_values(mock_model) }.to raise_error ArgumentError
|
135
121
|
end
|
136
122
|
end
|
137
123
|
end
|
@@ -146,7 +132,7 @@ describe MemoryModel::Base::Fieldable::FieldSet do
|
|
146
132
|
|
147
133
|
describe '#method_missing' do
|
148
134
|
it 'should delegate off to #to_a' do
|
149
|
-
mock_array =
|
135
|
+
mock_array = double
|
150
136
|
mock_array.should_receive :fubar
|
151
137
|
field_set.stub(:to_a).and_return(mock_array)
|
152
138
|
field_set.fubar
|
@@ -1,21 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MemoryModel::Base::
|
3
|
+
describe MemoryModel::Base::Fields::Field do
|
4
4
|
|
5
|
-
subject(:field) { MemoryModel::Base::
|
5
|
+
subject(:field) { MemoryModel::Base::Fields::Field.new(:foo) }
|
6
6
|
|
7
7
|
describe '.new' do
|
8
8
|
it 'should not raise an error' do
|
9
|
-
expect { MemoryModel::Base::
|
9
|
+
expect { MemoryModel::Base::Fields::Field.new(:foo) }.to_not raise_error
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should have default options' do
|
13
|
-
field = MemoryModel::Base::
|
13
|
+
field = MemoryModel::Base::Fields::Field.new(:foo)
|
14
14
|
field.options.should be_present
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should set_options' do
|
18
|
-
field = MemoryModel::Base::
|
18
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, bar: :baz)
|
19
19
|
field.options[:bar].should == :baz
|
20
20
|
end
|
21
21
|
end
|
@@ -23,27 +23,27 @@ describe MemoryModel::Base::Fieldable::Field do
|
|
23
23
|
describe '#==' do
|
24
24
|
context 'given a field' do
|
25
25
|
it 'should be equal to an object with the same name' do
|
26
|
-
field_a = MemoryModel::Base::
|
27
|
-
field_b = MemoryModel::Base::
|
26
|
+
field_a = MemoryModel::Base::Fields::Field.new(:foo, comparable: true)
|
27
|
+
field_b = MemoryModel::Base::Fields::Field.new(:foo, comparable: false)
|
28
28
|
(field_a == field_b).should be_true
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'should not be equal to an object with a different name' do
|
32
|
-
field_a = MemoryModel::Base::
|
33
|
-
field_b = MemoryModel::Base::
|
32
|
+
field_a = MemoryModel::Base::Fields::Field.new(:foo)
|
33
|
+
field_b = MemoryModel::Base::Fields::Field.new(:bar)
|
34
34
|
(field_a == field_b).should be_false
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'given a symbol' do
|
39
39
|
it 'should be equal to an object with the same name' do
|
40
|
-
field_a = MemoryModel::Base::
|
40
|
+
field_a = MemoryModel::Base::Fields::Field.new(:foo, comparable: true)
|
41
41
|
field_b = :foo
|
42
42
|
(field_a == field_b).should be_true
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should not be equal to an object with a different name' do
|
46
|
-
field_a = MemoryModel::Base::
|
46
|
+
field_a = MemoryModel::Base::Fields::Field.new(:foo)
|
47
47
|
field_b = :bar
|
48
48
|
(field_a == field_b).should be_false
|
49
49
|
end
|
@@ -52,31 +52,31 @@ describe MemoryModel::Base::Fieldable::Field do
|
|
52
52
|
|
53
53
|
describe '#comparable?' do
|
54
54
|
it 'should return true' do
|
55
|
-
field = MemoryModel::Base::
|
55
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, comparable: true)
|
56
56
|
field.comparable?.should be_true
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'should return false' do
|
60
|
-
field = MemoryModel::Base::
|
60
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, comparable: false)
|
61
61
|
field.comparable?.should be_false
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
describe '#default' do
|
66
66
|
it 'should return the default value' do
|
67
|
-
field = MemoryModel::Base::
|
67
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, default: 'foo')
|
68
68
|
field.default.should == 'foo'
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
describe '#readonly?' do
|
73
73
|
it 'should return true' do
|
74
|
-
field = MemoryModel::Base::
|
74
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, readonly: true)
|
75
75
|
field.readonly?.should be_true
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'should return false' do
|
79
|
-
field = MemoryModel::Base::
|
79
|
+
field = MemoryModel::Base::Fields::Field.new(:foo, readonly: false)
|
80
80
|
field.readonly?.should be_false
|
81
81
|
end
|
82
82
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe MemoryModel::Base::
|
3
|
+
describe MemoryModel::Base::Operations::Comparisons do
|
4
4
|
let(:model) do
|
5
5
|
Class.new(MemoryModel::Base) do
|
6
6
|
field :foo
|
@@ -47,7 +47,7 @@ describe MemoryModel::Base::Comparable do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
context 'given an instance of a different class' do
|
50
|
-
let(:other_class) {
|
50
|
+
let(:other_class) { model.dup }
|
51
51
|
let(:valid_instance) { other_class.new(foo: value) }
|
52
52
|
let(:invalid_instance) { other_class.new(foo: 'baz') }
|
53
53
|
it 'should be true when given a valid instance' do
|
@@ -98,7 +98,7 @@ describe MemoryModel::Base::Comparable do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
context 'given an instance of a different class' do
|
101
|
-
let(:other_class) {
|
101
|
+
let(:other_class) { model.dup }
|
102
102
|
let(:valid_instance) { other_class.new(foo: value) }
|
103
103
|
let(:invalid_instance) { other_class.new(foo: 'baz') }
|
104
104
|
it 'should be true when given a valid instance' do
|
@@ -145,7 +145,7 @@ describe MemoryModel::Base::Comparable do
|
|
145
145
|
end
|
146
146
|
|
147
147
|
context 'given an instance of a different class' do
|
148
|
-
let(:other_class) {
|
148
|
+
let(:other_class) { model.dup }
|
149
149
|
let(:other_instance) { other_class.new(foo: value) }
|
150
150
|
it 'should be false' do
|
151
151
|
(instance === other_instance).should be_false
|
@@ -14,7 +14,7 @@ describe MemoryModel::Base::Persistence do
|
|
14
14
|
|
15
15
|
describe '#persisted?' do
|
16
16
|
it 'should be true if persisted' do
|
17
|
-
instance.
|
17
|
+
instance.save
|
18
18
|
instance.persisted?.should be_true
|
19
19
|
end
|
20
20
|
|
@@ -25,7 +25,7 @@ describe MemoryModel::Base::Persistence do
|
|
25
25
|
|
26
26
|
describe '#new_record?' do
|
27
27
|
it 'should be true unless persisted' do
|
28
|
-
instance.
|
28
|
+
instance.save
|
29
29
|
instance.new_record?.should be_false
|
30
30
|
end
|
31
31
|
|
@@ -25,11 +25,7 @@ describe MemoryModel::Base do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'should have a field set' do
|
28
|
-
model.fields.should be_a MemoryModel::Base::
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should have an id field' do
|
32
|
-
model.fields.should include :id
|
28
|
+
model.fields.should be_a MemoryModel::Base::Fields::FieldSet
|
33
29
|
end
|
34
30
|
end
|
35
31
|
|
@@ -39,12 +35,17 @@ describe MemoryModel::Base do
|
|
39
35
|
field :foo
|
40
36
|
end
|
41
37
|
end
|
42
|
-
let(:
|
43
|
-
subject(:instance) { model.new(
|
38
|
+
let(:attributes) { { foo: 'bar' } }
|
39
|
+
subject(:instance) { model.new(attributes) }
|
44
40
|
|
45
41
|
describe '.new' do
|
46
|
-
it 'should
|
47
|
-
|
42
|
+
it 'should set the default values' do
|
43
|
+
expect_any_instance_of(MemoryModel::Base::Fields::FieldSet).to receive(:set_default_values).with(an_instance_of(model), attributes)
|
44
|
+
instance
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should run initialize callbacks' do
|
48
|
+
pending
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|