dm-mongo-adapter 0.2.0.pre3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/Gemfile +27 -0
  2. data/README.rdoc +16 -35
  3. data/Rakefile +6 -16
  4. data/VERSION.yml +2 -2
  5. data/dm-mongo-adapter.gemspec +100 -121
  6. data/lib/mongo_adapter.rb +19 -62
  7. data/lib/mongo_adapter/adapter.rb +49 -36
  8. data/lib/mongo_adapter/conditions.rb +0 -5
  9. data/lib/mongo_adapter/migrations.rb +17 -23
  10. data/lib/mongo_adapter/model.rb +3 -74
  11. data/lib/mongo_adapter/modifier.rb +1 -1
  12. data/lib/mongo_adapter/property/array.rb +10 -0
  13. data/lib/mongo_adapter/property/db_ref.rb +17 -0
  14. data/lib/mongo_adapter/property/hash.rb +27 -0
  15. data/lib/mongo_adapter/property/object_id.rb +47 -0
  16. data/lib/mongo_adapter/query.rb +42 -45
  17. data/lib/mongo_adapter/rails/storage.rb +15 -0
  18. data/lib/mongo_adapter/resource.rb +0 -130
  19. data/lib/mongo_adapter/support/class.rb +11 -0
  20. data/lib/mongo_adapter/support/date.rb +11 -0
  21. data/lib/mongo_adapter/support/date_time.rb +12 -0
  22. data/lib/mongo_adapter/support/object.rb +9 -0
  23. data/spec/legacy/adapter_spec.rb +9 -6
  24. data/spec/legacy/associations_spec.rb +37 -29
  25. data/spec/legacy/property_spec.rb +4 -3
  26. data/spec/legacy/sti_spec.rb +1 -2
  27. data/spec/lib/cleanup_models.rb +0 -1
  28. data/spec/public/aggregates_spec.rb +171 -0
  29. data/spec/public/model_spec.rb +8 -22
  30. data/spec/public/modifier_spec.rb +109 -0
  31. data/spec/public/properties/db_ref_spec.rb +17 -0
  32. data/spec/public/properties/object_id_spec.rb +15 -0
  33. data/spec/public/resource_spec.rb +2 -383
  34. data/spec/public/shared/object_id_shared_spec.rb +16 -39
  35. data/spec/spec_helper.rb +0 -4
  36. metadata +87 -117
  37. data/.gitignore +0 -9
  38. data/lib/mongo_adapter/embedded_model.rb +0 -187
  39. data/lib/mongo_adapter/embedded_resource.rb +0 -134
  40. data/lib/mongo_adapter/embedments/one_to_many.rb +0 -144
  41. data/lib/mongo_adapter/embedments/one_to_one.rb +0 -57
  42. data/lib/mongo_adapter/embedments/relationship.rb +0 -258
  43. data/lib/mongo_adapter/model/embedment.rb +0 -215
  44. data/lib/mongo_adapter/types/date.rb +0 -24
  45. data/lib/mongo_adapter/types/date_time.rb +0 -28
  46. data/lib/mongo_adapter/types/db_ref.rb +0 -65
  47. data/lib/mongo_adapter/types/discriminator.rb +0 -32
  48. data/lib/mongo_adapter/types/object_id.rb +0 -72
  49. data/lib/mongo_adapter/types/objects.rb +0 -31
  50. data/spec/legacy/embedded_resource_spec.rb +0 -157
  51. data/spec/legacy/embedments_spec.rb +0 -177
  52. data/spec/legacy/modifier_spec.rb +0 -81
  53. data/spec/public/embedded_collection_spec.rb +0 -61
  54. data/spec/public/embedded_resource_spec.rb +0 -220
  55. data/spec/public/model/embedment_spec.rb +0 -186
  56. data/spec/public/shared/model_embedments_spec.rb +0 -338
  57. data/spec/public/types/df_ref_spec.rb +0 -6
  58. data/spec/public/types/discriminator_spec.rb +0 -118
  59. data/spec/public/types/embedded_array_spec.rb +0 -55
  60. data/spec/public/types/embedded_hash_spec.rb +0 -83
  61. data/spec/public/types/object_id_spec.rb +0 -6
  62. data/spec/semipublic/embedded_model_spec.rb +0 -43
  63. data/spec/semipublic/model/embedment_spec.rb +0 -42
  64. data/spec/semipublic/resource_spec.rb +0 -70
