mongo_doc 0.6.17 → 0.6.18
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.
- data/Gemfile.lock +1 -1
- data/README.textile +1 -1
- data/VERSION +1 -1
- data/features/db_references.feature +21 -0
- data/features/step_definitions/documents.rb +7 -0
- data/lib/mongo_doc/references.rb +43 -1
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +3 -2
- data/spec/references_spec.rb +53 -6
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/README.textile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.18
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Feature: DBReferences
|
2
|
+
|
3
|
+
Background:
|
4
|
+
Given an empty Character document collection
|
5
|
+
And a Character document named 'Fry' :
|
6
|
+
| Name |
|
7
|
+
| Philip J. Fry |
|
8
|
+
And an empty Address document collection
|
9
|
+
And an Address document named 'office' :
|
10
|
+
| City |
|
11
|
+
| New New York City |
|
12
|
+
And an Address document named 'old_office' :
|
13
|
+
| City |
|
14
|
+
| New York City |
|
15
|
+
|
16
|
+
Scenario: Automatically dereferences in db_references association
|
17
|
+
When I save the document 'office'
|
18
|
+
And 'Fry' references 'office' as 'address'
|
19
|
+
And I save the document 'Fry'
|
20
|
+
And the document 'Fry' is reloaded
|
21
|
+
Then 'Fry' refers to 'office' as 'address'
|
data/lib/mongo_doc/references.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module MongoDoc
|
2
2
|
module References
|
3
|
-
# Declare a reference to another +Document+.
|
3
|
+
# Declare a reference to another +Document+. Use this when you have a
|
4
|
+
# simple reference or a single polymorphic collection.
|
4
5
|
#
|
5
6
|
# * classname:: name of +Document+ type as an +underscore+ symbol or string
|
6
7
|
# * options:: +:as+ specifies the name of the attribute
|
@@ -32,5 +33,46 @@ module MongoDoc
|
|
32
33
|
|
33
34
|
alias_method_chain "#{attr_name}_id=", :reference
|
34
35
|
end
|
36
|
+
|
37
|
+
|
38
|
+
# Declare a +DBRef+ to another +Document+. A +DBRef+ can be in any
|
39
|
+
# collection and can point to an instance of any class.
|
40
|
+
#
|
41
|
+
# * name:: name of the attribute
|
42
|
+
def db_references(name)
|
43
|
+
|
44
|
+
attr_accessor "#{name}_ref".to_sym, :type => ::BSON::DBRef
|
45
|
+
|
46
|
+
module_eval(<<-RUBY, __FILE__, __LINE__)
|
47
|
+
def #{name}_ref_with_reference=(value) # def address_ref_with_reference=(value)
|
48
|
+
@#{name} = nil # @address = nil
|
49
|
+
self.#{name}_ref_without_reference = value # self.address_ref_without_reference = value
|
50
|
+
end # end
|
51
|
+
|
52
|
+
def #{name}
|
53
|
+
@#{name} ||= if #{name}_ref.nil? # @address ||= if address_name_ref.nil?
|
54
|
+
nil # nil
|
55
|
+
else # else
|
56
|
+
self.class.dereference(#{name}_ref) # self.class.dereference(address_name_ref)
|
57
|
+
end # end
|
58
|
+
end
|
59
|
+
|
60
|
+
def #{name}=(value) # def address=(value)
|
61
|
+
@#{name} = value # @address = value
|
62
|
+
self.#{name}_ref = if value.nil? # self.address_ref = if value.nil?
|
63
|
+
nil # nil
|
64
|
+
else # else
|
65
|
+
::BSON::DBRef.new(value.class.collection_name, # ::BSON::DBRef.new(value.collection_name,
|
66
|
+
value._id) # value._id)
|
67
|
+
end # end
|
68
|
+
end # end
|
69
|
+
RUBY
|
70
|
+
|
71
|
+
alias_method_chain "#{name}_ref=", :reference
|
72
|
+
end
|
73
|
+
|
74
|
+
def dereference(db_ref)
|
75
|
+
MongoDoc::Collection.new(db_ref.namespace).find_one(db_ref.object_id)
|
76
|
+
end
|
35
77
|
end
|
36
78
|
end
|
data/lib/mongo_doc.rb
CHANGED
data/mongo_doc.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_doc}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.18"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Les Hill"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-15}
|
13
13
|
s.description = %q{ODM for MongoDB}
|
14
14
|
s.email = %q{leshill@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
"examples/simple_document.rb",
|
33
33
|
"examples/simple_object.rb",
|
34
34
|
"features/collections.feature",
|
35
|
+
"features/db_references.feature",
|
35
36
|
"features/embed_hash.feature",
|
36
37
|
"features/finders.feature",
|
37
38
|
"features/indexes.feature",
|
data/spec/references_spec.rb
CHANGED
@@ -33,6 +33,23 @@ describe MongoDoc::References do
|
|
33
33
|
Person._keys.should include(:address_id)
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
context "setting the id" do
|
38
|
+
class Person
|
39
|
+
include MongoDoc::Document
|
40
|
+
|
41
|
+
references :address
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:address) { Address.new(:_id => BSON::ObjectID.new) }
|
45
|
+
let(:person) { Person.new }
|
46
|
+
|
47
|
+
it "resets the object to nil" do
|
48
|
+
person.address = address
|
49
|
+
person.address_id = nil
|
50
|
+
person.address.should be_nil
|
51
|
+
end
|
52
|
+
end
|
36
53
|
end
|
37
54
|
|
38
55
|
context "Named Reference" do
|
@@ -63,20 +80,50 @@ describe MongoDoc::References do
|
|
63
80
|
end
|
64
81
|
end
|
65
82
|
|
66
|
-
|
83
|
+
context "DBRef Reference" do
|
67
84
|
class Person
|
68
85
|
include MongoDoc::Document
|
69
86
|
|
70
|
-
|
87
|
+
db_references :address
|
71
88
|
end
|
72
89
|
|
73
90
|
let(:address) { Address.new(:_id => BSON::ObjectID.new) }
|
74
91
|
let(:person) { Person.new }
|
92
|
+
subject { person }
|
75
93
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
94
|
+
context "Object accessor" do
|
95
|
+
it { should respond_to(:address) }
|
96
|
+
it { should respond_to(:address=) }
|
97
|
+
|
98
|
+
it "is not part of the persistent key set" do
|
99
|
+
Person._keys.should_not include(:address)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "DBRef accessor" do
|
104
|
+
it { should respond_to(:address_ref) }
|
105
|
+
it { should respond_to(:address_ref=) }
|
106
|
+
|
107
|
+
it "is part of the persistent key set" do
|
108
|
+
Person._keys.should include(:address_ref)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "setting the object" do
|
113
|
+
it "sets the reference" do
|
114
|
+
person.address = address
|
115
|
+
person.address_ref.namespace.should == Address.collection_name
|
116
|
+
person.address_ref.object_id.should == address._id
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "setting the reference" do
|
121
|
+
|
122
|
+
it "resets the object to nil" do
|
123
|
+
person.address = address
|
124
|
+
person.address_ref = nil
|
125
|
+
person.address.should be_nil
|
126
|
+
end
|
80
127
|
end
|
81
128
|
end
|
82
129
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 35
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 18
|
10
|
+
version: 0.6.18
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Les Hill
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-15 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -193,6 +193,7 @@ files:
|
|
193
193
|
- examples/simple_document.rb
|
194
194
|
- examples/simple_object.rb
|
195
195
|
- features/collections.feature
|
196
|
+
- features/db_references.feature
|
196
197
|
- features/embed_hash.feature
|
197
198
|
- features/finders.feature
|
198
199
|
- features/indexes.feature
|