mongo_mapper 0.13.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +61 -0
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +7 -0
  10. data/lib/mongo_mapper/connection.rb +16 -37
  11. data/lib/mongo_mapper/document.rb +4 -0
  12. data/lib/mongo_mapper/extensions/array.rb +14 -6
  13. data/lib/mongo_mapper/extensions/hash.rb +15 -3
  14. data/lib/mongo_mapper/extensions/object.rb +4 -0
  15. data/lib/mongo_mapper/extensions/object_id.rb +5 -1
  16. data/lib/mongo_mapper/extensions/string.rb +13 -5
  17. data/lib/mongo_mapper/extensions/symbol.rb +18 -0
  18. data/lib/mongo_mapper/plugins/accessible.rb +15 -5
  19. data/lib/mongo_mapper/plugins/associations.rb +7 -6
  20. data/lib/mongo_mapper/plugins/associations/base.rb +27 -14
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
  22. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
  23. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
  24. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
  25. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +60 -29
  26. data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
  27. data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
  28. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
  29. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
  30. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
  31. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
  32. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
  33. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
  34. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
  35. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
  36. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
  37. data/lib/mongo_mapper/plugins/associations/proxy.rb +36 -29
  38. data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
  39. data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
  40. data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
  41. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  42. data/lib/mongo_mapper/plugins/document.rb +1 -1
  43. data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
  44. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
  45. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
  46. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  47. data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
  48. data/lib/mongo_mapper/plugins/indexes.rb +14 -7
  49. data/lib/mongo_mapper/plugins/keys.rb +170 -151
  50. data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
  51. data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
  52. data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
  53. data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
  54. data/lib/mongo_mapper/plugins/persistence.rb +13 -8
  55. data/lib/mongo_mapper/plugins/protected.rb +6 -5
  56. data/lib/mongo_mapper/plugins/querying.rb +85 -42
  57. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +20 -15
  58. data/lib/mongo_mapper/plugins/rails.rb +1 -0
  59. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  60. data/lib/mongo_mapper/plugins/sci.rb +0 -0
  61. data/lib/mongo_mapper/plugins/scopes.rb +78 -7
  62. data/lib/mongo_mapper/plugins/stats.rb +17 -0
  63. data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
  64. data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
  65. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  66. data/lib/mongo_mapper/railtie.rb +4 -3
  67. data/lib/mongo_mapper/utils.rb +2 -2
  68. data/lib/mongo_mapper/version.rb +1 -1
  69. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
  70. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
  71. data/spec/examples.txt +1717 -0
  72. data/spec/functional/accessible_spec.rb +19 -13
  73. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
  74. data/spec/functional/associations/belongs_to_proxy_spec.rb +36 -20
  75. data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
  76. data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
  77. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  78. data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
  79. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
  80. data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
  81. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  82. data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
  83. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
  84. data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
  85. data/spec/functional/associations/one_proxy_spec.rb +10 -10
  86. data/spec/functional/associations_spec.rb +3 -3
  87. data/spec/functional/binary_spec.rb +2 -2
  88. data/spec/functional/caching_spec.rb +8 -15
  89. data/spec/functional/callbacks_spec.rb +89 -2
  90. data/spec/functional/counter_cache_spec.rb +235 -0
  91. data/spec/functional/dirty_spec.rb +63 -46
  92. data/spec/functional/document_spec.rb +30 -5
  93. data/spec/functional/dumpable_spec.rb +1 -1
  94. data/spec/functional/embedded_document_spec.rb +17 -17
  95. data/spec/functional/identity_map_spec.rb +29 -16
  96. data/spec/functional/indexes_spec.rb +19 -18
  97. data/spec/functional/keys_spec.rb +86 -28
  98. data/spec/functional/logger_spec.rb +3 -3
  99. data/spec/functional/modifiers_spec.rb +81 -19
  100. data/spec/functional/partial_updates_spec.rb +577 -0
  101. data/spec/functional/protected_spec.rb +14 -14
  102. data/spec/functional/querying_spec.rb +77 -28
  103. data/spec/functional/safe_spec.rb +23 -27
  104. data/spec/functional/sci_spec.rb +9 -9
  105. data/spec/functional/scopes_spec.rb +235 -2
  106. data/spec/functional/static_keys_spec.rb +153 -0
  107. data/spec/functional/stats_spec.rb +86 -0
  108. data/spec/functional/strong_parameters_spec.rb +49 -0
  109. data/spec/functional/touch_spec.rb +1 -1
  110. data/spec/functional/validations_spec.rb +51 -57
  111. data/spec/quality_spec.rb +51 -0
  112. data/spec/spec_helper.rb +37 -9
  113. data/spec/support/matchers.rb +5 -14
  114. data/spec/unit/associations/base_spec.rb +12 -12
  115. data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
  116. data/spec/unit/associations/many_association_spec.rb +2 -2
  117. data/spec/unit/associations/one_association_spec.rb +2 -2
  118. data/spec/unit/associations/proxy_spec.rb +19 -20
  119. data/spec/unit/clone_spec.rb +1 -1
  120. data/spec/unit/document_spec.rb +8 -8
  121. data/spec/unit/dynamic_finder_spec.rb +8 -8
  122. data/spec/unit/embedded_document_spec.rb +18 -19
  123. data/spec/unit/extensions_spec.rb +41 -17
  124. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  125. data/spec/unit/key_spec.rb +28 -26
  126. data/spec/unit/keys_spec.rb +20 -11
  127. data/spec/unit/model_generator_spec.rb +0 -0
  128. data/spec/unit/mongo_mapper_spec.rb +38 -85
  129. data/spec/unit/rails_spec.rb +5 -0
  130. data/spec/unit/serialization_spec.rb +1 -1
  131. data/spec/unit/time_zones_spec.rb +2 -2
  132. data/spec/unit/validations_spec.rb +46 -33
  133. metadata +66 -37
  134. data/README.rdoc +0 -59
  135. data/lib/mongo_mapper/connections/10gen.rb +0 -0
  136. data/lib/mongo_mapper/connections/moped.rb +0 -0
  137. data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -21,11 +21,11 @@ describe "Support" do