@@ -1,6 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Types::DBRef do
4
- before(:all) { @type_class = DataMapper::Mongo::Types::DBRef }
5
- it_should_behave_like 'An ObjectID Type'
6
- end
@@ -1,118 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Types::Discriminator do
4
- before :all do
5
- class Article
6
- include DataMapper::Mongo::Resource
7
-
8
- property :id, ObjectID
9
- property :title, String, :required => true
10
- property :type, Discriminator
11
- end
12
-
13
- class Announcement < Article; end
14
- class Release < Announcement; end
15
-
16
- Article.all.destroy!
17
-
18
- @article_model = Article
19
- @announcement_model = Announcement
20
- @release_model = Release
21
- end
22
-
23
- it 'should typecast to a Model' do
24
- @article_model.properties[:type].typecast('Release').should equal(@release_model)
25
- end
26
-
27
- describe 'Model#new' do
28
- describe 'when provided a String discriminator in the attributes' do
29
- before :all do
30
- @resource = @article_model.new(:type => 'Release')
31
- end
32
-
33
- it 'should return a Resource' do
34
- @resource.should be_kind_of(DataMapper::Resource)
35
- end
36
-
37
- it 'should be an descendant instance' do
38
- @resource.should be_instance_of(Release)
39
- end
40
- end
41
-
42
- describe 'when provided a Class discriminator in the attributes' do
43
- before :all do
44
- @resource = @article_model.new(:type => Release)
45
- end
46
-
47
- it 'should return a Resource' do
48
- @resource.should be_kind_of(DataMapper::Resource)
49
- end
50
-
51
- it 'should be an descendant instance' do
52
- @resource.should be_instance_of(Release)
53
- end
54
- end
55
-
56
- describe 'when not provided a discriminator in the attributes' do
57
- before :all do
58
- @resource = @article_model.new
59
- end
60
-
61
- it 'should return a Resource' do
62
- @resource.should be_kind_of(DataMapper::Resource)
63
- end
64
-
65
- it 'should be a base model instance' do
66
- @resource.should be_instance_of(@article_model)
67
- end
68
- end
69
- end
70
-
71
- describe 'Model#descendants' do
72
- it 'should set the descendants for the grandparent model' do
73
- @article_model.descendants.to_a.should == [ @article_model, @announcement_model, @release_model ]
74
- end
75
-
76
- it 'should set the descendants for the parent model' do
77
- @announcement_model.descendants.to_a.should == [ @announcement_model, @release_model ]
78
- end
79
-
80
- it 'should set the descendants for the child model' do
81
- @release_model.descendants.to_a.should == [ @release_model ]
82
- end
83
- end
84
-
85
- describe 'Model#default_scope' do
86
- it 'should set the default scope for the grandparent model' do
87
- @article_model.default_scope[:type].should equal(@article_model.descendants)
88
- end
89
-
90
- it 'should set the default scope for the parent model' do
91
- @announcement_model.default_scope[:type].should equal(@announcement_model.descendants)
92
- end
93
-
94
- it 'should set the default scope for the child model' do
95
- @release_model.default_scope[:type].should equal(@release_model.descendants)
96
- end
97
- end
98
-
99
- before :all do
100
- @announcement = @announcement_model.create(:title => 'Announcement')
101
- end
102
-
103
- it 'should persist the type' do
104
- @announcement.model.get(*@announcement.key).type.should equal(@announcement_model)
105
- end
106
-
107
- it 'should be retrieved as an instance of the correct class' do
108
- @announcement.model.get(*@announcement.key).should be_instance_of(@announcement_model)
109
- end
110
-
111
- it 'should include descendants in finders' do
112
- @article_model.first.should eql(@announcement)
113
- end
114
-
115
- it 'should not include ancestors' do
116
- @release_model.first.should be_nil
117
- end
118
- end
@@ -1,55 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Types::EmbeddedArray do
4
- describe '.load' do
5
- it 'should return nil when given nil' do
6
- DataMapper::Mongo::Types::EmbeddedArray.load(nil, nil).should be_nil
7
- end
8
-
9
- it 'should return the argument when given an Array' do
10
- loaded = DataMapper::Mongo::Types::EmbeddedArray.load([1, 2], nil)
11
- loaded.should == [1, 2]
12
- end
13
-
14
- it 'should raise an error when given anything else' do
15
- pending "EmbeddedArray should not be able to load arbitrary objects" do
16
- [ 0, 1, Object.new, true, false, {} ].each do |value|
17
- lambda {
18
- DataMapper::Mongo::Types::EmbeddedArray.load(value, nil)
19
- }.should raise_error(ArgumentError)
20
- end
21
- end
22
- end
23
- end
24
-
25
- describe '.dump' do
26
- it 'should return nil when given nil' do
27
- DataMapper::Mongo::Types::EmbeddedArray.dump(nil, nil).should == nil
28
- end
29
-
30
- it 'should return the argument when given an Array' do
31
- dumped = DataMapper::Mongo::Types::EmbeddedArray.dump([1, 2], nil)
32
- dumped.should == [1, 2]
33
- end
34
-
35
- it 'should return an Array when given a Set' do
36
- pending do
37
- dumped = DataMapper::Mongo::Types::EmbeddedArray.dump(Set.new(1, 2), nil)
38
- dumped.should be_kind_of(Array)
39
- dumped.should == [1, 2]
40
- end
41
- end
42
-
43
- it 'should raise an error when given anything else' do
44
- pending "EmbeddedArray should not be able to dump objects which " \
45
- "can't later be loaded" do
46
- [ 0, 1, Object.new, true, false, {} ].each do |value|
47
- lambda {
48
- DataMapper::Mongo::Types::EmbeddedArray.dump(value, nil)
49
- }.should raise_error(ArgumentError)
50
- end
51
- end
52
- end
53
- end
54
-
55
- end
@@ -1,83 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Types::EmbeddedHash do
4
- EHash = DataMapper::Mongo::Types::EmbeddedHash
5
-
6
- describe '.load' do
7
- it 'should return nil when given nil' do
8
- EHash.load(nil, nil).should be_nil
9
- end
10
-
11
- it 'should return an empty Hash when given an empty Hash' do
12
- loaded = EHash.load({}, nil)
13
- loaded.should == {}
14
- end
15
-
16
- it 'should return a Hash with values when given a Hash with values' do
17
- loaded = EHash.load({ :hello => 'World' }, nil)
18
- loaded.should == { :hello => 'World' }
19
- end
20
-
21
- it 'should return a Hash with symbolized keys' do
22
- loaded = EHash.load({ 'hello' => 'World' }, nil)
23
- loaded.should == { :hello => 'World' }
24
- end
25
-
26
- it 'should return an empty Hash when given an Array with no values' do
27
- loaded = EHash.load([], nil)
28
- loaded.should == {}
29
- end
30
-
31
- it 'should return an empty Hash with values when given an Array with values' do
32
- loaded = EHash.load([:hello, 'World'], nil)
33
- loaded.should == { :hello => 'World' }
34
- end
35
-
36
- it 'should raise an error when given anything else' do
37
- pending "EmbeddedHash should not be able to load arbitrary objects" do
38
- lambda { EHash.load(0, nil) }.should raise_error(ArgumentError)
39
- lambda { EHash.load(1, nil) }.should raise_error(ArgumentError)
40
- lambda { EHash.load(Object.new, nil) }.should raise_error(ArgumentError)
41
- lambda { EHash.load(true, nil) }.should raise_error(ArgumentError)
42
- lambda { EHash.load(false, nil) }.should raise_error(ArgumentError)
43
- end
44
- end
45
- end
46
-
47
- describe '.dump' do
48
- it 'should return nil when given nil' do
49
- EHash.dump(nil, nil).should be_nil
50
- end
51
-
52
- it 'should return an empty Hash when given an empty Hash' do
53
- EHash.dump({}, nil).should == {}
54
- end
55
-
56
- it 'should return a Hash with values when given a Hash with values' do
57
- EHash.dump({ :hello => 'World' }, nil).should == { :hello => 'World' }
58
- end
59
-
60
- it 'should return an empty Hash when given an empty Mash' do
61
- EHash.dump(Mash.new, nil).should == {}
62
- end
63
-
64
- it 'should return a Hash with values when given a Mash with values' do
65
- dumped = EHash.dump(Mash.new('hello' => 'World'), nil)
66
- dumped.should == { 'hello' => 'World' }
67
- end
68
-
69
- it 'should raise an error when given anything else' do
70
- pending "EmbeddedHash should not be able to dump objects which " \
71
- "can't later be loaded" do
72
- lambda { EHash.dump(0, nil) }.should raise_error(ArgumentError)
73
- lambda { EHash.dump(1, nil) }.should raise_error(ArgumentError)
74
- lambda { EHash.dump(Object.new, nil) }.should raise_error(ArgumentError)
75
- lambda { EHash.dump(true, nil) }.should raise_error(ArgumentError)
76
- lambda { EHash.dump(false, nil) }.should raise_error(ArgumentError)
77
- lambda { EHash.dump([], nil) }.should raise_error(ArgumentError)
78
- end
79
- end
80
-
81
- end
82
-
83
- end
@@ -1,6 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Types::ObjectID do
4
- before(:all) { @type_class = DataMapper::Mongo::Types::ObjectID }
5
- it_should_behave_like 'An ObjectID Type'
6
- end
@@ -1,43 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Resource do
4
-
5
- before(:all) do
6
- class ::CarType
7
- include DataMapper::Mongo::EmbeddedResource
8
- property :name, String
9
- end
10
-
11
- class ::Car
12
- include DataMapper::Mongo::Resource
13
- property :model, String
14
- end
15
-
16
- class ::DMCar
17
- include DataMapper::Resource
18
- property :type, String
19
- end
20
- end
21
-
22
- #
23
- # descendants
24
- #
25
-
26
- describe '#descendants' do
27
- before(:all) do
28
- @descendants = DataMapper::Mongo::EmbeddedModel.descendants
29
- end
30
-
31
- it 'should include Mongo embedment models' do
32
- @descendants.should include(CarType)
33
- end
34
-
35
- it 'should not include Mongo models' do
36
- @descendants.should_not include(Car)
37
- end
38
-
39
- it 'should not include DataMapper models' do
40
- @descendants.should_not include(DMCar)
41
- end
42
- end
43
- end
@@ -1,42 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Model::Embedment do
4
-
5
- before(:all) do
6
- class ::Address
7
- include DataMapper::Mongo::EmbeddedResource
8
- property :street, String
9
- property :city, String, :field => 'conurbation'
10
- end
11
-
12
- class ::User
13
- include DataMapper::Mongo::Resource
14
- property :id, ObjectID
15
- embeds 1, :address
16
- embeds n, :locations, Address
17
- end
18
- end
19
-
20
- describe '#embedments' do
21
- before(:all) do
22
- @embedments = User.embedments
23
- end
24
-
25
- it 'should return a hash' do
26
- @embedments.should be_kind_of(Hash)
27
- end
28
-
29
- it 'should include OneToOne embedments' do
30
- @embedments.should have_key(:address)
31
- @embedments[:address].should \
32
- be_kind_of(DataMapper::Mongo::Embedments::OneToOne::Relationship)
33
- end
34
-
35
- it 'should include OneToMany embedments' do
36
- @embedments.should have_key(:locations)
37
- @embedments[:locations].should \
38
- be_kind_of(DataMapper::Mongo::Embedments::OneToMany::Relationship)
39
- end
40
- end
41
-
42
- end
@@ -1,70 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
-
3
- describe DataMapper::Mongo::Resource do
4
-
5
- before(:all) do
6
- class ::Address
7
- include DataMapper::Mongo::EmbeddedResource
8
- property :city, String
9
- end
10
-
11
- class ::User
12
- include DataMapper::Mongo::Resource
13
- property :id, ObjectID
14
- property :name, String
15
- property :tags, Array
16
- property :metadata, Hash
17
- embeds 1, :address, :model => Address
18
- embeds n, :locations, :model => Address
19
- end
20
- end
21
-
22
- #
23
- # dirty_attributes
24
- #
25
-
26
- describe '#dirty_attributes' do
27
- describe 'when the resource has a change' do
28
- it 'should return true' do
29
- dirty = User.new(:name => 'Mongo').dirty_attributes
30
- dirty.should == { User.properties[:name] => 'Mongo' }
31
- end
32
- end
33
-
34
- describe 'when the resource has no changes' do
35
- it 'should return true if a one-to-one embedment has a change' do
36
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
37
- user.dirty_attributes.should == {
38
- User.embedments[:address] => {
39
- Address.properties[:city] => 'Rock Ridge'
40
- }
41
- }
42
- end
43
-
44
- it 'should return false having just been saved' do
45
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
46
- user.save
47
- user.dirty_attributes.should == {}
48
- user.dirty_attributes.should be_empty
49
- end
50
-
51
- it 'should return true if a one-to-many embedment has a change' do
52
- user = User.new
53
- user.locations << Address.new(:city => 'Rock Ridge')
54
- user.dirty_attributes.should == {
55
- User.embedments[:locations] => [{
56
- Address.properties[:city] => 'Rock Ridge'
57
- }]
58
- }
59
- end
60
-
61
- it 'should return false if no embedments have changes' do
62
- user = User.new(:address => Address.new(:city => 'Rock Ridge'))
63
- user.locations << Address.new(:city => 'Rock Ridge')
64
- user.save
65
- user.dirty_attributes.should be_empty
66
- end
67
- end
68
- end # dirty_attributes
69
-
70
- end