mongo_doc 0.6.9 → 0.6.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,6 +1,6 @@
1
1
  h1. MongoDoc
2
2
 
3
- Version: Hurricane (0.6.9) 2010/07/12
3
+ Version: Hurricane (0.6.10) 2010/07/16
4
4
 
5
5
  h2. Notes
6
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.9
1
+ 0.6.10
@@ -9,10 +9,20 @@ Feature: References
9
9
  And an Address document named 'office' :
10
10
  | City |
11
11
  | New New York City |
12
+ And an Address document named 'old_office' :
13
+ | City |
14
+ | New York City |
12
15
 
13
- Scenario: Automatically dereferenced
16
+ Scenario: Automatically dereferences in references association
14
17
  When I save the document 'office'
15
18
  And 'Fry' references 'office' as 'address'
16
19
  And I save the document 'Fry'
17
20
  And the document 'Fry' is reloaded
18
21
  Then 'Fry' refers to 'office' as 'address'
22
+
23
+ Scenario: Automatically dereferences in references_many association
24
+ When I save the document 'old_office'
25
+ And 'Fry' references 'old_office' through 'previous_addresses'
26
+ And I save the document 'Fry'
27
+ And the document 'Fry' is reloaded
28
+ Then 'Fry' has 'previous_addresses' that include 'old_office'
@@ -44,6 +44,7 @@ class Person
44
44
 
45
45
  attr_accessor :name
46
46
  references :address
47
+ references_many :addresses, :as => :previous_addresses
47
48
 
48
49
  timestamps!
49
50
  end
@@ -9,8 +9,20 @@ When /^'(\w+)' references '(\w+)' as '(\w+)'$/ do |parent, child, field|
9
9
  parent_doc.send("#{field}=", child_doc)
10
10
  end
11
11
 
12
+ When /^'(\w+)' references '(\w+)' through '(\w+)'$/ do |parent, child, field|
13
+ parent_doc = instance_variable_get("@#{parent}")
14
+ child_doc = instance_variable_get("@#{child}")
15
+ parent_doc.send("#{field.singularize}_ids") << child_doc._id
16
+ end
17
+
12
18
  Then /^'(\w+)' refers to '(\w+)' as '(\w+)'$/ do |name, other, field|
13
19
  doc = instance_variable_get("@#{name}")
14
20
  other_doc = instance_variable_get("@#{other}")
15
21
  doc.send("#{field}").should == other_doc
16
22
  end
23
+
24
+ Then /^'(\w+)' has '(\w+)' that include '(\w+)'$/ do |name, field, included|
25
+ doc = instance_variable_get("@#{name}")
26
+ included_doc = instance_variable_get("@#{included}")
27
+ doc.send("#{field}").should include(included_doc)
28
+ end
@@ -8,6 +8,7 @@ require 'mongo_doc/index'
8
8
  require 'mongo_doc/scope'
9
9
  require 'mongo_doc/timestamps'
10
10
  require 'mongo_doc/references'
11
+ require 'mongo_doc/references_many'
11
12
  require 'active_model/naming'
12
13
  require 'active_model/translation'
13
14
  require 'active_model/deprecated_error_methods'
@@ -35,6 +36,7 @@ module MongoDoc
35
36
  extend Scope
36
37
  extend Timestamps
37
38
  extend References
39
+ extend ReferencesMany
38
40
  include ::ActiveModel::Validations
39
41
  extend ::ActiveModel::Naming
40
42
  extend Validations
