mongo_mapper 0.13.0 → 0.15.1
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.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +61 -0
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +7 -0
- data/lib/mongo_mapper/connection.rb +16 -37
- data/lib/mongo_mapper/document.rb +4 -0
- data/lib/mongo_mapper/extensions/array.rb +14 -6
- data/lib/mongo_mapper/extensions/hash.rb +15 -3
- data/lib/mongo_mapper/extensions/object.rb +4 -0
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/extensions/string.rb +13 -5
- data/lib/mongo_mapper/extensions/symbol.rb +18 -0
- data/lib/mongo_mapper/plugins/accessible.rb +15 -5
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +27 -14
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +60 -29
- data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +36 -29
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
- data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
- data/lib/mongo_mapper/plugins/indexes.rb +14 -7
- data/lib/mongo_mapper/plugins/keys.rb +170 -151
- data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
- data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
- data/lib/mongo_mapper/plugins/persistence.rb +13 -8
- data/lib/mongo_mapper/plugins/protected.rb +6 -5
- data/lib/mongo_mapper/plugins/querying.rb +85 -42
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +20 -15
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/sci.rb +0 -0
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/railtie.rb +4 -3
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
- data/spec/examples.txt +1717 -0
- data/spec/functional/accessible_spec.rb +19 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +36 -20
- data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
- data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
- data/spec/functional/associations/one_proxy_spec.rb +10 -10
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +8 -15
- data/spec/functional/callbacks_spec.rb +89 -2
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +63 -46
- data/spec/functional/document_spec.rb +30 -5
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +17 -17
- data/spec/functional/identity_map_spec.rb +29 -16
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +86 -28
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +77 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +9 -9
- data/spec/functional/scopes_spec.rb +235 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +86 -0
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/touch_spec.rb +1 -1
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +37 -9
- data/spec/support/matchers.rb +5 -14
- data/spec/unit/associations/base_spec.rb +12 -12
- data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
- data/spec/unit/associations/many_association_spec.rb +2 -2
- data/spec/unit/associations/one_association_spec.rb +2 -2
- data/spec/unit/associations/proxy_spec.rb +19 -20
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +8 -8
- data/spec/unit/dynamic_finder_spec.rb +8 -8
- data/spec/unit/embedded_document_spec.rb +18 -19
- data/spec/unit/extensions_spec.rb +41 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +20 -11
- data/spec/unit/model_generator_spec.rb +0 -0
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/rails_spec.rb +5 -0
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +46 -33
- metadata +66 -37
- data/README.rdoc +0 -59
- data/lib/mongo_mapper/connections/10gen.rb +0 -0
- data/lib/mongo_mapper/connections/moped.rb +0 -0
- 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
|
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
|
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
|
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
|
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
|
54
|
-
Boolean.to_mongo('t').should
|
55
|
-
Boolean.to_mongo('1').should
|
56
|
-
Boolean.to_mongo(1).should
|
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
|
59
|
-
Boolean.to_mongo('f').should
|
60
|
-
Boolean.to_mongo('0').should
|
61
|
-
Boolean.to_mongo(0).should
|
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
|
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
|
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
|
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
|
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":
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
before do
|
23
|
+
@enabled = MongoMapper::Plugins::IdentityMap.enabled
|
24
|
+
MongoMapper::Plugins::IdentityMap.enabled = false
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
after do
|
28
|
+
MongoMapper::Plugins::IdentityMap.enabled = @enabled
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
49
|
+
class Enum < Struct.new(:iter)
|
50
|
+
def each(&b)
|
51
|
+
iter.call(&b)
|
54
52
|
end
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
78
|
+
MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
|
79
|
+
MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
|
81
80
|
|
82
|
-
|
81
|
+
body.close
|
83
82
|
|
84
|
-
|
85
|
-
|
83
|
+
MongoMapper::Plugins::IdentityMap.repository.should be_empty
|
84
|
+
end
|
86
85
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
data/spec/unit/key_spec.rb
CHANGED
@@ -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
|
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
|
58
|
+
key.options[:required].should be_truthy
|
60
59
|
end
|
61
60
|
|
62
61
|
it "should not permit reserved names" do
|
63
|
-
|
62
|
+
lambda { Key.new(:id) }.should raise_error(/reserved/)
|
64
63
|
end
|
65
64
|
|
66
65
|
it "should not permit bad names" do
|
67
|
-
|
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
|
-
|
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
|
-
|
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
|
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
|
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
|
102
|
-
Key.new(:age, Float).number?.should
|
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
|
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
|
-
|
152
|
-
|
153
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
166
|
-
|
167
|
-
|
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
|
-
|
174
|
-
|
175
|
-
|
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
|
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
|
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
|
data/spec/unit/keys_spec.rb
CHANGED
@@ -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
|
-
|
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
|
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
|
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
|
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
|
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
|
-
|
84
|
+
lambda {
|
86
85
|
doc = @klass.first
|
87
86
|
doc.user['id'].should == 1
|
88
87
|
doc.user['name'].should == 'John Nunemaker'
|
89
|
-
}.
|
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
|
-
|
145
|
-
|
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
|
-
}.
|
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
|