mongo_doc 0.6.18 → 0.6.19
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.textile +1 -1
- data/VERSION +1 -1
- data/features/step_definitions/documents.rb +1 -1
- data/lib/mongo_doc/references.rb +37 -17
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +1 -1
- data/spec/references_spec.rb +22 -30
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.textile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.19
|
data/lib/mongo_doc/references.rb
CHANGED
@@ -1,14 +1,44 @@
|
|
1
1
|
module MongoDoc
|
2
2
|
module References
|
3
|
-
|
4
|
-
#
|
3
|
+
|
4
|
+
# Dereference a DBRef and return the Object
|
5
|
+
def dereference(db_ref)
|
6
|
+
MongoDoc::Collection.new(db_ref.namespace).find_one(db_ref.object_id)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Declare a reference to another +Document+. The reference can either be an
|
10
|
+
# +ObjectID+ reference, or a +BSON::DBRef+
|
11
|
+
#
|
12
|
+
# Use an +ObjectID+ reference when you have a simple reference or will be
|
13
|
+
# referencing a single polymorphic collection. Example:
|
14
|
+
#
|
15
|
+
# +references :address
|
16
|
+
# +references :address, :as => :work_address+
|
5
17
|
#
|
6
18
|
# * classname:: name of +Document+ type as an +underscore+ symbol or string
|
7
|
-
# * options:: +:as+ specifies the name of the attribute
|
8
|
-
|
9
|
-
|
10
|
-
|
19
|
+
# * options:: +:as+ specifies the name of the attribute, defaults to
|
20
|
+
# classname
|
21
|
+
#
|
22
|
+
# Use a +BSON::DBRef+ when you need a reference to multiple collections.
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# +references :as_ref => :work_address+
|
26
|
+
#
|
27
|
+
# * options:: +:as_ref+ name of the attribute
|
28
|
+
def references(*args)
|
29
|
+
options = args.extract_options!
|
11
30
|
|
31
|
+
if options.has_key?(:as_ref)
|
32
|
+
references_by_dbref(options[:as_ref])
|
33
|
+
else
|
34
|
+
klass = args[0].to_s.camelize
|
35
|
+
references_by_id(klass, options[:as] || klass.to_s.demodulize.downcase)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def references_by_id(klass, attr_name)
|
12
42
|
attr_accessor "#{attr_name}_id".to_sym, :type => ::BSON::ObjectID
|
13
43
|
|
14
44
|
module_eval(<<-RUBY, __FILE__, __LINE__)
|
@@ -34,13 +64,7 @@ module MongoDoc
|
|
34
64
|
alias_method_chain "#{attr_name}_id=", :reference
|
35
65
|
end
|
36
66
|
|
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
|
-
|
67
|
+
def references_by_dbref(name)
|
44
68
|
attr_accessor "#{name}_ref".to_sym, :type => ::BSON::DBRef
|
45
69
|
|
46
70
|
module_eval(<<-RUBY, __FILE__, __LINE__)
|
@@ -70,9 +94,5 @@ module MongoDoc
|
|
70
94
|
|
71
95
|
alias_method_chain "#{name}_ref=", :reference
|
72
96
|
end
|
73
|
-
|
74
|
-
def dereference(db_ref)
|
75
|
-
MongoDoc::Collection.new(db_ref.namespace).find_one(db_ref.object_id)
|
76
|
-
end
|
77
97
|
end
|
78
98
|
end
|
data/lib/mongo_doc.rb
CHANGED
data/mongo_doc.gemspec
CHANGED
data/spec/references_spec.rb
CHANGED
@@ -8,20 +8,20 @@ describe MongoDoc::References do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
context "Simple Reference" do
|
11
|
-
class
|
11
|
+
class SimplePerson
|
12
12
|
include MongoDoc::Document
|
13
13
|
|
14
14
|
references :address
|
15
15
|
end
|
16
16
|
|
17
|
-
subject {
|
17
|
+
subject { SimplePerson.new }
|
18
18
|
|
19
19
|
context "Object accessor" do
|
20
20
|
it { should respond_to(:address) }
|
21
21
|
it { should respond_to(:address=) }
|
22
22
|
|
23
23
|
it "is not part of the persistent key set" do
|
24
|
-
|
24
|
+
SimplePerson._keys.should_not include(:address)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -30,43 +30,36 @@ describe MongoDoc::References do
|
|
30
30
|
it { should respond_to(:address_id=) }
|
31
31
|
|
32
32
|
it "is part of the persistent key set" do
|
33
|
-
|
33
|
+
SimplePerson._keys.should include(:address_id)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
context "setting the id" do
|
38
|
-
class Person
|
39
|
-
include MongoDoc::Document
|
40
|
-
|
41
|
-
references :address
|
42
|
-
end
|
43
|
-
|
44
38
|
let(:address) { Address.new(:_id => BSON::ObjectID.new) }
|
45
|
-
let(:person) { Person.new }
|
46
39
|
|
47
40
|
it "resets the object to nil" do
|
48
|
-
|
49
|
-
|
50
|
-
|
41
|
+
subject.address = address
|
42
|
+
subject.address_id = nil
|
43
|
+
subject.address.should be_nil
|
51
44
|
end
|
52
45
|
end
|
53
46
|
end
|
54
47
|
|
55
48
|
context "Named Reference" do
|
56
|
-
class
|
49
|
+
class NamedPerson
|
57
50
|
include MongoDoc::Document
|
58
51
|
|
59
52
|
references :address, :as => :work_address
|
60
53
|
end
|
61
54
|
|
62
|
-
subject {
|
55
|
+
subject { NamedPerson.new }
|
63
56
|
|
64
57
|
context "Object accessor" do
|
65
58
|
it { should respond_to(:work_address) }
|
66
59
|
it { should respond_to(:work_address=) }
|
67
60
|
|
68
61
|
it "is not part of the persistent key set" do
|
69
|
-
|
62
|
+
NamedPerson._keys.should_not include(:work_address)
|
70
63
|
end
|
71
64
|
end
|
72
65
|
|
@@ -75,28 +68,27 @@ describe MongoDoc::References do
|
|
75
68
|
it { should respond_to(:work_address_id=) }
|
76
69
|
|
77
70
|
it "is part of the persistent key set" do
|
78
|
-
|
71
|
+
NamedPerson._keys.should include(:work_address_id)
|
79
72
|
end
|
80
73
|
end
|
81
74
|
end
|
82
75
|
|
83
76
|
context "DBRef Reference" do
|
84
|
-
class
|
77
|
+
class DBRefPerson
|
85
78
|
include MongoDoc::Document
|
86
79
|
|
87
|
-
|
80
|
+
references :as_ref => :address
|
88
81
|
end
|
89
82
|
|
90
83
|
let(:address) { Address.new(:_id => BSON::ObjectID.new) }
|
91
|
-
|
92
|
-
subject { person }
|
84
|
+
subject { DBRefPerson.new }
|
93
85
|
|
94
86
|
context "Object accessor" do
|
95
87
|
it { should respond_to(:address) }
|
96
88
|
it { should respond_to(:address=) }
|
97
89
|
|
98
90
|
it "is not part of the persistent key set" do
|
99
|
-
|
91
|
+
DBRefPerson._keys.should_not include(:address)
|
100
92
|
end
|
101
93
|
end
|
102
94
|
|
@@ -105,24 +97,24 @@ describe MongoDoc::References do
|
|
105
97
|
it { should respond_to(:address_ref=) }
|
106
98
|
|
107
99
|
it "is part of the persistent key set" do
|
108
|
-
|
100
|
+
DBRefPerson._keys.should include(:address_ref)
|
109
101
|
end
|
110
102
|
end
|
111
103
|
|
112
104
|
context "setting the object" do
|
113
105
|
it "sets the reference" do
|
114
|
-
|
115
|
-
|
116
|
-
|
106
|
+
subject.address = address
|
107
|
+
subject.address_ref.namespace.should == Address.collection_name
|
108
|
+
subject.address_ref.object_id.should == address._id
|
117
109
|
end
|
118
110
|
end
|
119
111
|
|
120
112
|
context "setting the reference" do
|
121
113
|
|
122
114
|
it "resets the object to nil" do
|
123
|
-
|
124
|
-
|
125
|
-
|
115
|
+
subject.address = address
|
116
|
+
subject.address_ref = nil
|
117
|
+
subject.address.should be_nil
|
126
118
|
end
|
127
119
|
end
|
128
120
|
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: 33
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 19
|
10
|
+
version: 0.6.19
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Les Hill
|