mongoid_monkey 0.1.3 → 0.1.4

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.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/patches/atomic.rb +225 -1
  3. data/lib/version.rb +1 -1
  4. data/spec/app/models/address.rb +17 -0
  5. data/spec/app/models/name.rb +14 -0
  6. data/spec/app/models/person.rb +39 -0
  7. data/spec/unit/{atomic_spec.rb → atomic/atomic_contextual_spec.rb} +0 -0
  8. data/spec/unit/atomic/mongoid3_style/atomic/add_to_set_spec.rb +266 -0
  9. data/spec/unit/atomic/mongoid3_style/atomic/bit_spec.rb +92 -0
  10. data/spec/unit/atomic/mongoid3_style/atomic/inc_spec.rb +137 -0
  11. data/spec/unit/atomic/mongoid3_style/atomic/pop_spec.rb +115 -0
  12. data/spec/unit/atomic/mongoid3_style/atomic/pull_all_spec.rb +81 -0
  13. data/spec/unit/atomic/mongoid3_style/atomic/pull_spec.rb +84 -0
  14. data/spec/unit/atomic/mongoid3_style/atomic/push_all_spec.rb +81 -0
  15. data/spec/unit/atomic/mongoid3_style/atomic/push_spec.rb +81 -0
  16. data/spec/unit/atomic/mongoid3_style/atomic/rename_spec.rb +46 -0
  17. data/spec/unit/atomic/mongoid3_style/atomic/sets_spec.rb +158 -0
  18. data/spec/unit/atomic/mongoid3_style/atomic/unset_spec.rb +69 -0
  19. data/spec/unit/atomic/mongoid3_style/atomic_spec.rb +220 -0
  20. data/spec/unit/atomic/mongoid4_style/incrementable_spec.rb +232 -0
  21. data/spec/unit/atomic/mongoid4_style/logical_spec.rb +262 -0
  22. data/spec/unit/atomic/mongoid4_style/poppable_spec.rb +139 -0
  23. data/spec/unit/atomic/mongoid4_style/pullable_spec.rb +172 -0
  24. data/spec/unit/atomic/mongoid4_style/pushable_spec.rb +159 -0
  25. data/spec/unit/atomic/mongoid4_style/renamable_spec.rb +139 -0
  26. data/spec/unit/atomic/mongoid4_style/settable_spec.rb +172 -0
  27. data/spec/unit/atomic/mongoid4_style/unsettable_spec.rb +28 -0
  28. metadata +46 -4
