mongo_doc 0.6.26 → 0.6.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/README.textile +14 -203
  2. data/Rakefile +0 -134
  3. data/lib/mongo_doc/collection.rb +4 -0
  4. data/lib/mongo_doc/finders.rb +1 -0
  5. data/lib/mongo_doc/timestamps.rb +4 -3
  6. data/lib/mongo_doc/version.rb +3 -0
  7. data/lib/mongo_doc.rb +0 -5
  8. metadata +25 -212
  9. data/.document +0 -5
  10. data/.gitignore +0 -8
  11. data/.rspec +0 -2
  12. data/.rvmrc +0 -1
  13. data/.watchr +0 -32
  14. data/Gemfile +0 -3
  15. data/Gemfile.lock +0 -72
  16. data/HISTORY.md +0 -11
  17. data/VERSION +0 -1
  18. data/examples/simple_document.rb +0 -46
  19. data/examples/simple_object.rb +0 -34
  20. data/features/collections.feature +0 -9
  21. data/features/connection.feature +0 -13
  22. data/features/db_references.feature +0 -21
  23. data/features/embed_hash.feature +0 -16
  24. data/features/finders.feature +0 -76
  25. data/features/indexes.feature +0 -28
  26. data/features/mongodb.yml +0 -7
  27. data/features/mongodoc_base.feature +0 -128
  28. data/features/new_record.feature +0 -36
  29. data/features/partial_updates.feature +0 -95
  30. data/features/polymorphic_collections.feature +0 -16
  31. data/features/references.feature +0 -28
  32. data/features/removing_documents.feature +0 -68
  33. data/features/saving_an_object.feature +0 -15
  34. data/features/scopes.feature +0 -66
  35. data/features/step_definitions/collection_steps.rb +0 -17
  36. data/features/step_definitions/connection_steps.rb +0 -13
  37. data/features/step_definitions/document_steps.rb +0 -155
  38. data/features/step_definitions/documents.rb +0 -61
  39. data/features/step_definitions/embed_hash_steps.rb +0 -6
  40. data/features/step_definitions/field_steps.rb +0 -28
  41. data/features/step_definitions/finder_steps.rb +0 -15
  42. data/features/step_definitions/index_steps.rb +0 -10
  43. data/features/step_definitions/json_steps.rb +0 -9
  44. data/features/step_definitions/object_steps.rb +0 -50
  45. data/features/step_definitions/objects.rb +0 -24
  46. data/features/step_definitions/partial_update_steps.rb +0 -31
  47. data/features/step_definitions/query_steps.rb +0 -69
  48. data/features/step_definitions/removing_documents_steps.rb +0 -14
  49. data/features/step_definitions/scope_steps.rb +0 -18
  50. data/features/step_definitions/string_casting_steps.rb +0 -29
  51. data/features/step_definitions/util_steps.rb +0 -7
  52. data/features/string_casting.feature +0 -10
  53. data/features/support/support.rb +0 -12
  54. data/features/timestamps.feature +0 -14
  55. data/features/using_criteria.feature +0 -148
  56. data/mongo_doc.gemspec +0 -272
  57. data/mongodb.example.yml +0 -14
  58. data/perf/mongo_doc_object.rb +0 -83
  59. data/perf/mongo_document.rb +0 -84
  60. data/perf/ruby_driver.rb +0 -49
  61. data/script/console +0 -8
  62. data/spec/active_model_behavior.rb +0 -64
  63. data/spec/array_including_argument_matcher.rb +0 -62
  64. data/spec/associations/collection_proxy_spec.rb +0 -233
  65. data/spec/associations/document_proxy_spec.rb +0 -45
  66. data/spec/associations/hash_proxy_spec.rb +0 -181
  67. data/spec/associations/proxy_base_spec.rb +0 -92
  68. data/spec/associations_spec.rb +0 -218
  69. data/spec/attributes_accessor_spec.rb +0 -39
  70. data/spec/attributes_spec.rb +0 -184
  71. data/spec/bson_matchers.rb +0 -54
  72. data/spec/bson_spec.rb +0 -196
  73. data/spec/collection_spec.rb +0 -169
  74. data/spec/connection_spec.rb +0 -169
  75. data/spec/contexts/ids_spec.rb +0 -49
  76. data/spec/contexts/mongo_spec.rb +0 -241
  77. data/spec/contexts_spec.rb +0 -56
  78. data/spec/criteria/optional_spec.rb +0 -26
  79. data/spec/criteria_spec.rb +0 -73
  80. data/spec/cursor_spec.rb +0 -91
  81. data/spec/database_cleaner_spec.rb +0 -24
  82. data/spec/document_ext.rb +0 -9
  83. data/spec/document_spec.rb +0 -569
  84. data/spec/embedded_save_spec.rb +0 -73
  85. data/spec/ext_spec.rb +0 -100
  86. data/spec/finders_spec.rb +0 -61
  87. data/spec/hash_matchers.rb +0 -29
  88. data/spec/index_spec.rb +0 -79
  89. data/spec/matchers_spec.rb +0 -342
  90. data/spec/mongodb.yml +0 -6
  91. data/spec/mongodb_pairs.yml +0 -8
  92. data/spec/new_record_spec.rb +0 -128
  93. data/spec/polymorphic_collection_spec.rb +0 -47
  94. data/spec/references_many_spec.rb +0 -214
  95. data/spec/references_spec.rb +0 -121
  96. data/spec/root_spec.rb +0 -41
  97. data/spec/scope_spec.rb +0 -88
  98. data/spec/spec_helper.rb +0 -16
  99. data/spec/timestamps_spec.rb +0 -154
  100. data/spec/update_spec.rb +0 -167
  101. data/spec/validations_spec.rb +0 -30
