mongodoc 0.0.0 → 0.1.0

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.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Hill
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-22 00:00:00 -05:00
12
+ date: 2009-12-07 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -73,16 +73,23 @@ files:
73
73
  - features/saving_an_object.feature
74
74
  - features/step_definitions/collection_steps.rb
75
75
  - features/step_definitions/connect_steps.rb
76
+ - features/step_definitions/criteria_steps.rb
76
77
  - features/step_definitions/document_steps.rb
78
+ - features/step_definitions/documents.rb
77
79
  - features/step_definitions/json_steps.rb
78
80
  - features/step_definitions/object_steps.rb
81
+ - features/step_definitions/objects.rb
79
82
  - features/step_definitions/util_steps.rb
80
83
  - features/support/support.rb
84
+ - features/using_criteria.feature
81
85
  - lib/mongodoc.rb
82
86
  - lib/mongodoc/attributes.rb
83
- - lib/mongodoc/base.rb
84
87
  - lib/mongodoc/bson.rb
88
+ - lib/mongodoc/collection.rb
85
89
  - lib/mongodoc/connection.rb
90
+ - lib/mongodoc/criteria.rb
91
+ - lib/mongodoc/cursor.rb
92
+ - lib/mongodoc/document.rb
86
93
  - lib/mongodoc/ext/array.rb
87
94
  - lib/mongodoc/ext/binary.rb
88
95
  - lib/mongodoc/ext/boolean_class.rb
@@ -101,22 +108,22 @@ files:
101
108
  - lib/mongodoc/parent_proxy.rb
102
109
  - lib/mongodoc/proxy.rb
103
110
  - lib/mongodoc/query.rb
104
- - lib/mongodoc/value_equals.rb
105
111
  - mongod.example.yml
106
112
  - mongodoc.gemspec
107
113
  - script/console
108
114
  - spec/attributes_spec.rb
109
- - spec/base_ext.rb
110
- - spec/base_spec.rb
111
115
  - spec/bson_matchers.rb
112
116
  - spec/bson_spec.rb
117
+ - spec/collection_spec.rb
113
118
  - spec/connection_spec.rb
119
+ - spec/criteria_spec.rb
120
+ - spec/cursor_spec.rb
121
+ - spec/document_ext.rb
122
+ - spec/document_spec.rb
114
123
  - spec/parent_proxy_spec.rb
115
124
  - spec/query_spec.rb
116
125
  - spec/spec.opts
117
126
  - spec/spec_helper.rb
118
- - spec/test_classes.rb
119
- - spec/test_documents.rb
120
127
  has_rdoc: true
121
128
  homepage: http://github.com/leshill/mongodoc
122
129
  licenses: []
@@ -147,13 +154,14 @@ specification_version: 3
147
154
  summary: ODM for MongoDB
148
155
  test_files:
149
156
  - spec/attributes_spec.rb
150
- - spec/base_ext.rb
151
- - spec/base_spec.rb
152
157
  - spec/bson_matchers.rb
153
158
  - spec/bson_spec.rb
159
+ - spec/collection_spec.rb
154
160
  - spec/connection_spec.rb
161
+ - spec/criteria_spec.rb
162
+ - spec/cursor_spec.rb
163
+ - spec/document_ext.rb
164
+ - spec/document_spec.rb
155
165
  - spec/parent_proxy_spec.rb
156
166
  - spec/query_spec.rb
157
167
  - spec/spec_helper.rb
