mongomodel 0.5.5 → 0.5.6
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/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
|
@@ -5,46 +5,46 @@ module MongoModel
|
|
|
5
5
|
it "inherits from EmbeddedDocument" do
|
|
6
6
|
Document.ancestors.should include(EmbeddedDocument)
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
it "has an id property" do
|
|
10
10
|
property = Document.properties[:id]
|
|
11
11
|
property.name.should == :id
|
|
12
12
|
property.as.should == '_id'
|
|
13
13
|
property.default(double('instance', :generate_id => 'abc-123')).should == 'abc-123'
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
describe "equality" do
|
|
17
17
|
define_class(:DocumentA, Document)
|
|
18
18
|
define_class(:DocumentB, Document)
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
subject { DocumentA.new(:id => 'test') }
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
it "is equal to another document of the same class with the same id" do
|
|
23
23
|
subject.should == DocumentA.new(:id => 'test')
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
it "is not equal to another document of the same class with a different id" do
|
|
27
27
|
subject.should_not == DocumentA.new(:id => 'not-test')
|
|
28
28
|
end
|
|
29
|
-
|
|
29
|
+
|
|
30
30
|
it "is not equal to another document of a different class with the same id" do
|
|
31
31
|
subject.should_not == DocumentB.new(:id => 'test')
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
describe "single collection inheritance" do
|
|
36
36
|
define_class(:Event, Document)
|
|
37
37
|
define_class(:SpecialEvent, :Event)
|
|
38
38
|
define_class(:VerySpecialEvent, :SpecialEvent)
|
|
39
39
|
define_class(:SuperSpecialEvent, :VerySpecialEvent)
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
let(:missing) do
|
|
42
42
|
e = Event.new
|
|
43
43
|
e.type = 'MissingClass'
|
|
44
44
|
e.save!
|
|
45
45
|
e
|
|
46
46
|
end
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
before(:each) do
|
|
49
49
|
@event = Event.create!
|
|
50
50
|
@special = SpecialEvent.create!
|
|
@@ -52,44 +52,44 @@ module MongoModel
|
|
|
52
52
|
@super_special = SuperSpecialEvent.create!
|
|
53
53
|
@missing = missing
|
|
54
54
|
end
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
it "belongs to the same collection as its parent" do
|
|
57
57
|
SpecialEvent.collection_name.should == Event.collection_name
|
|
58
58
|
VerySpecialEvent.collection_name.should == Event.collection_name
|
|
59
59
|
end
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
it "is an instance of the correct class when loaded" do
|
|
62
62
|
Event.find(@event.id).should be_an_instance_of(Event)
|
|
63
63
|
Event.find(@special.id).should be_an_instance_of(SpecialEvent)
|
|
64
64
|
Event.find(@very_special.id).should be_an_instance_of(VerySpecialEvent)
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
SpecialEvent.find(@special.id).should be_an_instance_of(SpecialEvent)
|
|
67
67
|
SpecialEvent.find(@very_special.id).should be_an_instance_of(VerySpecialEvent)
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
VerySpecialEvent.find(@very_special.id).should be_an_instance_of(VerySpecialEvent)
|
|
70
70
|
end
|
|
71
|
-
|
|
71
|
+
|
|
72
72
|
it "defaults to superclass type if type missing" do
|
|
73
73
|
Event.find(@missing.id).should be_an_instance_of(Event)
|
|
74
74
|
end
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
it "considers document missing when finding from subclass using id of parent instance" do
|
|
77
77
|
lambda { SpecialEvent.find(@event.id) }.should raise_error(MongoModel::DocumentNotFound)
|
|
78
78
|
lambda { VerySpecialEvent.find(@special.id) }.should raise_error(MongoModel::DocumentNotFound)
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
describe "loading documents" do
|
|
82
82
|
it "loads all documents from root class" do
|
|
83
83
|
Event.all.should include(@event, @special, @very_special, @super_special, @missing)
|
|
84
84
|
end
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
it "only loads subclass documents from subclass" do
|
|
87
87
|
SpecialEvent.all.should include(@special, @very_special, @super_special)
|
|
88
88
|
SpecialEvent.all.should_not include(@event, @missing)
|
|
89
|
-
|
|
89
|
+
|
|
90
90
|
VerySpecialEvent.all.should include(@very_special, @super_special)
|
|
91
91
|
VerySpecialEvent.all.should_not include(@event, @special, @missing)
|
|
92
|
-
|
|
92
|
+
|
|
93
93
|
SuperSpecialEvent.all.should == [@super_special]
|
|
94
94
|
end
|
|
95
95
|
end
|
|
@@ -5,90 +5,90 @@ module MongoModel
|
|
|
5
5
|
it "is an abstract class" do
|
|
6
6
|
described_class.should be_an_abstract_class
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
it "instantiates with nil" do
|
|
10
10
|
described_class.new(nil).should be_an_instance_of(described_class)
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
describe "subclasses" do
|
|
14
14
|
define_class(:TestDocument, described_class)
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
it "is not an abstract class" do
|
|
17
17
|
TestDocument.should_not be_an_abstract_class
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
specs_for(EmbeddedDocument) do
|
|
23
23
|
define_class(:Event, EmbeddedDocument)
|
|
24
24
|
define_class(:SpecialEvent, :Event)
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
define_class(:Parent, Document) do
|
|
27
27
|
property :event, Event
|
|
28
28
|
property :events, Collection[Event], :default => []
|
|
29
29
|
end
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
let(:event) { Event.new }
|
|
32
32
|
let(:special) { SpecialEvent.new }
|
|
33
33
|
let(:parent) { Parent.new(:event => special) }
|
|
34
34
|
let(:reloaded) { parent.save!; Parent.find(parent.id) }
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
describe "equality" do
|
|
37
37
|
define_class(:DocumentA, EmbeddedDocument) do
|
|
38
38
|
property :name, String
|
|
39
39
|
end
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
define_class(:DocumentB, EmbeddedDocument) do
|
|
42
42
|
property :name, String
|
|
43
43
|
end
|
|
44
|
-
|
|
44
|
+
|
|
45
45
|
subject { DocumentA.new(:id => 'test', :name => 'Test') }
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
it "is equal to another document of the same class with identical attributes" do
|
|
48
48
|
subject.should == DocumentA.new(:id => 'test', :name => 'Test')
|
|
49
49
|
end
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
it "is not equal to another document of the same class with different attributes" do
|
|
52
52
|
subject.should_not == DocumentA.new(:id => 'test', :name => 'Different')
|
|
53
53
|
subject.should_not == DocumentA.new(:id => 'test', :name => 'Test', :special_attribute => 'Different')
|
|
54
54
|
end
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
it "is not equal to another document of a different class with identical attributes" do
|
|
57
57
|
subject.should_not == DocumentB.new(:id => 'test', :name => 'Different')
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
describe "single collection inheritance" do
|
|
62
62
|
it "does not typecast to parent type when assigning to property" do
|
|
63
63
|
parent.event.should be_an_instance_of(SpecialEvent)
|
|
64
64
|
end
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
it "is an instance of the correct class when reloaded" do
|
|
67
67
|
reloaded.event.should be_an_instance_of(SpecialEvent)
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
|
-
|
|
70
|
+
|
|
71
71
|
describe "parent document" do
|
|
72
72
|
context "on an embedded document" do
|
|
73
73
|
it "sets the parent document on the embedded document" do
|
|
74
74
|
parent.event.parent_document.should == parent
|
|
75
75
|
end
|
|
76
|
-
|
|
76
|
+
|
|
77
77
|
it "sets the parent document on the embedded document when loaded from database" do
|
|
78
78
|
reloaded.event.parent_document.should == reloaded
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
context "on a collection" do
|
|
83
83
|
it "sets the parent document on the collection" do
|
|
84
84
|
parent.events.parent_document.should == parent
|
|
85
85
|
end
|
|
86
|
-
|
|
86
|
+
|
|
87
87
|
it "sets the parent document on each item added to a collection" do
|
|
88
88
|
parent.events << special
|
|
89
89
|
parent.events.first.parent_document.should == parent
|
|
90
90
|
end
|
|
91
|
-
|
|
91
|
+
|
|
92
92
|
it "sets the parent document on each item in a collection when loaded from database" do
|
|
93
93
|
parent.events << special
|
|
94
94
|
reloaded.events.first.parent_document.should == parent
|
|
@@ -3,8 +3,9 @@ require 'spec_helper'
|
|
|
3
3
|
describe MongoModel do
|
|
4
4
|
after(:all) do
|
|
5
5
|
MongoModel.configuration = {}
|
|
6
|
+
MongoModel.logger = nil
|
|
6
7
|
end
|
|
7
|
-
|
|
8
|
+
|
|
8
9
|
describe "setting a custom database configuration" do
|
|
9
10
|
before(:each) do
|
|
10
11
|
MongoModel.configuration = {
|
|
@@ -12,13 +13,13 @@ describe MongoModel do
|
|
|
12
13
|
'database' => 'mydb'
|
|
13
14
|
}
|
|
14
15
|
end
|
|
15
|
-
|
|
16
|
+
|
|
16
17
|
it "shoulds merge configuration with defaults" do
|
|
17
18
|
MongoModel.configuration.host.should == '127.0.0.1'
|
|
18
19
|
MongoModel.configuration.port.should == 27017
|
|
19
20
|
MongoModel.configuration.database.should == 'mydb'
|
|
20
21
|
end
|
|
21
|
-
|
|
22
|
+
|
|
22
23
|
it "establishes database connection to given database" do
|
|
23
24
|
database = MongoModel.database
|
|
24
25
|
connection = database.connection
|
|
@@ -28,19 +29,19 @@ describe MongoModel do
|
|
|
28
29
|
database.name.should == 'mydb'
|
|
29
30
|
end
|
|
30
31
|
end
|
|
31
|
-
|
|
32
|
+
|
|
32
33
|
describe "setting a custom database configuration as a URI string" do
|
|
33
34
|
before(:each) do
|
|
34
35
|
MongoModel.configuration = "mongodb://127.0.0.2:27019/mydb"
|
|
35
36
|
end
|
|
36
|
-
|
|
37
|
+
|
|
37
38
|
it "shoulds merge configuration with defaults" do
|
|
38
39
|
MongoModel.configuration.host.should == '127.0.0.2'
|
|
39
40
|
MongoModel.configuration.port.should == 27019
|
|
40
41
|
MongoModel.configuration.database.should == 'mydb'
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
|
-
|
|
44
|
+
|
|
44
45
|
it "has a logger accessor" do
|
|
45
46
|
logger = double('logger').as_null_object
|
|
46
47
|
MongoModel.logger = logger
|
|
@@ -4,19 +4,19 @@ module MongoModel
|
|
|
4
4
|
describe Collection do
|
|
5
5
|
define_class(:TestDocument, EmbeddedDocument)
|
|
6
6
|
let(:doc) { TestDocument.new }
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
subject { Collection }
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
it { should be_a_subclass_of(Array) }
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
it "has type Object" do
|
|
13
13
|
subject.type.should == Object
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
it "does not show its type when inspecting" do
|
|
17
17
|
subject.inspect.should == "Collection"
|
|
18
18
|
end
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
it "allows any object to be added to the collection" do
|
|
21
21
|
collection = subject.new
|
|
22
22
|
collection << 123
|
|
@@ -24,51 +24,51 @@ module MongoModel
|
|
|
24
24
|
collection << doc
|
|
25
25
|
collection.should == [123, "Hello", doc]
|
|
26
26
|
end
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
it "converts to mongo representation" do
|
|
29
29
|
collection = subject.new([123, "Hello", doc])
|
|
30
30
|
collection.to_mongo.should == [123, "Hello", { "_type" => 'TestDocument' }]
|
|
31
31
|
end
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
it "loads from mongo representation" do
|
|
34
34
|
collection = subject.from_mongo([123, "Hello", { "_type" => 'TestDocument' }])
|
|
35
35
|
collection.should be_a(subject)
|
|
36
36
|
collection.should == [123, "Hello", doc]
|
|
37
37
|
end
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
it "caches collection types" do
|
|
40
40
|
Collection[String].should equal(Collection[String])
|
|
41
41
|
Collection[TestDocument].should equal(Collection[TestDocument])
|
|
42
42
|
end
|
|
43
|
-
|
|
43
|
+
|
|
44
44
|
describe "a Collection of Strings" do
|
|
45
45
|
subject { Collection[String] }
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
it { should be_a_subclass_of(Collection) }
|
|
48
48
|
it { should be_a_subclass_of(Array) }
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
it "has type String" do
|
|
51
51
|
subject.type.should == String
|
|
52
52
|
end
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
it "shows its type when inspecting" do
|
|
55
55
|
subject.inspect.should == "Collection[String]"
|
|
56
56
|
end
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
it "converts to mongo representation" do
|
|
59
59
|
collection = subject.new(["a", "bcd", "efg"])
|
|
60
60
|
collection.to_mongo.should == ["a", "bcd", "efg"]
|
|
61
61
|
end
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
it "loads from mongo representation" do
|
|
64
64
|
collection = subject.from_mongo(["a", "bcd", "efg"])
|
|
65
65
|
collection.should be_a(subject)
|
|
66
66
|
collection.should == ["a", "bcd", "efg"]
|
|
67
67
|
end
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
describe "casting" do
|
|
70
70
|
subject { Collection[String].new }
|
|
71
|
-
|
|
71
|
+
|
|
72
72
|
it "casts elements when instantiating" do
|
|
73
73
|
Collection[String].new(["abc", 123, 56.2]).should == ["abc", "123", "56.2"]
|
|
74
74
|
end
|
|
@@ -79,62 +79,62 @@ module MongoModel
|
|
|
79
79
|
subject << 56.2
|
|
80
80
|
subject.should == ["abc", "123", "56.2"]
|
|
81
81
|
end
|
|
82
|
-
|
|
82
|
+
|
|
83
83
|
it "casts elements on []=" do
|
|
84
84
|
subject[0] = "abc"
|
|
85
85
|
subject[1] = 123
|
|
86
86
|
subject[2] = 56.2
|
|
87
87
|
subject.should == ["abc", "123", "56.2"]
|
|
88
88
|
end
|
|
89
|
-
|
|
89
|
+
|
|
90
90
|
it "casts elements on +" do
|
|
91
91
|
result = subject + ["abc", 123, 56.2]
|
|
92
92
|
result.should be_an_instance_of(Collection[String])
|
|
93
93
|
result.should == ["abc", "123", "56.2"]
|
|
94
94
|
end
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
it "casts elements on concat" do
|
|
97
97
|
subject.concat(["abc", 123, 56.2])
|
|
98
98
|
subject.should == ["abc", "123", "56.2"]
|
|
99
99
|
end
|
|
100
|
-
|
|
100
|
+
|
|
101
101
|
it "casts elements on delete" do
|
|
102
102
|
subject.push("abc", 123, 56.2)
|
|
103
103
|
subject.delete(123)
|
|
104
104
|
subject.delete(56.2)
|
|
105
105
|
subject.should == ["abc"]
|
|
106
106
|
end
|
|
107
|
-
|
|
107
|
+
|
|
108
108
|
it "casts elements on index" do
|
|
109
109
|
subject.push("abc", 123, 56.2)
|
|
110
110
|
subject.index(123).should == 1
|
|
111
111
|
subject.index(56.2).should == 2
|
|
112
112
|
end
|
|
113
|
-
|
|
113
|
+
|
|
114
114
|
it "casts elements on insert" do
|
|
115
115
|
subject.insert(0, 56.2)
|
|
116
116
|
subject.insert(0, "abc")
|
|
117
117
|
subject.insert(1, 123)
|
|
118
118
|
subject.should == ["abc", "123", "56.2"]
|
|
119
119
|
end
|
|
120
|
-
|
|
120
|
+
|
|
121
121
|
it "casts elements on push" do
|
|
122
122
|
subject.push("abc", 123, 56.2)
|
|
123
123
|
subject.should == ["abc", "123", "56.2"]
|
|
124
124
|
end
|
|
125
|
-
|
|
125
|
+
|
|
126
126
|
it "casts elements on rindex" do
|
|
127
127
|
subject.push("abc", 123, 56.2, 123)
|
|
128
128
|
subject.rindex(123).should == 3
|
|
129
129
|
subject.rindex(56.2).should == 2
|
|
130
130
|
end
|
|
131
|
-
|
|
131
|
+
|
|
132
132
|
it "casts elements on unshift" do
|
|
133
133
|
subject.unshift("abc")
|
|
134
134
|
subject.unshift(123, 56.2)
|
|
135
135
|
subject.should == ["123", "56.2", "abc"]
|
|
136
136
|
end
|
|
137
|
-
|
|
137
|
+
|
|
138
138
|
it "casts elements on include?" do
|
|
139
139
|
subject.push("abc", 123, 56.2)
|
|
140
140
|
subject.should include(123)
|
|
@@ -143,58 +143,58 @@ module MongoModel
|
|
|
143
143
|
end
|
|
144
144
|
end
|
|
145
145
|
end
|
|
146
|
-
|
|
146
|
+
|
|
147
147
|
describe "a Collection of embedded documents" do
|
|
148
148
|
subject { Collection[TestDocument] }
|
|
149
|
-
|
|
149
|
+
|
|
150
150
|
it { should be_a_subclass_of(Collection) }
|
|
151
151
|
it { should be_a_subclass_of(Array) }
|
|
152
|
-
|
|
152
|
+
|
|
153
153
|
it "has type TestDocument" do
|
|
154
154
|
subject.type.should == TestDocument
|
|
155
155
|
end
|
|
156
|
-
|
|
156
|
+
|
|
157
157
|
it "shows its type when inspecting" do
|
|
158
158
|
subject.inspect.should == "Collection[TestDocument]"
|
|
159
159
|
end
|
|
160
|
-
|
|
160
|
+
|
|
161
161
|
it "converts to mongo representation" do
|
|
162
162
|
collection = subject.new([doc])
|
|
163
163
|
collection.to_mongo.should == [{ "_type" => 'TestDocument' }]
|
|
164
164
|
end
|
|
165
|
-
|
|
165
|
+
|
|
166
166
|
it "loads from mongo representation" do
|
|
167
167
|
collection = subject.from_mongo([{ "_type" => 'TestDocument' }])
|
|
168
168
|
collection.should be_a(subject)
|
|
169
169
|
collection.should == [doc]
|
|
170
170
|
end
|
|
171
171
|
end
|
|
172
|
-
|
|
172
|
+
|
|
173
173
|
describe "a Collection of CustomClasses" do
|
|
174
174
|
subject { Collection[CustomClass] }
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
it { should be_a_subclass_of(Collection) }
|
|
177
177
|
it { should be_a_subclass_of(Array) }
|
|
178
|
-
|
|
178
|
+
|
|
179
179
|
it "has type CustomClass" do
|
|
180
180
|
subject.type.should == CustomClass
|
|
181
181
|
end
|
|
182
|
-
|
|
182
|
+
|
|
183
183
|
it "shows its type when inspecting" do
|
|
184
184
|
subject.inspect.should == "Collection[CustomClass]"
|
|
185
185
|
end
|
|
186
|
-
|
|
186
|
+
|
|
187
187
|
it "converts to mongo representation" do
|
|
188
188
|
collection = subject.new([CustomClass.new("abc"), CustomClass.new("123")])
|
|
189
189
|
collection.to_mongo.should == [{ :name => "abc" }, { :name => "123" }]
|
|
190
190
|
end
|
|
191
|
-
|
|
191
|
+
|
|
192
192
|
it "loads from mongo representation" do
|
|
193
193
|
collection = subject.from_mongo([{ :name => "abc" }, { :name => "123" }])
|
|
194
194
|
collection.should be_a(subject)
|
|
195
195
|
collection.should == [CustomClass.new("abc"), CustomClass.new("123")]
|
|
196
196
|
end
|
|
197
|
-
|
|
197
|
+
|
|
198
198
|
it "loads from mongo representation of single item" do
|
|
199
199
|
collection = subject.from_mongo({ :name => "abc" })
|
|
200
200
|
collection.should be_a(subject)
|
|
@@ -202,70 +202,70 @@ module MongoModel
|
|
|
202
202
|
end
|
|
203
203
|
end
|
|
204
204
|
end
|
|
205
|
-
|
|
205
|
+
|
|
206
206
|
specs_for(Document, EmbeddedDocument) do
|
|
207
207
|
describe "defining a Collection property with default value" do
|
|
208
208
|
define_class(:TestDocument, described_class) do
|
|
209
209
|
property :test_collection, Collection[CustomClass], :default => ['abc', 'def']
|
|
210
210
|
end
|
|
211
|
-
|
|
211
|
+
|
|
212
212
|
subject { TestDocument.new }
|
|
213
|
-
|
|
213
|
+
|
|
214
214
|
it "casts items to collection type" do
|
|
215
215
|
subject.test_collection.should == [CustomClass.new('abc'), CustomClass.new('def')]
|
|
216
216
|
end
|
|
217
|
-
|
|
217
|
+
|
|
218
218
|
it "allows items to be added to collection" do
|
|
219
219
|
subject.test_collection << '123'
|
|
220
220
|
subject.test_collection.should == [CustomClass.new('abc'), CustomClass.new('def'), CustomClass.new('123')]
|
|
221
221
|
end
|
|
222
|
-
|
|
222
|
+
|
|
223
223
|
it "does not share collections between instances" do
|
|
224
224
|
subject.test_collection << '123'
|
|
225
225
|
TestDocument.new.test_collection.should_not == subject.test_collection
|
|
226
226
|
end
|
|
227
227
|
end
|
|
228
|
-
|
|
228
|
+
|
|
229
229
|
describe "with a Collection containing EmbeddedDocuments" do
|
|
230
230
|
define_class(:Embedded, EmbeddedDocument) do
|
|
231
231
|
property :number, Integer
|
|
232
232
|
end
|
|
233
|
-
|
|
233
|
+
|
|
234
234
|
define_class(:TestDocument, described_class) do
|
|
235
235
|
property :embedded, Embedded
|
|
236
236
|
property :embedded_collection, Collection[Embedded]
|
|
237
237
|
end
|
|
238
|
-
|
|
238
|
+
|
|
239
239
|
let(:embedded1) { Embedded.new(:number => 1) }
|
|
240
240
|
let(:embedded2) { Embedded.new(:number => 2) }
|
|
241
241
|
let(:embedded3) { Embedded.new(:number => 3) }
|
|
242
|
-
|
|
242
|
+
|
|
243
243
|
let(:empty) { TestDocument.new }
|
|
244
244
|
subject { TestDocument.new(:embedded => embedded1, :embedded_collection => [embedded2, embedded3]) }
|
|
245
|
-
|
|
245
|
+
|
|
246
246
|
it "defaults to an empty collection" do
|
|
247
247
|
empty.embedded_collection.should be_an_instance_of(Collection[Embedded])
|
|
248
248
|
empty.embedded_collection.should be_empty
|
|
249
249
|
end
|
|
250
|
-
|
|
250
|
+
|
|
251
251
|
it "includes the embedded properties in the embedded documents list" do
|
|
252
252
|
subject.embedded_documents.should include(embedded1)
|
|
253
253
|
end
|
|
254
|
-
|
|
254
|
+
|
|
255
255
|
it "includes the elements in the collection in the embedded documents list" do
|
|
256
256
|
subject.embedded_documents.should include(embedded2, embedded3)
|
|
257
257
|
end
|
|
258
|
-
|
|
258
|
+
|
|
259
259
|
it "casts items to embedded document class when assigning array of hashes" do
|
|
260
260
|
subject.embedded_collection = [ { :number => 5 }, { :number => 99 } ]
|
|
261
261
|
subject.embedded_collection.should == [ Embedded.new(:number => 5), Embedded.new(:number => 99) ]
|
|
262
262
|
end
|
|
263
|
-
|
|
263
|
+
|
|
264
264
|
it "casts items to embedded document class when assigning collection hash" do
|
|
265
265
|
subject.embedded_collection = { :_collection => true, :items => [ { :number => 49 }, { :number => 64 } ] }
|
|
266
266
|
subject.embedded_collection.should == [ Embedded.new(:number => 49), Embedded.new(:number => 64) ]
|
|
267
267
|
end
|
|
268
|
-
|
|
268
|
+
|
|
269
269
|
it "casts item to embedded document class when assigning attributes hash" do
|
|
270
270
|
subject.embedded_collection = { :number => 8 }
|
|
271
271
|
subject.embedded_collection.should == [ Embedded.new(:number => 8) ]
|