@@ -1,218 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Associations" do
4
-
5
- context ".embed" do
6
- class TestDoc
7
- include MongoDoc::Document
8
-
9
- embed :sub_doc
10
- end
11
-
12
- class SubDoc
13
- include MongoDoc::Document
14
-
15
- attr_accessor :data
16
- end
17
-
18
- class TestHasOne
19
- include MongoDoc::Document
20
-
21
- has_one :sub_doc
22
- end
23
-
24
- let(:subdoc) { SubDoc.new }
25
- let(:doc) { TestDoc.new(:sub_doc => subdoc) }
26
-
27
- it "uses a proxy" do
28
- MongoDoc::Associations::DocumentProxy.should === doc.sub_doc
29
- end
30
-
31
- it ".has_one is an alias for embed" do
32
- MongoDoc::Associations::DocumentProxy.should === TestHasOne.new(:sub_doc => SubDoc.new).sub_doc
33
- end
34
-
35
- it "set the subdocuments root" do
36
- doc.should == subdoc._root
37
- end
38
-
39
- context "validations" do
40
- class EmbedValidationTest
41
- include MongoDoc::Document
42
-
43
- attr_accessor :data
44
- validates_presence_of :data
45
- end
46
-
47
- it "cascades validations down" do
48
- invalid = EmbedValidationTest.new
49
- TestDoc.new(:sub_doc => invalid).should have(1).error_on(:sub_doc)
50
- end
51
- end
52
- end
53
-
54
- context ".embed_many" do
55
-
56
- class SubEmbedManyDoc
57
- include MongoDoc::Document
58
-
59
- attr_accessor :data
60
- end
61
-
62
- class TestEmbedManyDoc
63
- include MongoDoc::Document
64
-
65
- embed_many :sub_docs, :class_name => 'SubEmbedManyDoc'
66
- end
67
-
68
- class TestEmbedManyDoc2
69
- include MongoDoc::Document
70
-
71
- embed_many :sub_docs, :class_name => :sub_embed_many_doc
72
- end
73
-
74
- class TestImplicitEmbedManyDoc
75
- include MongoDoc::Document
76
-
77
- embed_many :sub_embed_many_docs
78
- end
79
-
80
- class TestHasManyDoc
81
- include MongoDoc::Document
82
-
83
- has_many :sub_docs, :class_name => 'SubEmbedManyDoc'
84
- end
85
-
86
- let(:subdoc) { SubEmbedManyDoc.new }
87
- let(:doc) { TestEmbedManyDoc.new(:sub_docs => [subdoc]) }
88
-
89
- it "uses a proxy" do
90
- MongoDoc::Associations::CollectionProxy.should === TestEmbedManyDoc.new.sub_docs
91
- end
92
-
93
- it ".has_many is an alias for .embed_many" do
94
- MongoDoc::Associations::CollectionProxy.should === TestHasManyDoc.new.sub_docs
95
- end
96
-
97
- it "set the subdocuments root to the root" do
98
- doc.should == subdoc._root
99
- end
100
-
101
- it "uses the association name to find the children's class name" do
102
- TestImplicitEmbedManyDoc.new.sub_embed_many_docs._assoc_class.should == SubEmbedManyDoc
103
- end
104
-
105
- it "uses class_name attribute for the children's class name" do
106
- TestEmbedManyDoc.new.sub_docs._assoc_class.should == SubEmbedManyDoc
107
- end
108
-
109
- it "uses class_name attribute for the children's class name" do
110
- TestEmbedManyDoc2.new.sub_docs._assoc_class.should == SubEmbedManyDoc
111
- end
112
-
113
- context "validations" do
114
- class EmbedManyValidationChild
115
- include MongoDoc::Document
116
-
117
- attr_accessor :data
118
- validates_presence_of :data
119
- end
120
-
121
- class EmbedManyValidationTest
122
- include MongoDoc::Document
123
-
124
- embed_many :subdocs, :class_name => 'HasManyValidationChild'
125
- end
126
-
127
- let(:invalid_child) { EmbedManyValidationChild.new }
128
- let(:doc) { EmbedManyValidationTest.new(:subdocs => [invalid_child]) }
129
-
130
- it "cascades validations and marks it in the parent" do
131
- doc.should have(1).error_on(:subdocs)
132
- end
133
-
134
- it "cascades validations and marks it in the child" do
135
- invalid_child.should have(1).error_on(:data)
136
- end
137
-
138
- it "ignores non-document children" do
139
- EmbedManyValidationTest.new(:subdocs => ['not a doc']).should be_valid
140
- end
141
- end
142
- end
143
-
144
- context ".embed_hash" do
145
- class SubEmbedHashDoc
146
- include MongoDoc::Document
147
-
148
- attr_accessor :data
149
- end
150
-
151
- class TestEmbedHashDoc
152
- include MongoDoc::Document
153
-
154
- embed_hash :sub_docs, :class_name => 'SubEmbedHashDoc'
155
- end
156
-
157
- class TestImplicitEmbedHashDoc
158
- include MongoDoc::Document
159
-
160
- embed_hash :sub_embed_hash_docs
161
- end
162
-
163
- class TestHasHashDoc
164
- include MongoDoc::Document
165
-
166
- has_hash :sub_embed_hash_docs
167
- end
168
-
169
- let(:subdoc) { SubEmbedHashDoc.new }
170
- let(:doc) { TestEmbedHashDoc.new(:sub_docs => {:key => subdoc}) }
171
-
172
- it "uses a proxy" do
173
- MongoDoc::Associations::HashProxy.should === TestEmbedHashDoc.new.sub_docs
174
- end
175
-
176
- it ".has_hash is an alias for embed_hash" do
177
- MongoDoc::Associations::HashProxy.should === TestEmbedHashDoc.new.sub_docs
178
- end
179
-
180
- it "set the subdocuments root to the root" do
181
- doc.should == subdoc._root
182
- end
183
-
184
- it "uses the association name to find the children's class name" do
185
- TestImplicitEmbedHashDoc.new.sub_embed_hash_docs._assoc_class.should == SubEmbedHashDoc
186
- end
187
-
188
- context "validations" do
189
- class EmbedHashValidationChild
190
- include MongoDoc::Document
191
-
192
- attr_accessor :data
193
- validates_presence_of :data
194
- end
195
-
196
- class EmbedHashValidationTest
197
- include MongoDoc::Document
198
-
199
- embed_hash :subdocs, :class_name => 'EmbedHashValidationChild'
200
- end
201
-
202
- let(:invalid_child) { EmbedHashValidationChild.new }
203
- let(:doc) { EmbedHashValidationTest.new(:subdocs => {:key => invalid_child}) }
204
-
205
- it "cascades validations and marks it in the parent" do
206
- doc.should have(1).error_on(:subdocs)
207
- end
208
-
209
- it "cascades validations and marks it in the child" do
210
- invalid_child.should have(1).error_on(:data)
211
- end
212
-
213
- it "ignores non-document children" do
214
- EmbedHashValidationTest.new(:subdocs => {:key => 'data'}).should be_valid
215
- end
216
- end
217
- end
218
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Attributes attributes accessor" do
4
- class AttributesTest
5
- include MongoDoc::Attributes
6
-
7
- attr_accessor :name
8
- attr_accessor :age
9
- attr_accessor :birthdate, :type => Date
10
- end
11
-
12
- context "#attributes" do
13
- subject do
14
- AttributesTest.new.attributes
15
- end
16
-
17
- it "returns a hash of the given attributes" do
18
- should have_key(:name)
19
- should have_key(:age)
20
- should have_key(:birthdate)
21
- end
22
- end
23
-
24
- context "#attributes=" do
25
- let(:object) { AttributesTest.new }
26
-
27
- it "sets attributes from a hash" do
28
- name = 'name'
29
- object.attributes = {:name => name}
30
- object.name.should == name
31
- end
32
-
33
- it "ignores a nil" do
34
- expect do
35
- object.attributes = nil
36
- end.should_not raise_error
37
- end
38
- end
39
- end
@@ -1,184 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Attributes" do
4
- class AttributesTest
5
- include MongoDoc::Attributes
6
- end
7
-
8
- it "defines _id attribute" do
9
- AttributesTest.new.should respond_to(:_id)
10
- AttributesTest.new.should respond_to(:_id=)
11
- end
12
-
13
- context ".key" do
14
- class AttributeAccessorTest
15
- include MongoDoc::Attributes
16
-
17
- key :date, :default => Date.today, :type => Date
18
- end
19
-
20
- it "is an alias for attr_accessor" do
21
- AttributeAccessorTest._keys.should include(:date)
22
- end
23
- end
24
-
25
- context ".attr_accessor" do
26
- class TestKeys
27
- include MongoDoc::Attributes
28
-
29
- attr_accessor :attr1, :attr2
30
- attr_accessor :validation_context
31
- attr_accessor :boolean, :type => Boolean
32
- end
33
-
34
- it "adds its arguments to _keys" do
35
- TestKeys._keys.should include(:attr1, :attr2)
36
- end
37
-
38
- context "the :validation_context attribute from ActiveModel" do
39
-
40
- it "is an attribute" do
41
- TestKeys.new.should respond_to(:validation_context)
42
- TestKeys.new.should respond_to(:validation_context=)
43
- end
44
-
45
- it "is not in _keys" do
46
- TestKeys._keys.should_not include(:validation_context)
47
- end
48
- end
49
-
50
- describe "accessors" do
51
- subject do
52
- TestKeys.new
53
- end
54
-
55
- it "has an attr1 reader" do
56
- should respond_to(:attr1)
57
- end
58
-
59
- it "has an attr1 writer" do
60
- should respond_to(:attr1=)
61
- end
62
-
63
- it "has a reader for a boolean" do
64
- should respond_to(:boolean)
65
- end
66
-
67
- it "has a ? reader for a boolean" do
68
- should respond_to(:boolean?)
69
- end
70
- end
71
-
72
- context "default values" do
73
- class TestDefault
74
- include MongoDoc::Attributes
75
-
76
- attr_accessor :with_default, :default => 'value'
77
- attr_accessor :boolean_default, :type => Boolean, :default => false
78
- end
79
-
80
- let(:object) { TestDefault.new }
81
-
82
- it "uses the default value" do
83
- object.with_default.should == 'value'
84
- end
85
-
86
- it "only uses the default value once" do
87
- object.with_default.should == 'value'
88
- class << object
89
- def _default_with_default
90
- 'other value'
91
- end
92
- end
93
- object.with_default.should == 'value'
94
- end
95
-
96
- it "does not set the default value if the setter is invoked first" do
97
- object.with_default = nil
98
- object.with_default.should be_nil
99
- end
100
-
101
- it "allows a false default for booleans" do
102
- object.boolean_default.should == false
103
- end
104
- end
105
-
106
- context "specified type" do
107
- class TestType
108
- include MongoDoc::Attributes
109
-
110
- attr_accessor :birthdate, :type => Date
111
- end
112
-
113
- let(:object) { TestType.new }
114
-
115
- it "does not call Type.cast_from_string when the set value is not a string" do
116
- Date.should_not_receive :cast_from_string
117
- object.birthdate = Date.today
118
- end
119
-
120
- context "when the accessor is set with a string" do
121
- let(:date) { Date.today }
122
-
123
- it "delegates to Type.cast_from_string to set the value" do
124
- Date.should_receive(:cast_from_string).with(date.to_s)
125
- object.birthdate = date.to_s
126
- end
127
-
128
- it "sets the value to the result of the cast" do
129
- object.birthdate = date.to_s
130
- object.birthdate.should == date
131
- end
132
- end
133
-
134
- context "when a namespace collision exists" do
135
- class TestType
136
- attr_accessor :bson_id, :type => ::BSON::ObjectID
137
- end
138
-
139
- let(:bson_id) { ::BSON::ObjectID.new }
140
- it "sets the value to the result of the cast" do
141
- object.bson_id = bson_id.to_s
142
- object.bson_id.should == bson_id
143
- end
144
- end
145
- end
146
-
147
- describe "used with inheritance" do
148
- class TestParent
149
- include MongoDoc::Attributes
150
-
151
- attr_accessor :parent_attr
152
- end
153
-
154
- class TestChild < TestParent
155
- attr_accessor :child_attr
156
- end
157
-
158
- it "has its own keys" do
159
- TestChild._keys.should include(:child_attr)
160
- end
161
-
162
- it "has the keys from the parent class" do
163
- TestChild._keys.should include(*TestParent._keys)
164
- end
165
-
166
- it "does not add keys to the parent class" do
167
- TestParent._keys.should_not include(:child_attr)
168
- end
169
- end
170
- end
171
-
172
- context "._attributes" do
173
- class TestHasOneDoc
174
- include MongoDoc::Document
175
-
176
- attr_accessor :key
177
- embed :embed
178
- end
179
-
180
- it "is _keys + _associations" do
181
- TestHasOneDoc._attributes.should == TestHasOneDoc._keys + TestHasOneDoc._associations
182
- end
183
- end
184
- end
@@ -1,54 +0,0 @@
1
- module BsonMatchers
2
- class BeBsonEql
3
- def initialize(expected)
4
- @expected = expected
5
- end
6
-
7
- def matches?(target)
8
- @target = target
9
- @target == @expected
10
- end
11
-
12
- def failure_message
13
- "expected\...#{@target.inspect}\n" +
14
- "to be BSON code equivalent to\...#{@expected.inspect}\n" +
15
- "Difference:\...#{@expected.diff(@target).inspect}"
16
- end
17
-
18
- def negative_failure_message
19
- "expected\...#{@target.inspect}\n" +
20
- "to be BSON code different from\...#{@expected.inspect}"
21
- end
22
- end
23
-
24
- class BeMongoEql
25
- def initialize(expected, include_id)
26
- @include_id = include_id
27
- @expected = include_id ? expected : expected.except('_id')
28
- end
29
-
30
- def matches?(target)
31
- @target = @include_id ? target : target.except('_id')
32
- @target == @expected
33
- end
34
-
35
- def failure_message
36
- "expected\...#{@target.inspect}\n" +
37
- "to be BSON code equivalent to\...#{@expected.inspect}\n" +
38
- "Difference:\...#{@expected.diff(@target).inspect}"
39
- end
40
-
41
- def negative_failure_message
42
- "expected\...#{@target.inspect}\n" +
43
- "to be BSON code different from\...#{@expected.inspect}"
44
- end
45
- end
46
-
47
- def be_bson_eql(expected)
48
- BeBsonEql.new(expected)
49
- end
50
-
51
- def be_mongo_eql(expected, include_id = true)
52
- BeMongoEql.new(expected, include_id)
53
- end
54
- end
data/spec/bson_spec.rb DELETED
@@ -1,196 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "BSON for Mongo (BSON)" do
4
- describe "#decode" do
5
- it "just returns the json if the :raw_json option is used" do
6
- hash = {}
7
- MongoDoc::BSON.should_not_receive(:bson_create)
8
- MongoDoc::BSON.decode(hash, :raw_json => true).should == hash
9
- end
10
-
11
- it "calls bson_create if parameter is a hash" do
12
- hash = {}
13
- options = {:option => true}
14
- MongoDoc::BSON.should_receive(:bson_create).with(hash, options)
15
- MongoDoc::BSON.decode(hash, options)
16
- end
17
-
18
- it "if parameter is an array, it calls array_create" do
19
- array = []
20
- options = {:option => true}
21
- MongoDoc::BSON.should_receive(:array_create).with(array, options)
22
- MongoDoc::BSON.decode(array, options)
23
- end
24
-
25
- it "returns the json value as is if the parameter is not a hash or array" do
26
- ["", 1, 1.5, true, false, nil].each do |type_value|
27
- MongoDoc::BSON.decode(type_value).should == type_value
28
- end
29
- end
30
- end
31
-
32
- describe "#array_create" do
33
- it "calls decode for each element" do
34
- first = 1
35
- array = [first]
36
- options = {:option => true}
37
- MongoDoc::BSON.should_receive(:decode).with(first, options)
38
- MongoDoc::BSON.array_create(array, options)
39
- end
40
-
41
- it "just returns the array if the :raw_json option is used" do
42
- hash = {'key' => 'value', MongoDoc::BSON::CLASS_KEY => 'Date'}
43
- array = [hash]
44
- MongoDoc::BSON.should_not_receive(:decode)
45
- MongoDoc::BSON.array_create(array, :raw_json => true).should == array
46
- end
47
- end
48
-
49
- describe "#bson_create" do
50
- it "leaves a simple hash intact" do
51
- hash = {}
52
- MongoDoc::BSON.bson_create(hash).should == hash
53
- end
54
-
55
- it "a class hash extracts the class, and calls class.bson_create" do
56
- base_hash = {'key' => 'value'}
57
- bson_hash = base_hash.merge(MongoDoc::BSON::CLASS_KEY => 'Date')
58
- Date.should_receive(:bson_create).with(base_hash, {})
59
- MongoDoc::BSON.bson_create(bson_hash)
60
- end
61
-
62
- it "ignores a class hash when the :raw_json option is used" do
63
- hash = {'key' => 'value', MongoDoc::BSON::CLASS_KEY => 'Date'}
64
- MongoDoc::BSON.bson_create(hash, :raw_json => true).should == hash
65
- end
66
- end
67
-
68
- describe "Hash" do
69
- it "#to_bson returns the hash" do
70
- hash = {'key' => 1}
71
- hash.to_bson.should == hash
72
- end
73
-
74
- it "#to_bson returns the hash with symbol keys as strings" do
75
- {:key => 1}.to_bson.should == {"key" => 1}
76
- end
77
-
78
- it "decodes to a hash" do
79
- hash = {'key' => 1}
80
- MongoDoc::BSON.decode(hash.to_bson).should == hash
81
- end
82
-
83
- it "decodes the values of the hash" do
84
- hash = {'key' => {'subkey' => Date.today}}
85
- MongoDoc::BSON.decode(hash.to_bson).should == hash
86
- end
87
- end
88
-
89
- describe "Array" do
90
- it "#to_bson returns the array" do
91
- array = ['string', 1]
92
- array.to_bson.should == array
93
- end
94
-
95
- it "#to_bson iterates over its elements" do
96
- array = []
97
- array.should_receive(:map)
98
- array.to_bson
99
- end
100
-
101
- it "decodes to an array" do
102
- array = ['string', 1]
103
- MongoDoc::BSON.decode(array.to_bson).should == array
104
- end
105
- end
106
-
107
- describe "Extensions to core classes" do
108
- it "#to_bson for objects that are BSON native return themselves" do
109
- [true, false, nil, 1.0, 1, /regexp/, 'string', :symbol, Time.now].each do |native|
110
- native.to_bson.should == native
111
- end
112
- end
113
-
114
- it "objects that are BSON native decode to themselves" do
115
- [true, false, nil, 1.0, 1, /regexp/, 'string', :symbol, Time.now].each do |native|
116
- hash = {'native' => native}
117
- MongoDoc::BSON.decode(hash.to_bson).should == hash
118
- end
119
- end
120
-
121
- it "Date#to_bson returns a date hash" do
122
- date = Date.today
123
- date.to_bson.should == {MongoDoc::BSON::CLASS_KEY => "Date", "dt" => date.strftime, "sg" => date.respond_to?(:start) ? date.start : date.sg}
124
- end
125
-
126
- it "roundtrips Date" do
127
- date = Date.today
128
- MongoDoc::BSON.decode(date.to_bson).should == date
129
- end
130
-
131
- it "DateTime#to_bson returns a datetime hash" do
132
- datetime = DateTime.now
133
- datetime.to_bson.should == {MongoDoc::BSON::CLASS_KEY => "DateTime", "dt" => datetime.strftime, "sg" => datetime.respond_to?(:start) ? datetime.start : datetime.sg}
134
- end
135
-
136
- it "roundtrips DateTime" do
137
- datetime = DateTime.now
138
- MongoDoc::BSON.decode(datetime.to_bson).to_s.should == datetime.to_s
139
- end
140
- end
141
-
142
- describe "Mongo Classes" do
143
- [BSON::ObjectID.new, BSON::DBRef.new('ns', 1), BSON::Code.new('code'), BSON::Binary.new, BSON::MinKey.new, BSON::MaxKey.new].each do |obj|
144
- it "#to_bson for #{obj.class.name} returns self" do
145
- obj.to_bson.should == obj
146
- end
147
-
148
- it "objects of type #{obj.class.name} decode to themselves" do
149
- hash = {"mongo" => obj}
150
- MongoDoc::BSON.decode(hash.to_bson)["mongo"].should == obj
151
- end
152
- end
153
- end
154
-
155
- describe "Extensions to Object" do
156
- class Simple
157
- attr_accessor :value
158
- end
159
-
160
- class Contained
161
- attr_accessor :array_of_simple
162
- end
163
-
164
- before do
165
- @value1 = 'value1'
166
- @simple1 = Simple.new
167
- @simple1.value = @value1
168
- @value2 = 'value2'
169
- @simple2 = Simple.new
170
- @simple2.value = @value2
171
- @complex = Contained.new
172
- @complex.array_of_simple = [@simple1, @simple2]
173
- end
174
-
175
- it "renders a json representation of a simple object" do
176
- @simple1.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => Simple.name, "value" => @value1})
177
- end
178
-
179
- it "renders a json representation of an object with embedded objects" do
180
- @complex.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => Contained.name, "array_of_simple" => [@simple1.to_bson, @simple2.to_bson]})
181
- end
182
-
183
- it "ignores a class hash when the :raw_json option is used" do
184
- Contained.bson_create(@complex.to_bson.except(MongoDoc::BSON::CLASS_KEY), :raw_json => true).array_of_simple.first.should == @simple1.to_bson
185
- end
186
-
187
- it "roundtrips the object" do
188
- MongoDoc::BSON.decode(@complex.to_bson).should be_kind_of(Contained)
189
- end
190
-
191
- it "allows for embedded arrays of objects" do
192
- obj = MongoDoc::BSON.decode(@complex.to_bson)
193
- obj.array_of_simple.each {|o| o.should be_kind_of(Simple)}
194
- end
195
- end
196
- end