mongoid_monkey 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
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,137 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Inc do
6
+
7
+ describe "#inc" do
8
+
9
+ let(:person) do
10
+ Person.create(age: 100)
11
+ end
12
+
13
+ let(:reloaded) do
14
+ person.reload
15
+ end
16
+
17
+ context "when incrementing 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!(:inced) do
24
+ address.inc(:number, 5)
25
+ end
26
+
27
+ it "increments the provided value" do
28
+ inced.should eq(10)
29
+ end
30
+
31
+ it "persists the change" do
32
+ reloaded.addresses.first.number.should eq(10)
33
+ end
34
+ end
35
+
36
+ context "when incrementing a field with a value" do
37
+
38
+ context "when provided an integer" do
39
+
40
+ let!(:inced) do
41
+ person.inc(:age, 2)
42
+ end
43
+
44
+ it "increments by the provided value" do
45
+ person.age.should eq(102)
46
+ end
47
+
48
+ it "returns the new value" do
49
+ inced.should eq(102)
50
+ end
51
+
52
+ it "persists the changes" do
53
+ reloaded.age.should eq(102)
54
+ end
55
+
56
+ it "keeps the field as an integer" do
57
+ inced.should be_a(Integer)
58
+ end
59
+
60
+ it "resets the dirty attributes" do
61
+ person.changes["age"].should be_nil
62
+ end
63
+ end
64
+
65
+ context "when provided a big decimal" do
66
+
67
+ let!(:inced) do
68
+ person.inc(:blood_alcohol_content, BigDecimal.new("2.2"))
69
+ end
70
+
71
+ it "increments by the provided value" do
72
+ person.blood_alcohol_content.should eq(2.2)
73
+ end
74
+
75
+ it "returns the new value" do
76
+ inced.should eq(2.2)
77
+ end
78
+
79
+ it "persists the changes" do
80
+ reloaded.blood_alcohol_content.should eq(2.2)
81
+ end
82
+
83
+ it "resets the dirty attributes" do
84
+ person.changes["blood_alcohol_content"].should be_nil
85
+ end
86
+ end
87
+ end
88
+
89
+ context "when incrementing a nil field" do
90
+
91
+ let!(:inced) do
92
+ person.inc(:score, 2)
93
+ end
94
+
95
+ it "sets the value to the provided number" do
96
+ person.score.should eq(2)
97
+ end
98
+
99
+ it "returns the new value" do
100
+ inced.should eq(2)
101
+ end
102
+
103
+ it "persists the changes" do
104
+ reloaded.score.should eq(2)
105
+ end
106
+
107
+ it "resets the dirty attributes" do
108
+ person.changes["score"].should be_nil
109
+ end
110
+ end
111
+
112
+ context "when incrementing a non existant field" do
113
+
114
+ let!(:inced) do
115
+ person.inc(:high_score, 5)
116
+ end
117
+
118
+ it "sets the value to the provided number" do
119
+ person.high_score.should eq(5)
120
+ end
121
+
122
+ it "returns the new value" do
123
+ inced.should eq(5)
124
+ end
125
+
126
+ it "persists the changes" do
127
+ reloaded.high_score.should eq(5)
128
+ end
129
+
130
+ it "resets the dirty attributes" do
131
+ person.changes["high_score"].should be_nil
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,115 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Pop do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when the field exists" do
10
+
11
+ let(:person) do
12
+ Person.create(aliases: [ "007", "008", "009" ])
13
+ end
14
+
15
+ context "when popping the last element" do
16
+
17
+ let!(:popped) do
18
+ person.pop(:aliases, 1)
19
+ end
20
+
21
+ let(:reloaded) do
22
+ person.reload
23
+ end
24
+
25
+ it "pops the value from the array" do
26
+ person.aliases.should eq([ "007", "008" ])
27
+ end
28
+
29
+ it "persists the data" do
30
+ reloaded.aliases.should eq([ "007", "008" ])
31
+ end
32
+
33
+ it "removes the field from the dirty attributes" do
34
+ person.changes["aliases"].should be_nil
35
+ end
36
+
37
+ it "resets the document dirty flag" do
38
+ person.should_not be_changed
39
+ end
40
+
41
+ it "returns the new array value" do
42
+ popped.should eq([ "007", "008" ])
43
+ end
44
+ end
45
+
46
+ context "when popping the first element" do
47
+
48
+ let!(:popped) do
49
+ person.pop(:aliases, -1)
50
+ end
51
+
52
+ let(:reloaded) do
53
+ person.reload
54
+ end
55
+
56
+ it "pops the value from the array" do
57
+ person.aliases.should eq([ "008", "009" ])
58
+ end
59
+
60
+ it "persists the data" do
61
+ reloaded.aliases.should eq([ "008", "009" ])
62
+ end
63
+
64
+ it "removes the field from the dirty attributes" do
65
+ person.changes["aliases"].should be_nil
66
+ end
67
+
68
+ it "resets the document dirty flag" do
69
+ person.should_not be_changed
70
+ end
71
+
72
+ it "returns the new array value" do
73
+ popped.should eq([ "008", "009" ])
74
+ end
75
+ end
76
+ end
77
+
78
+ context "when the field does not exist" do
79
+
80
+ let(:person) do
81
+ Person.create
82
+ end
83
+
84
+ let!(:popped) do
85
+ person.pop(:aliases, 1)
86
+ end
87
+
88
+ let(:reloaded) do
89
+ person.reload
90
+ end
91
+
92
+ it "does not modify the field" do
93
+ person.aliases.should be_nil
94
+ end
95
+
96
+ it "persists no data" do
97
+ reloaded.aliases.should be_nil
98
+ end
99
+
100
+ it "removes the field from the dirty attributes" do
101
+ person.changes["aliases"].should be_nil
102
+ end
103
+
104
+ it "resets the document dirty flag" do
105
+ person.should_not be_changed
106
+ end
107
+
108
+ it "returns nil" do
109
+ popped.should be_nil
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::PullAll do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when the field exists" do
10
+
11
+ let(:person) do
12
+ Person.create(aliases: [ "007" ])
13
+ end
14
+
15
+ let!(:pulled) do
16
+ person.pull_all(:aliases, [ "007" ])
17
+ end
18
+
19
+ let(:reloaded) do
20
+ person.reload
21
+ end
22
+
23
+ it "pulls the value from the array" do
24
+ person.aliases.should be_empty
25
+ end
26
+
27
+ it "persists the data" do
28
+ reloaded.aliases.should be_empty
29
+ end
30
+
31
+ it "removes the field from the dirty attributes" do
32
+ person.changes["aliases"].should be_nil
33
+ end
34
+
35
+ it "resets the document dirty flag" do
36
+ person.should_not be_changed
37
+ end
38
+
39
+ it "returns the new array value" do
40
+ pulled.should be_empty
41
+ end
42
+ end
43
+
44
+ context "when the field does not exist" do
45
+
46
+ let(:person) do
47
+ Person.create
48
+ end
49
+
50
+ let!(:pulled) do
51
+ person.pull_all(:aliases, [ "Bond" ])
52
+ end
53
+
54
+ let(:reloaded) do
55
+ person.reload
56
+ end
57
+
58
+ it "does not modify the field" do
59
+ person.aliases.should be_nil
60
+ end
61
+
62
+ it "persists no data" do
63
+ reloaded.aliases.should be_nil
64
+ end
65
+
66
+ it "removes the field from the dirty attributes" do
67
+ person.changes["aliases"].should be_nil
68
+ end
69
+
70
+ it "resets the document dirty flag" do
71
+ person.should_not be_changed
72
+ end
73
+
74
+ it "returns nil" do
75
+ pulled.should be_nil
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Pull do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when the field exists" do
10
+
11
+ let(:person) do
12
+ Person.create(aliases: [ "007", "008" ])
13
+ end
14
+
15
+ context "when pulling an exact value" do
16
+
17
+ let!(:pulled) do
18
+ person.pull(:aliases, "007")
19
+ end
20
+
21
+ let(:reloaded) do
22
+ person.reload
23
+ end
24
+
25
+ it "pulls the value from the array" do
26
+ person.aliases.should eq([ "008" ])
27
+ end
28
+
29
+ it "persists the data" do
30
+ reloaded.aliases.should eq([ "008" ])
31
+ end
32
+
33
+ it "removes the field from the dirty attributes" do
34
+ person.changes["aliases"].should be_nil
35
+ end
36
+
37
+ it "resets the document dirty flag" do
38
+ person.should_not be_changed
39
+ end
40
+
41
+ it "returns the new array value" do
42
+ pulled.should eq([ "008" ])
43
+ end
44
+ end
45
+ end
46
+
47
+ context "when the field does not exist" do
48
+
49
+ let(:person) do
50
+ Person.create
51
+ end
52
+
53
+ let!(:pulled) do
54
+ person.pull(:aliases, "Bond")
55
+ end
56
+
57
+ let(:reloaded) do
58
+ person.reload
59
+ end
60
+
61
+ it "does not modify the field" do
62
+ person.aliases.should be_nil
63
+ end
64
+
65
+ it "persists no data" do
66
+ reloaded.aliases.should be_nil
67
+ end
68
+
69
+ it "removes the field from the dirty attributes" do
70
+ person.changes["aliases"].should be_nil
71
+ end
72
+
73
+ it "resets the document dirty flag" do
74
+ person.should_not be_changed
75
+ end
76
+
77
+ it "returns nil" do
78
+ pulled.should be_nil
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::PushAll do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when the field exists" do
10
+
11
+ let(:person) do
12
+ Person.create(aliases: [ "007" ])
13
+ end
14
+
15
+ let!(:pushed) do
16
+ person.push_all(:aliases, [ "Bond", "James" ])
17
+ end
18
+
19
+ let(:reloaded) do
20
+ person.reload
21
+ end
22
+
23
+ it "pushes the value onto the array" do
24
+ person.aliases.should eq([ "007", "Bond", "James" ])
25
+ end
26
+
27
+ it "persists the data" do
28
+ reloaded.aliases.should eq([ "007", "Bond", "James" ])
29
+ end
30
+
31
+ it "removes the field from the dirty attributes" do
32
+ person.changes["aliases"].should be_nil
33
+ end
34
+
35
+ it "resets the document dirty flag" do
36
+ person.should_not be_changed
37
+ end
38
+
39
+ it "returns the new array value" do
40
+ pushed.should eq([ "007", "Bond", "James" ])
41
+ end
42
+ end
43
+
44
+ context "when the field does not exist" do
45
+
46
+ let(:person) do
47
+ Person.create
48
+ end
49
+
50
+ let!(:pushed) do
51
+ person.push_all(:aliases, [ "Bond", "James" ])
52
+ end
53
+
54
+ let(:reloaded) do
55
+ person.reload
56
+ end
57
+
58
+ it "pushes the value onto the array" do
59
+ person.aliases.should eq([ "Bond", "James" ])
60
+ end
61
+
62
+ it "persists the data" do
63
+ reloaded.aliases.should eq([ "Bond", "James" ])
64
+ end
65
+
66
+ it "removes the field from the dirty attributes" do
67
+ person.changes["aliases"].should be_nil
68
+ end
69
+
70
+ it "resets the document dirty flag" do
71
+ person.should_not be_changed
72
+ end
73
+
74
+ it "returns the new array value" do
75
+ pushed.should eq([ "Bond", "James" ])
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Push do
6
+
7
+ describe "#persist" do
8
+
9
+ context "when the field exists" do
10
+
11
+ let(:person) do
12
+ Person.create(aliases: [ "007" ])
13
+ end
14
+
15
+ let!(:pushed) do
16
+ person.push(:aliases, "Bond")
17
+ end
18
+
19
+ let(:reloaded) do
20
+ person.reload
21
+ end
22
+
23
+ it "pushes the value onto the array" do
24
+ person.aliases.should eq([ "007", "Bond" ])
25
+ end
26
+
27
+ it "persists the data" do
28
+ reloaded.aliases.should eq([ "007", "Bond" ])
29
+ end
30
+
31
+ it "removes the field from the dirty attributes" do
32
+ person.changes["aliases"].should be_nil
33
+ end
34
+
35
+ it "resets the document dirty flag" do
36
+ person.should_not be_changed
37
+ end
38
+
39
+ it "returns the new array value" do
40
+ pushed.should eq([ "007", "Bond" ])
41
+ end
42
+ end
43
+
44
+ context "when the field does not exist" do
45
+
46
+ let(:person) do
47
+ Person.create
48
+ end
49
+
50
+ let!(:pushed) do
51
+ person.push(:aliases, "Bond")
52
+ end
53
+
54
+ let(:reloaded) do
55
+ person.reload
56
+ end
57
+
58
+ it "pushes the value onto the array" do
59
+ person.aliases.should eq([ "Bond" ])
60
+ end
61
+
62
+ it "persists the data" do
63
+ reloaded.aliases.should eq([ "Bond" ])
64
+ end
65
+
66
+ it "removes the field from the dirty attributes" do
67
+ person.changes["aliases"].should be_nil
68
+ end
69
+
70
+ it "resets the document dirty flag" do
71
+ person.should_not be_changed
72
+ end
73
+
74
+ it "returns the new array value" do
75
+ pushed.should eq([ "Bond" ])
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe Mongoid::Persistence::Atomic::Rename do
6
+
7
+ describe "#rename" do
8
+
9
+ context "when incrementing a field with a value" do
10
+
11
+ let(:person) do
12
+ Person.create(age: 100)
13
+ end
14
+
15
+ let!(:rename) do
16
+ person.rename(:age, :years)
17
+ end
18
+
19
+ it "removes the old field" do
20
+ person.age.should be_nil
21
+ end
22
+
23
+ it "adds the new field" do
24
+ person.years.should eq(100)
25
+ end
26
+
27
+ it "returns the value" do
28
+ rename.should eq(100)
29
+ end
30
+
31
+ it "resets the old dirty attributes" do
32
+ person.changes["age"].should be_nil
33
+ end
34
+
35
+ it "resets the new field dirty attributes" do
36
+ person.changes["years"].should be_nil
37
+ end
38
+
39
+ it "persists the changes" do
40
+ person.reload.years.should eq(100)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end