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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongo_doc (0.6.18)
4
+ mongo_doc (0.6.19)
5
5
  activemodel (>= 3.0.0.beta.4)
6
6
  activesupport (>= 3.0.0.beta.4)
7
7
  bson (>= 1.0.0)
data/README.textile CHANGED
@@ -1,6 +1,6 @@
1
1
  h1. MongoDoc
2
2
 
3
- Version: 0.6.18 2010/08/15
3
+ Version: 0.6.19 2010/08/15
4
4
 
5
5
  h2. Notes
6
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.18
1
+ 0.6.19
@@ -57,5 +57,5 @@ class Character
57
57
  include MongoDoc::Document
58
58
 
59
59
  attr_accessor :name
60
- db_references :address
60
+ references :as_ref => :address
61
61
  end
@@ -1,14 +1,44 @@
1
1
  module MongoDoc
2
2
  module References
3
- # Declare a reference to another +Document+. Use this when you have a
4
- # simple reference or a single polymorphic collection.
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
- def references(classname, options = {})
9
- klass = classname.to_s.camelize
10
- attr_name = options[:as] || klass.to_s.demodulize.downcase
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
@@ -3,7 +3,7 @@ require 'active_support'
3
3
  require 'active_support/core_ext'
4
4
 
5
5
  module MongoDoc
6
- VERSION = '0.6.18'
6
+ VERSION = '0.6.19'
7
7
  end
8
8
 
9
9
  require 'mongo_doc/connection'
data/mongo_doc.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_doc}
8
- s.version = "0.6.18"
8
+ s.version = "0.6.19"
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"]
@@ -8,20 +8,20 @@ describe MongoDoc::References do
8
8
  end
9
9
 
10
10
  context "Simple Reference" do
11
- class Person
11
+ class SimplePerson
12
12
  include MongoDoc::Document
13
13
 
14
14
  references :address
15
15
  end
16
16
 
17
- subject { Person.new }
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
- Person._keys.should_not include(:address)
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
- Person._keys.should include(:address_id)
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
- person.address = address
49
- person.address_id = nil
50
- person.address.should be_nil
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 Person
49
+ class NamedPerson
57
50
  include MongoDoc::Document
58
51
 
59
52
  references :address, :as => :work_address
60
53
  end
61
54
 
62
- subject { Person.new }
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
- Person._keys.should_not include(:work_address)
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
- Person._keys.should include(:work_address_id)
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 Person
77
+ class DBRefPerson
85
78
  include MongoDoc::Document
86
79
 
87
- db_references :address
80
+ references :as_ref => :address
88
81
  end
89
82
 
90
83
  let(:address) { Address.new(:_id => BSON::ObjectID.new) }
91
- let(:person) { Person.new }
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
- Person._keys.should_not include(:address)
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
- Person._keys.should include(:address_ref)
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
- person.address = address
115
- person.address_ref.namespace.should == Address.collection_name
116
- person.address_ref.object_id.should == address._id
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
- person.address = address
124
- person.address_ref = nil
125
- person.address.should be_nil
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: 35
4
+ hash: 33
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 18
10
- version: 0.6.18
9
+ - 19
10
+ version: 0.6.19
11
11
  platform: ruby
12
12
  authors:
13
13
  - Les Hill