21
21
 
22
22
  context "Binary.to_mongo" do
23
23
  it "should convert to binary if not binary" do
24
- Binary.to_mongo('asdfsadasdfs').is_a?(BSON::Binary).should be_true
24
+ Binary.to_mongo('asdfsadasdfs').is_a?(BSON::Binary).should be_truthy
25
25
  end
26
26
 
27
27
  it "should be binary if binary" do
28
- Binary.to_mongo(BSON::Binary.new('asdfsadasdfs')).is_a?(BSON::Binary).should be_true
28
+ Binary.to_mongo(BSON::Binary.new('asdfsadasdfs')).is_a?(BSON::Binary).should be_truthy
29
29
  end
30
30
 
31
31
  it "should be nil if nil" do
@@ -42,23 +42,23 @@ describe "Support" do
42
42
 
43
43
  context "Boolean.to_mongo" do
44
44
  it "should be true for true" do
45
- Boolean.to_mongo(true).should be_true
45
+ Boolean.to_mongo(true).should be_truthy
46
46
  end
47
47
 
48
48
  it "should be false for false" do
49
- Boolean.to_mongo(false).should be_false
49
+ Boolean.to_mongo(false).should be_falsey
50
50
  end
51
51
 
52
52
  it "should handle odd assortment of other values" do
53
- Boolean.to_mongo('true').should be_true
54
- Boolean.to_mongo('t').should be_true
55
- Boolean.to_mongo('1').should be_true
56
- Boolean.to_mongo(1).should be_true
53
+ Boolean.to_mongo('true').should be_truthy
54
+ Boolean.to_mongo('t').should be_truthy
55
+ Boolean.to_mongo('1').should be_truthy
56
+ Boolean.to_mongo(1).should be_truthy
57
57
 
58
- Boolean.to_mongo('false').should be_false
59
- Boolean.to_mongo('f').should be_false
60
- Boolean.to_mongo('0').should be_false
61
- Boolean.to_mongo(0).should be_false
58
+ Boolean.to_mongo('false').should be_falsey
59
+ Boolean.to_mongo('f').should be_falsey
60
+ Boolean.to_mongo('0').should be_falsey
61
+ Boolean.to_mongo(0).should be_falsey
62
62
  end