@@ -0,0 +1,46 @@
1
+ module MongoDoc
2
+ module ReferencesMany
3
+ # Declare an array of references to other +Document+s.
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_many(classname, options = {})
8
+ klass = classname.to_s.camelize.singularize
9
+ objects_name = (options[:as] || klass.demodulize.downcase.pluralize).to_s
10
+ ids_name = "#{objects_name.singularize}_ids"
11
+
12
+ _keys << ids_name unless _keys.include?(ids_name)
13
+
14
+ module_eval(<<-RUBY, __FILE__, __LINE__)
15
+ def #{objects_name}=(array) # def addresses=(array)
16
+ @#{objects_name} = array # @addresses = array
17
+ self.#{ids_name} = array.map(&:_id) # self.address_ids = array.map(&:_id)
18
+ end # end
19
+
20
+ def #{objects_name} # def addresses
21
+ @#{objects_name} ||= if #{ids_name}.empty? # @addresses ||= if address_ids.empty?
22
+ [] # []
23
+ else # else
24
+ "#{klass}".constantize. # "Address".constantize.
25
+ find(*#{ids_name}).entries # find(*address_ids).entries
26
+ end # end
27
+ end
28
+
29
+ def #{ids_name}=(array) # def address_ids=(array)
30
+ @#{objects_name} = nil # @addresses = nil
31
+ @#{ids_name} = ReferencesMany.cast_array(array) # @address_ids = ReferencesMany.cast_array(array)
32
+ end # end
33
+
34
+ def #{ids_name} # def address_ids
35
+ @#{ids_name} ||= [] # @address_ids ||= []
36
+ end # end
37
+ RUBY
38
+ end
39
+
40
+ def self.cast_array(array)
41
+ array.nil? ? [] : array.map do |item|
42
+ String === item ? ::BSON::ObjectID.cast_from_string(item) : item
43
+ end
44
+ end
45
+ end
46
+ 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.9'
6
+ VERSION = '0.6.10'
7
7
  end
8
8
 
9
9
  require 'mongo_doc/connection'
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.9"
8
+ s.version = "0.6.10"
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}
12
+ s.date = %q{2010-07-16}
13
13
  s.description = %q{ODM for MongoDB}