158
- - spec/test_classes.rb
159
- - spec/test_documents.rb
data/lib/mongodoc/base.rb DELETED
@@ -1,163 +0,0 @@
1
- require 'mongodoc/bson'
2
- require 'mongodoc/connection'
3
- require 'mongodoc/value_equals'
4
- require 'mongodoc/query'
5
- require 'mongodoc/attributes'
6
-
7
- module MongoDoc
8
- module Document
9
- class DocumentInvalidError < RuntimeError; end
10
- class NotADocumentError < RuntimeError; end
11
-
12
- def self.included(klass)
13
- klass.instance_eval do
14
- extend MongoDoc::Document::Attributes
15
- extend MongoDoc::Document::BSONCreate
16
- include MongoDoc::Document::ToBSON
17
- include MongoDoc::Document::ValueEquals
18
- include MongoDoc::Document::Identity
19
- include Validatable
20
- end
21
- end
22
-
23
- module ValueEquals
24
- def ==(other)
25
- return false unless self.class === other
26
- self.class._attributes.all? {|var| self.send(var) == other.send(var)}
27
- end
28
- end
29
-
30
- module Identity
31
- attr_accessor :_id
32
- alias :id :_id
33
- alias :to_param :_id
34
-
35
- def new_record?
36
- _id.nil?
37
- end
38
- end
39
-
40
- module ToBSON
41
- def to_bson(*args)
42
- {MongoDoc::BSON::CLASS_KEY => self.class.name}.tap do |bson_hash|
43
- bson_hash['_id'] = _id unless _id.nil?
44
- self.class._attributes.each do |name|
45
- bson_hash[name.to_s] = send(name).to_bson(args)
46
- end
47
- end
48
- end
49
- end
50
-
51
- module BSONCreate
52
- def bson_create(bson_hash, options = {})
53
- new.tap do |obj|
54
- bson_hash.each do |name, value|
55
- obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
56
- end
57
- end
58
- end
59
- end
60
- end
61
-
62
- class Base
63
- include MongoDoc::Document
64
-
65
- def attributes=(attrs)
66
- attrs.each do |key, value|
67
- send("#{key}=", value)
68
- end
69
- end
70
-
71
- def initialize(attrs = {})
72
- self.attributes = attrs
73
- end
74
-
75
- def save(validate = true)
76
- return _root.save(validate) if _root
77
- unless validate and not valid?
78
- _save(false)
79
- else
80
- false
81
- end
82
- end
83
-
84
- def save!
85
- return _root.save! if _root
86
- if valid?
87
- _save(true)
88
- else
89
- raise DocumentInvalidError
90
- end
91
- end
92
-
93
- def update_attributes(attrs)
94
- self.attributes = attrs
95
- return false unless valid?
96
- _propose_update_attributes(self, path_to_root(attrs), false)
97
- end
98
-
99
- def update_attributes!(attrs)
100
- self.attributes = attrs
101
- if valid?
102
- _propose_update_attributes(self, path_to_root(attrs), true)
103
- else
104
- raise DocumentInvalidError
105
- end
106
- end
107
-
108
- def self.collection_name
109
- self.to_s.tableize.gsub('/', '.')
110
- end
111
-
112
- def self.collection
113
- MongoDoc.database.collection(collection_name)
114
- end
115
-
116
- def self.count
117
- collection.count
118
- end
119
-
120
- def self.create(attrs = {}, safe = false)
121
- instance = new(attrs)
122
- instance.valid? and _create(instance, false)
123
- end
124
-
125
- def self.create!(attrs = {})
126
- instance = new(attrs)
127
- if instance.valid?
128
- _create(instance, true)
129
- else
130
- raise DocumentInvalidError
131
- end
132
- end
133
-
134
- def self.find_one(id)
135
- MongoDoc::BSON.decode(collection.find_one(id))
136
- end
137
-
138
- protected
139
-
140
- def _propose_update_attributes(src, attrs, safe)
141
- if _parent
142
- _parent.send(:_propose_update_attributes, src, attrs, safe)
143
- else
144
- _update_attributes(attrs, safe)
145
- end
146
- end
147
-
148
- def _save(safe)
149
- self._id = self.class.collection.save(self.to_bson, :safe => safe)
150
- end
151
-
152
- def _update_attributes(attrs, safe)
153
- self.class.collection.update({'_id' => self._id}, MongoDoc::Query.set_modifier(attrs.to_bson), :safe => safe)
154
- result = MongoDoc.database.db_command({'getlasterror' => 1})
155
- return (result and result.has_key?('updatedExisting') and result['updatedExisting'])
156
- end
157
-
158
- def self._create(instance, safe)
159
- instance._id = collection.insert(instance.to_bson, :safe => safe)
160
- instance
161
- end
162
- end
163
- end
@@ -1,8 +0,0 @@
1
- module MongoDoc
2
- module ValueEquals
3
- def ==(other)
4
- return false unless instance_variables.size == other.instance_variables.size
5
- instance_variables.all? {|var| self.instance_variable_get(var) == other.instance_variable_get(var)}
6
- end
7
- end
8
- end
data/spec/base_spec.rb DELETED
@@ -1,273 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "MongoDoc::Base" do
4
-
5
- context "satisfies form_for requirements" do
6
- before do
7
- @address = Address.new
8
- @address._id = '1'
9
- end
10
-
11
- it "#id returns the _id" do
12
- @address.id.should == @address._id
13
- end
14
-
15
- it "#to_param returns the _id" do
16
- @address.to_param.should == @address._id
17
- end
18
-
19
- context "#new_record?" do
20
- it "is true when the object does not have an _id" do
21
- @address._id = nil
22
- @address.new_record?.should be_true
23
- end
24
-
25
- it "is false when the object has an id" do
26
- @address.new_record?.should be_false
27
- end
28
- end
29
- end
30
-
31
- context "validations" do
32
- class SimpleValidationTest < MongoDoc::Base
33
- key :data
34
- validates_presence_of :data
35
- end
36
-
37
- it "are part of Base" do
38
- Validatable.should === MongoDoc::Base.new
39
- end
40
-
41
- it "valid? fails when a document is invalid" do
42
- doc = SimpleValidationTest.new
43
- doc.should_not be_valid
44
- doc.should have(1).error_on(:data)
45
- end
46
- end
47
-
48
- context "#save" do
49
- before do
50
- @id = Mongo::ObjectID.new([1])
51
- @address = Address.new
52
- @collection = stub('collection')
53
- Address.stub(:collection).and_return(@collection)
54
- end
55
-
56
- it "saves a #to_bson on the collection" do
57
- bson = stub('bson')
58
- @address.should_receive(:to_bson).and_return(bson)
59
- @collection.should_receive(:save).with(bson, anything).and_return(Mongo::ObjectID.new([1]))
60
- @address.save
61
- end
62
-
63
- it "sets the _id of the document" do
64
- @collection.stub(:save).and_return(@id)
65
- @address.save
66
- @address._id.should == @id
67
- end
68
-
69
- it "returns the _id of the document" do
70
- @collection.stub(:save).and_return(@id)
71
- @address.save.should == @id
72
- end
73
-
74
- it "ignores validates if asked to" do
75
- @address.stub(:valid?).and_return(false)
76
- @collection.stub(:save).and_return(@id)
77
- @address.save(false).should == @id
78
- end
79
-
80
- it "returns false if the object is not valid" do
81
- @address.stub(:valid?).and_return(false)
82
- @address.save.should be_false
83
- end
84
- end
85
-
86
- context ".create" do
87
- it "calls insert with the :safe => false option" do
88
- collection = stub('collection')
89
- Address.stub(:collection).and_return(collection)
90
- collection.should_receive(:insert).with(anything, hash_including(:safe => false))
91
- Address.create
92
- end
93
-
94
- it "is false if the object is not valid" do
95
- class CreateValidationTest < MongoDoc::Base
96
- key :data
97
- validates_presence_of :data
98
- end
99
-
100
- CreateValidationTest.create.should be_false
101
- end
102
- end
103
-
104
- context "#bang methods!" do
105
- before do
106
- @address = Address.new
107
- @collection = stub('collection')
108
- Address.stub(:collection).and_return(@collection)
109
- end
110
-
111
- it "create! calls insert with the :safe => true option" do
112
- @collection.should_receive(:insert).with(anything, hash_including(:safe => true))
113
- Address.create!
114
- end
115
-
116
- it "create! raises if not valid" do
117
- class CreateBangValidationTest < MongoDoc::Base
118
- key :data
119
- validates_presence_of :data
120
- end
121
-
122
- expect do
123
- CreateBangValidationTest.create!
124
- end.should raise_error(MongoDoc::Document::DocumentInvalidError)
125
- end
126
-
127
- it "save! call insert with the :safe => true option" do
128
- @collection.should_receive(:save).with(anything, hash_including(:safe => true))
129
- @address.save!
130
- end
131
-
132
- it "save! raises if not valid" do
133
- @address.stub(:valid?).and_return(false)
134
- expect do
135
- @address.save!
136
- end.should raise_error(MongoDoc::Document::DocumentInvalidError)
137
- end
138
- end
139
-
140
- context "#update_attributes" do
141
- before do
142
- @attrs = {:state => 'FL'}
143
- @spec = {'_id' => 1}
144
- @address = Address.new(@spec)
145
- @address.stub(:_update_attributes).and_return(true)
146
- end
147
-
148
- it "returns true on success" do
149
- @address.update_attributes(@attrs).should be_true
150
- end
151
-
152
- it "sets the attributes" do
153
- @address.update_attributes(@attrs)
154
- @address.state.should == 'FL'
155
- end
156
-
157
- it "calls _update_attributes" do
158
- @address.should_receive(:_update_attributes).with(@attrs, false)
159
- @address.update_attributes(@attrs)
160
- end
161
-
162
- it "returns false if the object is not valid" do
163
- @address.stub(:valid?).and_return(false)
164
- @address.update_attributes(@attrs).should be_false
165
- end
166
-
167
- context "with a bang" do
168
- it "with a bang, updates the document with the :safe => true option" do
169
- @address.should_receive(:_update_attributes).with(@attrs, true)
170
- @address.update_attributes!(@attrs)
171
- end
172
-
173
- it "raises if not valid" do
174
- @address.stub(:valid?).and_return(false)
175
- expect do
176
- @address.update_attributes!(@attrs)
177
- end.should raise_error(MongoDoc::Document::DocumentInvalidError)
178
- end
179
- end
180
- end
181
-
182
- context "from a nested document" do
183
- class NestedDocsRoot < MongoDoc::Base
184
- has_many :nested_children
185
- end
186
-
187
- class NestedChild < MongoDoc::Base
188
- has_one :leaf
189
- end
190
-
191
- class LeafDoc < MongoDoc::Base
192
- key :data
193
- end
194
-
195
- context "#save" do
196
- before do
197
- @leaf = LeafDoc.new
198
- @root = NestedDocsRoot.new(:nested_children => [NestedChild.new(:leaf => @leaf)])
199
- end
200
-
201
- it "calls the root document's save" do
202
- @root.should_receive(:save).with(true)
203
- @leaf.save
204
- end
205
-
206
- it "(with bang!) calls the root documents save!" do
207
- @root.should_receive(:save!)
208
- @leaf.save!
209
- end
210
- end
211
-
212
- context "with no has_many, update_attributes" do
213
- before do
214
- @leaf = LeafDoc.new
215
- @root = NestedChild.new(:leaf => @leaf)
216
- end
217
-
218
- it "calls the root document's _update_attributes with a full attribute path and not safe" do
219
- @root.should_receive(:_update_attributes).with({'leaf.data' => 'data'}, false)
220
- @leaf.update_attributes(:data => 'data')
221
- end
222
-
223
- it "(with bang!) calls the root document's _update_attributes with a full attribute path and safe" do
224
- @root.should_receive(:_update_attributes).with({'leaf.data' => 'data'}, true)
225
- @leaf.update_attributes!(:data => 'data')
226
- end
227
- end
228
-
229
- context "with has_many, update_attributes" do
230
- before do
231
- @leaf = LeafDoc.new
232
- NestedDocsRoot.new(:nested_children => [NestedChild.new(:leaf => @leaf)])
233
- end
234
-
235
- it "returns false" do
236
- @leaf.update_attributes(:data => 'data').should be_false
237
- end
238
-
239
- it "sets an error on base" do
240
- @leaf.update_attributes(:data => 'data')
241
- @leaf.errors[:base].should_not be_nil
242
- end
243
-
244
- it "(with bang!) returns false" do
245
- @leaf.update_attributes!(:data => 'data').should be_false
246
- end
247
-
248
- it "(with bang!) sets an error on base" do
249
- @leaf.update_attributes(:data => 'data')
250
- @leaf.errors[:base].should_not be_nil
251
- end
252
- end
253
-
254
- end
255
-
256
- it ".count calls the collection count" do
257
- collection = stub('collection')
258
- MongoDoc::Base.stub(:collection).and_return(collection)
259
- collection.should_receive(:count).and_return(1)
260
- MongoDoc::Base.count
261
- end
262
-
263
- it ".collection_name returns the name of the collection for this class" do
264
- Address.collection_name.should == Address.to_s.tableize.gsub('/', '.')
265
- end
266
-
267
- it ".collection calls through MongoDoc.database using the class name" do
268
- db = stub('db')
269
- db.should_receive(:collection).with(MongoDoc::Base.to_s.tableize.gsub('/', '.'))
270
- MongoDoc.should_receive(:database).and_return(db)
271
- MongoDoc::Base.collection
272
- end
273
- end
data/spec/test_classes.rb DELETED
@@ -1,19 +0,0 @@
1
- require 'mongodoc/value_equals'
2
-
3
- class Movie
4
- include MongoDoc::ValueEquals
5
-
6
- attr_accessor :title, :director, :writers
7
- end
8
-
9
- class Director
10
- include MongoDoc::ValueEquals
11
-
12
- attr_accessor :name, :awards
13
- end
14
-
15
- class AcademyAward
16
- include MongoDoc::ValueEquals
17
-
18
- attr_accessor :year, :category
19
- end
@@ -1,35 +0,0 @@
1
- class Address < MongoDoc::Base
2
- key :street
3
- key :city
4
- key :state
5
- key :zip_code
6
- end
7
-
8
- class Place < MongoDoc::Base
9
- key :name
10
- has_one :address
11
- end
12
-
13
- class Location < MongoDoc::Base
14
- has_one :address
15
- key :website
16
- end
17
-
18
- class WifiAccessible < Location
19
- key :network_name
20
- end
21
-
22
- class WebSite
23
- attr_accessor :url
24
- end
25
-
26
- module Automobile
27
- class Ariel < MongoDoc::Base
28
- key :name
29
- end
30
- end
31
-
32
- class Contact < MongoDoc::Base
33
- key :name
34
- has_many :addresses
35
- end