63
63
 
64
64
  it "should be nil for nil" do
@@ -68,11 +68,11 @@ describe "Support" do
68
68
 
69
69
  context "Boolean.from_mongo" do
70
70
  it "should be true for true" do
71
- Boolean.from_mongo(true).should be_true
71
+ Boolean.from_mongo(true).should be_truthy
72
72
  end
73
73
 
74
74
  it "should be false for false" do
75
- Boolean.from_mongo(false).should be_false
75
+ Boolean.from_mongo(false).should be_falsey
76
76
  end
77
77
 
78
78
  it "should be nil for nil" do
@@ -153,7 +153,7 @@ describe "Support" do
153
153
  it "should be hash if nil" do
154
154
  hash = Hash.from_mongo(nil)
155
155
  hash.should == {}
156
- hash.is_a?(HashWithIndifferentAccess).should be_true
156
+ hash.is_a?(HashWithIndifferentAccess).should be_truthy
157
157
  end
158
158
  end
159
159
 
@@ -359,7 +359,7 @@ describe "Support" do
359
359
 
360
360
  time = Time.from_mongo(Time.utc(2009, 10, 1))
361
361
  time.should == Time.zone.local(2009, 9, 30, 14)
362
- time.is_a?(ActiveSupport::TimeWithZone).should be_true
362
+ time.is_a?(ActiveSupport::TimeWithZone).should be_truthy
363
363
 
364
364
  Time.zone = nil
365
365
  end
@@ -387,8 +387,32 @@ describe "Support" do
387
387
 
388
388
  it "should support ruby driver syntax also" do
389
389
  id = BSON::ObjectId.new
390
- id.original_to_json.should == %Q({"$oid": "#{id}"})
390
+ id.original_to_json.should == %Q({"$oid":"#{id}"})
391
391
  end
392
392
  end
393
393
  end
394
+
395
+ context "Symbol.to_mongo" do
396
+ it "should convert value to_sym" do
397
+ Symbol.to_mongo('asdfasdfasdf').should == :asdfasdfasdf
398
+ end
399
+
400
+ it "should convert string if not string" do
401
+ Symbol.to_mongo(123).should == :'123'
402
+ end
403
+
404
+ it "should return nil for nil" do
405
+ Symbol.to_mongo(nil).should be_nil
406
+ end
407
+ end
408
+
409
+ context "Symbol.from_mongo" do
410
+ it "should convert value to_sym" do
411
+ Symbol.from_mongo(:asdfasdfasdf).should == :asdfasdfasdf
412
+ end
413
+
414
+ it "should return nil for nil" do
415
+ Symbol.from_mongo(nil).should be_nil
416
+ end
417
+ end
394
418
  end
@@ -19,116 +19,85 @@ module IdentityMapSpec
19
19
  end.to_app
20
20
  end
21
21
 
22
- context "" do
23
- before do
24
- @enabled = MongoMapper::Plugins::IdentityMap.enabled
25
- MongoMapper::Plugins::IdentityMap.enabled = false
26
- end
22
+ before do
23
+ @enabled = MongoMapper::Plugins::IdentityMap.enabled
24
+ MongoMapper::Plugins::IdentityMap.enabled = false
25
+ end
27
26
 
28
- after do
29
- MongoMapper::Plugins::IdentityMap.enabled = @enabled
30
- end
27
+ after do
28
+ MongoMapper::Plugins::IdentityMap.enabled = @enabled
29
+ end
31
30
 
32
- it "should delegate" do
33
- called = false
34
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
35
- called = true
36
- [200, {}, nil]
37
- }
38
- mw.call({})
39
- called.should be_true
40
- end
31
+ it "should delegate" do
32
+ called = false
33
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
34
+ called = true
35
+ [200, {}, nil]
36
+ }
37
+ mw.call({})
38
+ called.should be_truthy
39
+ end
41
40
 
