mongo_doc 0.6.5 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/VERSION +1 -1
- data/features/references.feature +18 -0
- data/features/step_definitions/documents.rb +1 -0
- data/features/step_definitions/field_steps.rb +12 -0
- data/features/step_definitions/reference_steps.rb +0 -0
- data/lib/mongo_doc/document.rb +2 -0
- data/lib/mongo_doc/ext/object_id.rb +4 -0
- data/lib/mongo_doc/references.rb +36 -0
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +7 -2
- data/spec/ext_spec.rb +11 -0
- data/spec/references_spec.rb +82 -0
- metadata +9 -4
data/README.textile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.6
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Feature: References
|
2
|
+
|
3
|
+
Background:
|
4
|
+
Given an empty Person document collection
|
5
|
+
And a Person 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
|
+
|
13
|
+
Scenario: Automatically dereferenced
|
14
|
+
When I save the document 'office'
|
15
|
+
And 'Fry' references 'office' as 'address'
|
16
|
+
And I save the document 'Fry'
|
17
|
+
And the document 'Fry' is reloaded
|
18
|
+
Then 'Fry' refers to 'office' as 'address'
|
@@ -2,3 +2,15 @@ Then /^the field (\w+) of the document '(\w+)' is not nil$/ do |field, name|
|
|
2
2
|
doc = instance_variable_get("@#{name}")
|
3
3
|
doc.send(field).should_not be_nil
|
4
4
|
end
|
5
|
+
|
6
|
+
When /^'(\w+)' references '(\w+)' as '(\w+)'$/ do |parent, child, field|
|
7
|
+
parent_doc = instance_variable_get("@#{parent}")
|
8
|
+
child_doc = instance_variable_get("@#{child}")
|
9
|
+
parent_doc.send("#{field}=", child_doc)
|
10
|
+
end
|
11
|
+
|
12
|
+
Then /^'(\w+)' refers to '(\w+)' as '(\w+)'$/ do |name, other, field|
|
13
|
+
doc = instance_variable_get("@#{name}")
|
14
|
+
other_doc = instance_variable_get("@#{other}")
|
15
|
+
doc.send("#{field}").should == other_doc
|
16
|
+
end
|
File without changes
|
data/lib/mongo_doc/document.rb
CHANGED
@@ -7,6 +7,7 @@ require 'mongo_doc/finders'
|
|
7
7
|
require 'mongo_doc/index'
|
8
8
|
require 'mongo_doc/scope'
|
9
9
|
require 'mongo_doc/timestamps'
|
10
|
+
require 'mongo_doc/references'
|
10
11
|
require 'active_model/naming'
|
11
12
|
require 'active_model/translation'
|
12
13
|
require 'active_model/deprecated_error_methods'
|
@@ -33,6 +34,7 @@ module MongoDoc
|
|
33
34
|
extend Index
|
34
35
|
extend Scope
|
35
36
|
extend Timestamps
|
37
|
+
extend References
|
36
38
|
include ::ActiveModel::Validations
|
37
39
|
extend ::ActiveModel::Naming
|
38
40
|
extend Validations
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MongoDoc
|
2
|
+
module References
|
3
|
+
# Declare a reference to another +Document+.
|
4
|
+
#
|
5
|
+
# * classname:: name of +Document+ type as an +underscore+ symbol or string
|
6
|
+
# * options:: +:as+ specifies the name of the attribute
|
7
|
+
def references(classname, options = {})
|
8
|
+
klass = classname.to_s.camelize
|
9
|
+
attr_name = options[:as] || klass.to_s.demodulize.downcase
|
10
|
+
|
11
|
+
attr_accessor "#{attr_name}_id".to_sym, :type => ::BSON::ObjectID
|
12
|
+
|
13
|
+
module_eval(<<-RUBY, __FILE__, __LINE__)
|
14
|
+
def #{attr_name}_id_with_reference=(value) # def address_id_with_reference=(value)
|
15
|
+
@#{attr_name} = nil # @address = nil
|
16
|
+
self.#{attr_name}_id_without_reference = value # self.address_id_without_reference = value
|
17
|
+
end # end
|
18
|
+
|
19
|
+
def #{attr_name}
|
20
|
+
@#{attr_name} ||= if #{attr_name}_id.nil? # @address ||= if address_name_id.nil?
|
21
|
+
nil # nil
|
22
|
+
else # else
|
23
|
+
#{klass}.find_one(#{attr_name}_id) # Address.find_one(address_name_id)
|
24
|
+
end # end
|
25
|
+
end
|
26
|
+
|
27
|
+
def #{attr_name}=(value) # def address=(value)
|
28
|
+
@#{attr_name} = value # @address = value
|
29
|
+
self.#{attr_name}_id = value.nil? ? nil : value._id # self.address_id = value.nil? ? nil : value._id
|
30
|
+
end # end
|
31
|
+
RUBY
|
32
|
+
|
33
|
+
alias_method_chain "#{attr_name}_id=", :reference
|
34
|
+
end
|
35
|
+
end
|
36
|
+
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.6"
|
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-07-
|
12
|
+
s.date = %q{2010-07-09}
|
13
13
|
s.description = %q{ODM for MongoDB}
|
14
14
|
s.email = %q{leshill@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -40,6 +40,7 @@ Gem::Specification.new do |s|
|
|
40
40
|
"features/mongodoc_base.feature",
|
41
41
|
"features/new_record.feature",
|
42
42
|
"features/partial_updates.feature",
|
43
|
+
"features/references.feature",
|
43
44
|
"features/removing_documents.feature",
|
44
45
|
"features/saving_an_object.feature",
|
45
46
|
"features/scopes.feature",
|
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
|
|
55
56
|
"features/step_definitions/objects.rb",
|
56
57
|
"features/step_definitions/partial_update_steps.rb",
|
57
58
|
"features/step_definitions/query_steps.rb",
|
59
|
+
"features/step_definitions/reference_steps.rb",
|
58
60
|
"features/step_definitions/removing_documents_steps.rb",
|
59
61
|
"features/step_definitions/scope_steps.rb",
|
60
62
|
"features/step_definitions/string_casting_steps.rb",
|
@@ -103,6 +105,7 @@ Gem::Specification.new do |s|
|
|
103
105
|
"lib/mongo_doc/railtie.rb",
|
104
106
|
"lib/mongo_doc/railties/config.rb",
|
105
107
|
"lib/mongo_doc/railties/db_prepare.task",
|
108
|
+
"lib/mongo_doc/references.rb",
|
106
109
|
"lib/mongo_doc/root.rb",
|
107
110
|
"lib/mongo_doc/scope.rb",
|
108
111
|
"lib/mongo_doc/timestamps.rb",
|
@@ -163,6 +166,7 @@ Gem::Specification.new do |s|
|
|
163
166
|
"spec/mongodb.yml",
|
164
167
|
"spec/mongodb_pairs.yml",
|
165
168
|
"spec/new_record_spec.rb",
|
169
|
+
"spec/references_spec.rb",
|
166
170
|
"spec/root_spec.rb",
|
167
171
|
"spec/scope_spec.rb",
|
168
172
|
"spec/spec_helper.rb",
|
@@ -203,6 +207,7 @@ Gem::Specification.new do |s|
|
|
203
207
|
"spec/index_spec.rb",
|
204
208
|
"spec/matchers_spec.rb",
|
205
209
|
"spec/new_record_spec.rb",
|
210
|
+
"spec/references_spec.rb",
|
206
211
|
"spec/root_spec.rb",
|
207
212
|
"spec/scope_spec.rb",
|
208
213
|
"spec/spec_helper.rb",
|
data/spec/ext_spec.rb
CHANGED
@@ -75,6 +75,17 @@ describe "Ruby Object Extensions" do
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
context "ObjectID" do
|
79
|
+
it "returns nil for a blank string" do
|
80
|
+
BSON::ObjectID.cast_from_string('').should be_nil
|
81
|
+
end
|
82
|
+
|
83
|
+
it "converts from a string to an ObjectID" do
|
84
|
+
obj = BSON::ObjectID.new
|
85
|
+
BSON::ObjectID.cast_from_string(obj.to_s).should == obj
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
78
89
|
context "Time" do
|
79
90
|
it "returns nil for a blank string" do
|
80
91
|
Time.cast_from_string('').should be_nil
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoDoc::References do
|
4
|
+
class Address
|
5
|
+
include MongoDoc::Document
|
6
|
+
|
7
|
+
attr_accessor :state
|
8
|
+
end
|
9
|
+
|
10
|
+
context "Simple Reference" do
|
11
|
+
class Person
|
12
|
+
include MongoDoc::Document
|
13
|
+
|
14
|
+
references :address
|
15
|
+
end
|
16
|
+
|
17
|
+
subject { Person.new }
|
18
|
+
|
19
|
+
context "Object accessor" do
|
20
|
+
it { should respond_to(:address) }
|
21
|
+
it { should respond_to(:address=) }
|
22
|
+
|
23
|
+
it "is not part of the persistent key set" do
|
24
|
+
Person._keys.should_not include(:address)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "Object Id accessor" do
|
29
|
+
it { should respond_to(:address_id) }
|
30
|
+
it { should respond_to(:address_id=) }
|
31
|
+
|
32
|
+
it "is part of the persistent key set" do
|
33
|
+
Person._keys.should include(:address_id)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "Named Reference" do
|
39
|
+
class Person
|
40
|
+
include MongoDoc::Document
|
41
|
+
|
42
|
+
references :address, :as => :work_address
|
43
|
+
end
|
44
|
+
|
45
|
+
subject { Person.new }
|
46
|
+
|
47
|
+
context "Object accessor" do
|
48
|
+
it { should respond_to(:work_address) }
|
49
|
+
it { should respond_to(:work_address=) }
|
50
|
+
|
51
|
+
it "is not part of the persistent key set" do
|
52
|
+
Person._keys.should_not include(:work_address)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "Object Id accessor" do
|
57
|
+
it { should respond_to(:work_address_id) }
|
58
|
+
it { should respond_to(:work_address_id=) }
|
59
|
+
|
60
|
+
it "is part of the persistent key set" do
|
61
|
+
Person._keys.should include(:work_address_id)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "setting the id" do
|
67
|
+
class Person
|
68
|
+
include MongoDoc::Document
|
69
|
+
|
70
|
+
references :address
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:address) { Address.new(:_id => BSON::ObjectID.new) }
|
74
|
+
let(:person) { Person.new }
|
75
|
+
|
76
|
+
it "resets the object to nil" do
|
77
|
+
person.address = address
|
78
|
+
person.address_id = nil
|
79
|
+
person.address.should be_nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
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: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 6
|
10
|
+
version: 0.6.6
|
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-07-
|
18
|
+
date: 2010-07-09 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -169,6 +169,7 @@ files:
|
|
169
169
|
- features/mongodoc_base.feature
|
170
170
|
- features/new_record.feature
|
171
171
|
- features/partial_updates.feature
|
172
|
+
- features/references.feature
|
172
173
|
- features/removing_documents.feature
|
173
174
|
- features/saving_an_object.feature
|
174
175
|
- features/scopes.feature
|
@@ -184,6 +185,7 @@ files:
|
|
184
185
|
- features/step_definitions/objects.rb
|
185
186
|
- features/step_definitions/partial_update_steps.rb
|
186
187
|
- features/step_definitions/query_steps.rb
|
188
|
+
- features/step_definitions/reference_steps.rb
|
187
189
|
- features/step_definitions/removing_documents_steps.rb
|
188
190
|
- features/step_definitions/scope_steps.rb
|
189
191
|
- features/step_definitions/string_casting_steps.rb
|
@@ -232,6 +234,7 @@ files:
|
|
232
234
|
- lib/mongo_doc/railtie.rb
|
233
235
|
- lib/mongo_doc/railties/config.rb
|
234
236
|
- lib/mongo_doc/railties/db_prepare.task
|
237
|
+
- lib/mongo_doc/references.rb
|
235
238
|
- lib/mongo_doc/root.rb
|
236
239
|
- lib/mongo_doc/scope.rb
|
237
240
|
- lib/mongo_doc/timestamps.rb
|
@@ -292,6 +295,7 @@ files:
|
|
292
295
|
- spec/mongodb.yml
|
293
296
|
- spec/mongodb_pairs.yml
|
294
297
|
- spec/new_record_spec.rb
|
298
|
+
- spec/references_spec.rb
|
295
299
|
- spec/root_spec.rb
|
296
300
|
- spec/scope_spec.rb
|
297
301
|
- spec/spec_helper.rb
|
@@ -360,6 +364,7 @@ test_files:
|
|
360
364
|
- spec/index_spec.rb
|
361
365
|
- spec/matchers_spec.rb
|
362
366
|
- spec/new_record_spec.rb
|
367
|
+
- spec/references_spec.rb
|
363
368
|
- spec/root_spec.rb
|
364
369
|
- spec/scope_spec.rb
|
365
370
|
- spec/spec_helper.rb
|