@@ -0,0 +1,158 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Sets do
6
+
7
+ describe "#set" do
8
+
9
+ let(:person) do
10
+ Person.create(age: 100, pets: true)
11
+ end
12
+
13
+ let(:reloaded) do
14
+ person.reload
15
+ end
16
+
17
+ context "when setting a field on an embedded document" do
18
+
19
+ let(:address) do
20
+ person.addresses.create(street: "Tauentzienstr", number: 5)
21
+ end
22
+
23
+ let!(:set) do
24
+ address.set(:number, 5)
25
+ end
26
+
27
+ it "sets the provided value" do
28
+ set.should eq(5)
29
+ end
30
+
31
+ it "persists the change" do
32
+ reloaded.addresses.first.number.should eq(5)
33
+ end
34
+ end
35
+
36
+ context "when setting a field with a value" do
37
+
38
+ let!(:set) do
39
+ person.set(:age, 2)
40
+ end
41
+
42
+ it "sets the provided value" do
43
+ person.age.should eq(2)
44
+ end
45
+
46
+ it "returns the new value" do
47
+ set.should eq(2)
48
+ end
49
+
50
+ it "persists the changes" do
51
+ reloaded.age.should eq(2)
52
+ end
53
+
54
+ it "resets the dirty attributes" do
55
+ person.changes["age"].should be_nil
56
+ end
57
+ end
58
+
59
+ context "when setting a field with a value that must be cast" do
60
+
61
+ let(:date_time) do
62
+ DateTime.new(2012, 1, 2)
63
+ end
64
+
65
+ let!(:set) do
66
+ person.set(:lunch_time, date_time)
67
+ end
68
+
69
+ it "sets the provided value" do
70
+ person.lunch_time.should eq(date_time)
71
+ end
72
+
73
+ it "returns the new value" do
74
+ set.should eq(date_time)
75
+ end
76
+
77
+ it "persists the changes" do
78
+ reloaded.lunch_time.should eq(date_time)
79
+ end
80
+
81
+ it "resets the dirty attributes" do
82
+ person.changes["lunch_time"].should be_nil
83
+ end
84
+ end
85
+
86
+ context "when setting a field to false" do
87
+
88
+ let!(:set) do
89
+ person.set(:pets, false)
90
+ end
91
+
92
+ it "sets the provided value" do
93
+ person.pets.should be false
94
+ end
95
+
96
+ it "returns the new value" do
97
+ set.should be false
98
+ end
99
+
100
+ it "persists the changes" do
101
+ reloaded.pets.should be false
102
+ end
103
+
104
+ it "resets the dirty attributes" do
105
+ person.changes["pets"].should be_nil
106
+ end
107
+
108
+ end
109
+
110
+ context "when setting a nil field" do
111
+
112
+ let!(:set) do
113
+ person.set(:score, 2)
114
+ end
115
+
116
+ it "sets the value to the provided number" do
117
+ person.score.should eq(2)
118
+ end
119
+
120
+ it "returns the new value" do
121
+ set.should eq(2)
122
+ end
123
+
124
+ it "persists the changes" do
125
+ reloaded.score.should eq(2)
126
+ end
127
+
128
+ it "resets the dirty attributes" do
129
+ person.changes["score"].should be_nil
130
+ end
131
+ end
132
+
133
+ context "when setting a non existant field" do
134
+
135
+ let!(:set) do
136
+ person.set(:high_score, 5)
137
+ end
138
+
139
+ it "sets the value to the provided number" do
140
+ person.high_score.should eq(5)
141
+ end
142
+
143
+ it "returns the new value" do
144
+ set.should eq(5)
145
+ end
146
+
147
+ it "persists the changes" do
148
+ reloaded.high_score.should eq(5)
149
+ end
150
+
151
+ it "resets the dirty attributes" do
152
+ person.changes["high_score"].should be_nil
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Unset do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when unsetting a field" do
10
+
11
+ let(:person) do
12
+ Person.create(age: 100)
13
+ end
14
+
15
+ let!(:removed) do
16
+ person.unset(:age)
17
+ end
18
+
19
+ it "removes the field" do
20
+ person.age.should be_nil
21
+ end
22
+
23
+ it "resets the dirty attributes" do
24
+ person.changes["age"].should be_nil
25
+ end
26
+
27
+ it "returns nil" do
28
+ removed.should be_nil
29
+ end
30
+ end
31
+
32
+
33
+ [[ :age, :score, { safe: true }], [ :age, :score ], [ [:age, :score ]]].each do |args|
34
+
35
+ context "when unsetting multiple fields using #{args}" do
36
+
37
+ let(:person) do
38
+ Person.create(age: 100, score: 2)
39
+ end
40
+
41
+ let!(:removed) do
42
+ person.unset *(args)
43
+ end
44
+
45
+ it "removes age field" do
46
+ person.age.should be_nil
47
+ end
48
+
49
+ it "removes score field" do
50
+ person.score.should be_nil
51
+ end
52
+
53
+ it "resets the age dirty attribute" do
54
+ person.changes["age"].should be_nil
55
+ end
56
+
57
+ it "resets the score dirty attribute" do
58
+ person.changes["score"].should be_nil
59
+ end
60
+
61
+ it "returns nil" do
62
+ removed.should be_nil
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,220 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic do
6
+
7
+ context "when using aliased field names" do
8
+
9
+ describe "#add_to_set" do
10
+
11
+ let(:person) do
12
+ Person.create(array: [ "test" ])
13
+ end
14
+
15
+ before do
16
+ person.add_to_set(:array, "testy")
17
+ end
18
+
19
+ it "adds to the aliased field" do
20
+ person.array.should eq([ "test", "testy" ])
21
+ end
22
+
23
+ it "persists the change" do
24
+ person.reload.array.should eq([ "test", "testy" ])
25
+ end
26
+ end
27
+
28
+ describe "#bit" do
29
+
30
+ let(:person) do
31
+ Person.create(inte: 60)
32
+ end
33
+
34
+ before do
35
+ person.bit(:inte, { and: 13 })
36
+ end
37
+
38
+ it "performs the bitwise operation" do
39
+ person.inte.should eq(12)
40
+ end
41
+
42
+ it "persists the changes" do
43
+ person.reload.inte.should eq(12)
44
+ end
45
+ end
46
+
47
+ describe "#inc" do
48
+
49
+ let(:person) do
50
+ Person.create(inte: 5)
51
+ end
52
+
53
+ before do
54
+ person.inc(:inte, 1)
55
+ end
56
+
57
+ it "increments the aliased field" do
58
+ person.inte.should eq(6)
59
+ end
60
+
61
+ it "persists the change" do
62
+ person.reload.inte.should eq(6)
63
+ end
64
+ end
65
+
66
+ describe "#pop" do
67
+
68
+ let(:person) do
69
+ Person.create(array: [ "test1", "test2" ])
70
+ end
71
+
72
+ before do
73
+ person.pop(:array, 1)
74
+ end
75
+
76
+ it "removes from the aliased field" do
77
+ person.array.should eq([ "test1" ])
78
+ end
79
+
80
+ it "persists the change" do
81
+ person.reload.array.should eq([ "test1" ])
82
+ end
83
+ end
84
+
85
+ describe "#pull" do
86
+
87
+ let(:person) do
88
+ Person.create(array: [ "test1", "test2" ])
89
+ end
90
+
91
+ before do
92
+ person.pull(:array, "test1")
93
+ end
94
+
95
+ it "removes from the aliased field" do
96
+ person.array.should eq([ "test2" ])
97
+ end
98
+
99
+ it "persists the change" do
100
+ person.reload.array.should eq([ "test2" ])
101
+ end
102
+ end
103
+
104
+ describe "#pull_all" do
105
+
106
+ let(:person) do
107
+ Person.create(array: [ "test1", "test2" ])
108
+ end
109
+
110
+ before do
111
+ person.pull_all(:array, [ "test1", "test2" ])
112
+ end
113
+
114
+ it "removes from the aliased field" do
115
+ person.array.should be_empty
116
+ end
117
+
118
+ it "persists the change" do
119
+ person.reload.array.should be_empty
120
+ end
121
+ end
122
+
123
+ describe "#push" do
124
+
125
+ let(:person) do
126
+ Person.create(array: [ "test" ])
127
+ end
128
+
129
+ before do
130
+ person.push(:array, "testy")
131
+ end
132
+
133
+ it "adds to the aliased field" do
134
+ person.array.should eq([ "test", "testy" ])
135
+ end
136
+
137
+ it "persists the change" do
138
+ person.reload.array.should eq([ "test", "testy" ])
139
+ end
140
+ end
141
+
142
+ describe "#push_all" do
143
+
144
+ let(:person) do
145
+ Person.create(array: [ "test" ])
146
+ end
147
+
148
+ before do
149
+ person.push_all(:array, [ "testy", "test2" ])
150
+ end
151
+
152
+ it "adds to the aliased field" do
153
+ person.array.should eq([ "test", "testy", "test2" ])
154
+ end
155
+
156
+ it "persists the change" do
157
+ person.reload.array.should eq([ "test", "testy", "test2" ])
158
+ end
159
+ end
160
+
161
+ describe "#rename" do
162
+
163
+ let(:person) do
164
+ Person.create(inte: 5)
165
+ end
166
+
167
+ before do
168
+ person.rename(:inte, :integer)
169
+ end
170
+
171
+ it "renames the aliased field" do
172
+ person.integer.should eq(5)
173
+ end
174
+
175
+ it "persists the change" do
176
+ person.reload.integer.should eq(5)
177
+ end
178
+ end
179
+
180
+ describe "#set" do
181
+
182
+ let(:person) do
183
+ Person.create(inte: 5)
184
+ end
185
+
186
+ before do
187
+ person.set(:inte, 8)
188
+ end
189
+
190
+ it "sets the aliased field" do
191
+ person.inte.should eq(8)
192
+ end
193
+
194
+ it "persists the change" do
195
+ person.reload.inte.should eq(8)
196
+ end
197
+ end
198
+
199
+ describe "#unset" do
200
+
201
+ let(:person) do
202
+ Person.create(inte: 5)
203
+ end
204
+
205
+ before do
206
+ person.unset(:inte)
207
+ end
208
+
209
+ it "unsets the aliased field" do
210
+ person.inte.should be_nil
211
+ end
212
+
213
+ it "persists the change" do
214
+ person.reload.inte.should be_nil
215
+ end
216
+ end
217
+ end
218
+ end
219
+
220
+ end
@@ -0,0 +1,232 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe 'Mongoid::Persistable::Pullable' do
6
+
7
+ describe "#pull" do
8
+
9
+ context "when the document is a root document" do
10
+
11
+ shared_examples_for "a pullable root document" do
12
+
13
+ it "pulls the first value" do
14
+ expect(person.aliases).to eq([ 2, 3 ])
15
+ end
16
+
17
+ it "pulls the last value" do
18
+ expect(person.array).to eq([ 4, 6 ])
19
+ end
20
+
21
+ it "returns self object" do
22
+ expect(pull).to eq(person)
23
+ end
24
+
25
+ it "resets dirty changes" do
26
+ expect(person).to_not be_changed
27
+ end
28
+
29
+ it "persists the first pull" do
30
+ expect(person.reload.aliases).to eq([ 2, 3 ])
31
+ end
32
+
33
+ it "persists the last pull" do
34
+ expect(person.reload.array).to eq([ 4, 6 ])
35
+ end
36
+ end
37
+
38
+ let(:person) do
39
+ Person.create(aliases: [ 1, 1, 2, 3 ], array: [ 4, 5, 6 ])
40
+ end
41
+
42
+ context "when providing string keys" do
43
+
44
+ let!(:pull) do
45
+ person.pull("aliases" => 1, "array" => 5, "test_array" => 2)
46
+ end
47
+
48
+ it_behaves_like "a pullable root document"
49
+ end
50
+
51
+ context "when providing symbol keys" do
52
+
53
+ let!(:pull) do
54
+ person.pull(aliases: 1, array: 5)
55
+ end
56
+
57
+ it_behaves_like "a pullable root document"
58
+ end
59
+ end
60
+
61
+ context "when the document is embedded" do
62
+
63
+ shared_examples_for "a pullable embedded document" do
64
+
65
+ it "pulls the first value" do
66
+ expect(address.services).to eq([ 2, 3 ])
67
+ end
68
+
69
+ it "pulls the last value" do
70
+ expect(address.a).to eq([ 4, 6 ])
71
+ end
72
+
73
+ it "returns self object" do
74
+ expect(pull).to eq(address)
75
+ end
76
+
77
+ it "resets dirty changes" do
78
+ expect(address).to_not be_changed
79
+ end
80
+
81
+ it "persists the first pull" do
82
+ expect(address.reload.services).to eq([ 2, 3 ])
83
+ end
84
+
85
+ it "persists the last pull" do
86
+ expect(address.reload.a).to eq([ 4, 6 ])
87
+ end
88
+ end
89
+
90
+ let(:person) do
91
+ Person.create
92
+ end
93
+
94
+ let(:address) do
95
+ person.addresses.create(street: "t", services: [ 1, 2, 3 ], a: [ 4, 5, 6 ])
96
+ end
97
+
98
+ context "when providing string keys" do
99
+
100
+ let!(:pull) do
101
+ address.pull("services" => 1, "a" => 5)
102
+ end
103
+
104
+ it_behaves_like "a pullable embedded document"
105
+ end
106
+
107
+ context "when providing symbol keys" do
108
+
109
+ let!(:pull) do
110
+ address.pull(services: 1, a: 5)
111
+ end
112
+
113
+ it_behaves_like "a pullable embedded document"
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "#pull_all" do
119
+
120
+ context "when the document is the root document" do
121
+
122
+ shared_examples_for "a multi-pullable root document" do
123
+
124
+ it "pulls the first value" do
125
+ expect(person.aliases).to eq([ 3 ])
126
+ end
127
+
128
+ it "pulls the last value" do
129
+ expect(person.array).to eq([ 4 ])
130
+ end
131
+
132
+ it "returns self object" do
133
+ expect(pull_all).to eq(person)
134
+ end
135
+
136
+ it "resets dirty changes" do
137
+ expect(person).to_not be_changed
138
+ end
139
+
140
+ it "persists the first pull" do
141
+ expect(person.reload.aliases).to eq([ 3 ])
142
+ end
143
+
144
+ it "persists the last pull" do
145
+ expect(person.reload.array).to eq([ 4 ])
146
+ end
147
+ end
148
+
149
+ let(:person) do
150
+ Person.create(aliases: [ 1, 1, 2, 3 ], array: [ 4, 5, 6 ])
151
+ end
152
+
153
+ context "when providing string keys" do
154
+
155
+ let!(:pull_all) do
156
+ person.pull_all(
157
+ "aliases" => [ 1, 2 ], "array" => [ 5, 6 ], "test_array" => [ 1 ]
158
+ )
159
+ end
160
+
161
+ it_behaves_like "a multi-pullable root document"
162
+ end
163
+
164
+ context "when providing symbol keys" do
165
+
166
+ let!(:pull_all) do
167
+ person.pull_all(aliases: [ 1, 2 ], array: [ 5, 6 ])
168
+ end
169
+
170
+ it_behaves_like "a multi-pullable root document"
171
+ end
172
+ end
173
+
174
+ context "when the document is embedded" do
175
+
176
+ shared_examples_for "a multi-pullable embedded document" do
177
+
178
+ it "pulls the first value" do
179
+ expect(address.services).to eq([ 3 ])
180
+ end
181
+
182
+ it "pulls the last value" do
183
+ expect(address.a).to eq([ 4 ])
184
+ end
185
+
186
+ it "returns self object" do
187
+ expect(pull_all).to eq(address)
188
+ end
189
+
190
+ it "resets dirty changes" do
191
+ expect(address).to_not be_changed
192
+ end
193
+
194
+ it "persists the first pull" do
195
+ expect(address.reload.services).to eq([ 3 ])
196
+ end
197
+
198
+ it "persists the last pull" do
199
+ expect(address.reload.a).to eq([ 4 ])
200
+ end
201
+ end
202
+
203
+ let(:person) do
204
+ Person.create
205
+ end
206
+
207
+ let(:address) do
208
+ person.addresses.create(street: "t", services: [ 1, 2, 3 ], a: [ 4, 5, 6 ])
209
+ end
210
+
211
+ context "when providing string keys" do
212
+
213
+ let!(:pull_all) do
214
+ address.pull_all("services" => [ 1, 2 ], "a" => [ 5, 6 ])
215
+ end
216
+
217
+ it_behaves_like "a multi-pullable embedded document"
218
+ end
219
+
220
+ context "when providing symbol keys" do
221
+
222
+ let!(:pull_all) do
223
+ address.pull_all(services: [ 1, 2 ], a: [ 5, 6 ])
224
+ end
225
+
226
+ it_behaves_like "a multi-pullable embedded document"
227
+ end
228
+ end
229
+ end
230
+ end
231
+
232
+ end