42
- it "should enable identity map during delegation" do
43
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
44
- MongoMapper::Plugins::IdentityMap.should be_enabled
45
- [200, {}, nil]
46
- }
47
- mw.call({})
48
- end
41
+ it "should enable identity map during delegation" do
42
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
43
+ MongoMapper::Plugins::IdentityMap.should be_enabled
44
+ [200, {}, nil]
45
+ }
46
+ mw.call({})
47
+ end
49
48
 
50
- class Enum < Struct.new(:iter)
51
- def each(&b)
52
- iter.call(&b)
53
- end
49
+ class Enum < Struct.new(:iter)
50
+ def each(&b)
51
+ iter.call(&b)
54
52
  end
53
+ end
55
54
 
56
- it "should enable IM for body each" do
57
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
58
- [200, {}, Enum.new(lambda { |&b|
59
- MongoMapper::Plugins::IdentityMap.should be_enabled
60
- b.call "hello"
61
- })]
62
- }
63
- body = mw.call({}).last
64
- body.each { |x| x.should eql('hello') }
65
- end
55
+ it "should enable IM for body each" do
56
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
57
+ [200, {}, Enum.new(lambda { |&b|
58
+ MongoMapper::Plugins::IdentityMap.should be_enabled
59
+ b.call "hello"
60
+ })]
61
+ }
62
+ body = mw.call({}).last
63
+ body.each { |x| x.should eql('hello') }
64
+ end
66
65
 
67
- it "should disable IM after body close" do
68
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
69
- body = mw.call({}).last
70
- MongoMapper::Plugins::IdentityMap.should be_enabled
71
- body.close
72
- MongoMapper::Plugins::IdentityMap.should_not be_enabled
73
- end
66
+ it "should disable IM after body close" do
67
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
68
+ body = mw.call({}).last
69
+ MongoMapper::Plugins::IdentityMap.should be_enabled
70
+ body.close
71
+ MongoMapper::Plugins::IdentityMap.should_not be_enabled
72
+ end
74
73
 
75
- it "should clear IM after body close" do
76
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
77
- body = mw.call({}).last
74
+ it "should clear IM after body close" do
75
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
76
+ body = mw.call({}).last
78
77
 
79
- MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
80
- MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
78
+ MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
79
+ MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
81
80
 
82
- body.close
81
+ body.close
83
82
 
84
- MongoMapper::Plugins::IdentityMap.repository.should be_empty
85
- end
83
+ MongoMapper::Plugins::IdentityMap.repository.should be_empty
84
+ end
86
85
 
87
- context "with a successful request" do
88
- it "should clear the identity map" do
89
- MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
90
- get '/'
91
- end
86
+ context "with a successful request" do
87
+ it "should clear the identity map" do
88
+ # for some reason, body.close gets called twice - once in rack-2.2.3/lib/rack/response.rb:281
89
+ # the other time rack-test-0.8.3/lib/rack/mock_session.rb:32
90
+ # use at_least(:twice) here to handle the error happening more than twice
91
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).at_least(:twice)
92
+ get '/'
92
93
  end
94
+ end
93
95
 
94
- context "when the request raises an error" do
95
- it "should clear the identity map" do
96
- MongoMapper::Plugins::IdentityMap.should_receive(:clear).once
97
- get '/fail' rescue nil
98
- end
96
+ context "when the request raises an error" do
97
+ it "should clear the identity map" do
98
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).once
99
+ get '/fail' rescue nil
99
100
  end
100
101
  end
101
102
  end
102
-
103
- # describe "IdentityMapMiddleware" do
104
- # include Rack::Test::Methods
105
-
106
- # def app
107
- # @app ||= Rack::Builder.new do
108
- # use MongoMapper::Middleware::IdentityMap
109
- # map "/" do
110
- # run lambda {|env| [200, {}, []] }
111
- # end
112
- # map "/fail" do
113
- # run lambda {|env| raise "FAIL!" }
114
- # end
115
- # end.to_app
116
- # end
117
-
118
- # context "with a successful request" do
119
- # it "should clear the identity map" do
120
- # MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
121
- # get '/'
122
- # end
123
- # end
124
-
125
- # context "when the request raises an error" do
126
- # it "should clear the identity map" do
127
- # MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
128
- # get '/fail' rescue nil
129
- # end
130
- # end
131
-
132
-
133
- # end
134
- end
103
+ end
@@ -1,6 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  class FooType < Struct.new(:bar)
5
4
  def self.to_mongo(value)