14
14
  s.email = %q{leshill@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -54,7 +54,6 @@ Gem::Specification.new do |s|
54
54
  "features/step_definitions/objects.rb",
55
55
  "features/step_definitions/partial_update_steps.rb",
56
56
  "features/step_definitions/query_steps.rb",
57
- "features/step_definitions/reference_steps.rb",
58
57
  "features/step_definitions/removing_documents_steps.rb",
59
58
  "features/step_definitions/scope_steps.rb",
60
59
  "features/step_definitions/string_casting_steps.rb",
@@ -104,6 +103,7 @@ Gem::Specification.new do |s|
104
103
  "lib/mongo_doc/railties/config.rb",
105
104
  "lib/mongo_doc/railties/db_prepare.task",
106
105
  "lib/mongo_doc/references.rb",
106
+ "lib/mongo_doc/references_many.rb",
107
107
  "lib/mongo_doc/root.rb",
108
108
  "lib/mongo_doc/scope.rb",
109
109
  "lib/mongo_doc/timestamps.rb",
@@ -165,6 +165,7 @@ Gem::Specification.new do |s|
165
165
  "spec/mongodb.yml",
166
166
  "spec/mongodb_pairs.yml",
167
167
  "spec/new_record_spec.rb",
168
+ "spec/references_many_spec.rb",
168
169
  "spec/references_spec.rb",
169
170
  "spec/root_spec.rb",
170
171
  "spec/scope_spec.rb",
@@ -207,6 +208,7 @@ Gem::Specification.new do |s|
207
208
  "spec/index_spec.rb",
208
209
  "spec/matchers_spec.rb",
209
210
  "spec/new_record_spec.rb",
211
+ "spec/references_many_spec.rb",
210
212
  "spec/references_spec.rb",
211
213
  "spec/root_spec.rb",
212
214
  "spec/scope_spec.rb",
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoDoc::ReferencesMany 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_many :addresses
15
+ end
16
+
17
+ subject { Person.new }
18
+
19
+ context "Object accessor" do
20
+ it { should respond_to(:addresses) }
21
+ it { should respond_to(:addresses=) }
22
+
23
+ it "is not part of the persistent key set" do
24
+ Person._keys.should_not include('addresses')
25
+ end
26
+ end
27
+
28
+ context "Object Id accessor" do
29
+ it { should respond_to(:address_ids) }
30
+ it { should respond_to(:address_ids=) }
31
+
32
+ it "is part of the persistent key set" do
33
+ Person._keys.should include('address_ids')
34
+ end
35
+ end
36
+ end
37
+
38
+ context "Named Reference" do
39
+ class Person
40
+ include MongoDoc::Document
41
+
42
+ references_many :addresses, :as => :known_addresses
43
+ end
44
+
45
+ subject { Person.new }
46
+
47
+ context "Object accessor" do
48
+ it { should respond_to(:known_addresses) }
49
+ it { should respond_to(:known_addresses=) }
50
+
51
+ it "is not part of the persistent key set" do
52
+ Person._keys.should_not include('known_addresses')
53
+ end
54
+ end
55
+
56
+ context "Object Id accessor" do
57
+ it { should respond_to(:known_address_ids) }
58
+ it { should respond_to(:known_address_ids=) }
59
+
60
+ it "is part of the persistent key set" do
61
+ Person._keys.should include('known_address_ids')
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "setting the ids" do
67
+ class Person
68
+ include MongoDoc::Document
69
+
70
+ references_many :address
71
+ end
72
+
73
+ let(:address) { Address.new(:_id => BSON::ObjectID.new) }
74
+ let(:person) { Person.new }
75
+
76
+ context "to" do
77
+ before do
78
+ person.addresses = [address]
79
+ end
80
+
81
+ context "nil" do
82
+ before do
83
+ person.address_ids = nil
84
+ end
85
+
86
+ it "sets the ids to []" do
87
+ person.address_ids.should == []
88
+ end
89
+
90
+ it "resets the objects to nil" do
91
+ person.addresses.should == []
92
+ end
93
+ end
94
+
95
+ it "[] resets the objects to []" do
96
+ person.address_ids = []
97
+ person.addresses.should == []
98
+ end
99
+ end
100
+
101
+ context "to strings" do
102
+ it "converts the strings to ids" do
103
+ person.address_ids = [address._id.to_s]
104
+ person.address_ids.should == [address._id]
105
+ end
106
+ end
107
+ end
108
+ 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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 9
10
- version: 0.6.9
9
+ - 10
10
+ version: 0.6.10
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-12 00:00:00 -04:00
18
+ date: 2010-07-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -183,7 +183,6 @@ files:
183
183
  - features/step_definitions/objects.rb
184
184
  - features/step_definitions/partial_update_steps.rb
185
185
  - features/step_definitions/query_steps.rb
186
- - features/step_definitions/reference_steps.rb
187
186
  - features/step_definitions/removing_documents_steps.rb
188
187
  - features/step_definitions/scope_steps.rb
189
188
  - features/step_definitions/string_casting_steps.rb
@@ -233,6 +232,7 @@ files:
233
232
  - lib/mongo_doc/railties/config.rb
234
233
  - lib/mongo_doc/railties/db_prepare.task
235
234
  - lib/mongo_doc/references.rb
235
+ - lib/mongo_doc/references_many.rb
236
236
  - lib/mongo_doc/root.rb
237
237
  - lib/mongo_doc/scope.rb
238
238
  - lib/mongo_doc/timestamps.rb
@@ -294,6 +294,7 @@ files:
294
294
  - spec/mongodb.yml
295
295
  - spec/mongodb_pairs.yml
296
296
  - spec/new_record_spec.rb
297
+ - spec/references_many_spec.rb
297
298
  - spec/references_spec.rb
298
299
  - spec/root_spec.rb
299
300
  - spec/scope_spec.rb
@@ -364,6 +365,7 @@ test_files:
364
365
  - spec/index_spec.rb
365
366
  - spec/matchers_spec.rb
366
367
  - spec/new_record_spec.rb
368
+ - spec/references_many_spec.rb
367
369
  - spec/references_spec.rb
368
370
  - spec/root_spec.rb
369
371
  - spec/scope_spec.rb
File without changes