6
5
  'to_mongo'
@@ -50,29 +49,29 @@ describe "Key" do
50
49
  key = Key.new(:foo, String, :required => true)
51
50
  key.name.should == 'foo'
52
51
  key.type.should == String
53
- key.options[:required].should be_true
52
+ key.options[:required].should be_truthy
54
53
  end
55
54
 
56
55
  it "should work with name and options" do
57
56
  key = Key.new(:foo, :required => true)
58
57
  key.name.should == 'foo'
59
- key.options[:required].should be_true
58
+ key.options[:required].should be_truthy
60
59
  end
61
60
 
62
61
  it "should not permit reserved names" do
63
- expect { Key.new(:id) }.to raise_error(/reserved/)
62
+ lambda { Key.new(:id) }.should raise_error(/reserved/)
64
63
  end
65
64
 
66
65
  it "should not permit bad names" do
67
- expect { Key.new(:"id.bar") }.to raise_error(/must match/)
66
+ lambda { Key.new(:"id.bar") }.should raise_error(/must match/)
68
67
  end
69
68
 
70
69
  it "should permit bad names if __dynamic" do
71
- expect { Key.new(:"id.bar", :__dynamic => true) }.to_not raise_error
70
+ lambda { Key.new(:"id.bar", :__dynamic => true) }.should_not raise_error
72
71
  end
73
72
 
74
73
  it "should permit bad names if it is not to create accessors" do
75
- expect { Key.new(:"id.bar", :accessors => :skip) }.to_not raise_error
74
+ lambda { Key.new(:"id.bar", :accessors => :skip) }.should_not raise_error
76
75
  end
77
76
  end
78
77
 
@@ -90,20 +89,20 @@ describe "Key" do
90
89
  end
91
90
 
92
91
  it "should know if it is a embedded_document" do
93
- Key.new(:name, EDoc()).embeddable?.should be_true
92
+ Key.new(:name, EDoc()).embeddable?.should be_truthy
94
93
  end
95
94
 
96
95
  it "should know if it is not a embedded_document" do
97
- Key.new(:name, String).embeddable?.should be_false
96
+ Key.new(:name, String).embeddable?.should be_falsey
98
97
  end
99
98
 
100
99
  it "should know if it is a number" do
101
- Key.new(:age, Integer).number?.should be_true
102
- Key.new(:age, Float).number?.should be_true
100
+ Key.new(:age, Integer).number?.should be_truthy
101
+ Key.new(:age, Float).number?.should be_truthy
103
102
  end
104
103
 
105
104
  it "should know if it is not a number" do
106
- Key.new(:age, String).number?.should be_false
105
+ Key.new(:age, String).number?.should be_falsey
107
106
  end
108
107
  end
109
108
 
@@ -148,31 +147,34 @@ describe "Key" do
148
147
 
149
148
  context "with :read" do
150
149
  let(:accessor) { :read }
151
- its(:read_accessor?) { should be_true }
152
- its(:write_accessor?) { should be_false }
153
- its(:predicate_accessor?) { should be_false }
150
+
151
+ it { subject.read_accessor?.should be_truthy }
152
+ it { subject.write_accessor?.should be_falsey }
153
+ it { subject.predicate_accessor?.should be_falsey }
154
154
  end
155
155
 
156
156
  context "with :write" do
157
157
  let(:accessor) { :write }
158
- its(:read_accessor?) { should be_false }
159
- its(:write_accessor?) { should be_true }
160
- its(:predicate_accessor?) { should be_false }
158
+
159
+ it { subject.read_accessor?.should be_falsey }
160
+ it { subject.write_accessor?.should be_truthy }
161
+ it { subject.predicate_accessor?.should be_falsey }
161
162
  end
162
163
 
163
164
  context "with :predicate" do
164
165
  let(:accessor) { :predicate }
165
- its(:read_accessor?) { should be_false }
166
- its(:write_accessor?) { should be_false }
167
- its(:predicate_accessor?) { should be_true }
166
+
167
+ it { subject.read_accessor?.should be_falsey }
168
+ it { subject.write_accessor?.should be_falsey }
169
+ it { subject.predicate_accessor?.should be_truthy }
168
170
  end
169
171
 
170
172
  context "with an array of options" do
171
173
  let(:accessor) { [:read, :write] }
172
174
 
173
- its(:read_accessor?) { should be_true }
174
- its(:write_accessor?) { should be_true }
175
- its(:predicate_accessor?) { should be_false }
175
+ it { subject.read_accessor?.should be_truthy }
176
+ it { subject.write_accessor?.should be_truthy }
177
+ it { subject.predicate_accessor?.should be_falsey }
176
178
  end
177
179
  end
178
180
 
@@ -254,11 +256,11 @@ describe "Key" do
254
256
  end
255
257
 
256
258
  it "should work with Boolean type and false value" do
257
- Key.new(:active, Boolean, :default => false).default_value.should be_false
259
+ Key.new(:active, Boolean, :default => false).default_value.should be_falsey
258
260
  end
259
261
 
260
262
  it "should work with Boolean type and true value" do
261
- Key.new(:active, Boolean, :default => true).default_value.should be_true
263
+ Key.new(:active, Boolean, :default => true).default_value.should be_truthy
262
264
  end
263
265
 
264
266
  it "should work with Array values" do
@@ -1,6 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  describe "Key" do
5
4
  context ".new with no id and _id of type integer" do
6
5
  it "should not error" do
@@ -9,18 +8,18 @@ describe "Key" do
9
8
  key :_id, Integer
10
9
  end
11
10
  # No sensible default id for integer, people better pass them in if they user this
12
- silence_stderr { klass.new.id.should be_nil }
11
+ suppress_stderr { klass.new.id.should be_nil }
13
12
  }.should_not raise_error
14
13
  end
15
14
  end
16
15
 
17
16
  context ".key?(:symbol)" do
18
17
  it "should be true if document has key" do
19
- Address.key?(:city).should be_true
18
+ Address.key?(:city).should be_truthy
20
19
  end
21
20
 
22
21
  it "should be false if document does not have key" do
23
- Address.key?(:foo).should be_false
22
+ Address.key?(:foo).should be_falsey
24
23
  end
25
24
  end
26
25
 
@@ -59,11 +58,11 @@ describe "Key" do
59
58
 
60
59
  context ".key?('string')" do
61
60
  it "should be true if document has key" do
62
- Address.key?('city').should be_true
61
+ Address.key?('city').should be_truthy
63
62
  end
64
63
 
65
64
  it "should be false if document does not have key" do
66
- Address.key?('foo').should be_false
65
+ Address.key?('foo').should be_falsey
67
66
  end
68
67
  end
69
68
 
@@ -82,11 +81,11 @@ describe "Key" do
82
81
  end
83
82
 
84
83
  it "should use []= for keys instead of public writer" do
85
- expect {
84
+ lambda {
86
85
  doc = @klass.first
87
86
  doc.user['id'].should == 1
88
87
  doc.user['name'].should == 'John Nunemaker'
89
- }.to_not raise_error
88
+ }.should_not raise_error
90
89
  end
91
90
  end
92
91
 
@@ -141,15 +140,25 @@ describe "Key" do
141
140
  key :value, Integer, :default => lambda { counter += 1 }
142
141
  end
143
142
 
144
- expect { instance = klass.create }.to change { counter }.by(1)
145
- expect {
143
+ lambda { instance = klass.create }.should change { counter }.by(1)
144
+ lambda {
146
145
  instance.reload.value.should == 1
147
146
 
148
147
  instance.value = 10
149
148
  instance.save
150
149
 
151
150
  instance.reload.value.should == 10
152
- }.to_not change { counter }
151
+ }.should_not change { counter }
152
+ end
153
+ end
154
+
155
+ context "with attributes key" do
156
+ it "should raise an error" do
157
+ lambda do
158
+ klass = Doc do
159
+ key :attributes, Hash
160
+ end
161
+ end.should raise_error("`attributes` is a reserved key name")
153
162
  end
154
163
  end
155
164
  end # KeyTest