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.
@@ -1,76 +1,74 @@
1
1
  # Thanks Sandro!
2
2
  # http://github.com/sandro
3
3
  module MongoDoc
4
- module Document
5
- class Proxy
6
- # List of array methods (that are not in +Object+) that need to be
7
- # delegated to +collection+.
8
- ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
4
+ class Proxy
5
+ # List of array methods (that are not in +Object+) that need to be
6
+ # delegated to +collection+.
7
+ ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
9
8
 
10
- # List of additional methods that must be delegated to +collection+.
11
- MUST_DEFINE = %w[to_a to_ary inspect to_bson ==]
9
+ # List of additional methods that must be delegated to +collection+.
10
+ MUST_DEFINE = %w[to_a to_ary inspect to_bson ==]
12
11
 
13
- (ARRAY_METHODS + MUST_DEFINE).uniq.each do |method|
14
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
- def #{method}(*args, &block) # def each(*args, &block)
16
- collection.send(:#{method}, *args, &block) # collection.send(:each, *args, &block)
17
- end # end
18
- RUBY
19
- end
12
+ (ARRAY_METHODS + MUST_DEFINE).uniq.each do |method|
13
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
14
+ def #{method}(*args, &block) # def each(*args, &block)
15
+ collection.send(:#{method}, *args, &block) # collection.send(:each, *args, &block)
16
+ end # end
17
+ RUBY
18
+ end
20
19
 
21
- attr_reader :assoc_name, :collection, :collection_class, :_parent, :_root
20
+ attr_reader :assoc_name, :collection, :collection_class, :_parent, :_root
22
21
 
23
- def _parent=(parent)
24
- @_parent = parent
25
- end
22
+ def _parent=(parent)
23
+ @_parent = parent
24
+ end
26
25
 
27
- def _root=(root)
28
- @_root = root
29
- collection.each do |item|
30
- item._root = root
31
- end
26
+ def _root=(root)
27
+ @_root = root
28
+ collection.each do |item|
29
+ item._root = root
32
30
  end
31
+ end
33
32
 
34
- def initialize(options)
35
- @assoc_name = options[:assoc_name]
36
- @collection = []
37
- @collection_class = options[:collection_class]
38
- @_root = options[:root]
39
- @_parent = options[:parent]
40
- end
33
+ def initialize(options)
34
+ @assoc_name = options[:assoc_name]
35
+ @collection = []
36
+ @collection_class = options[:collection_class]
37
+ @_root = options[:root]
38
+ @_parent = options[:parent]
39
+ end
41
40
 
42
- alias_method :append, :<<
43
- def <<(items)
44
- items = [items] unless items.kind_of?(Array)
45
- items.each do |item|
46
- item = collection_class.new(item) if Hash === item
47
- raise NotADocumentError unless collection_class === item
48
- append item
49
- item._parent = self
50
- item._root = _root
51
- end
52
- self
41
+ alias_method :append, :<<
42
+ def <<(items)
43
+ items = [items] unless items.kind_of?(Array)
44
+ items.each do |item|
45
+ item = collection_class.new(item) if Hash === item
46
+ raise NotADocumentError unless collection_class === item
47
+ append item
48
+ item._parent = self
49
+ item._root = _root
53
50
  end
54
- alias_method :push, :<<
55
- alias_method :concat, :<<
51
+ self
52
+ end
53
+ alias_method :push, :<<
54
+ alias_method :concat, :<<
56
55
 
57
- # Lie about our class. Borrowed from Rake::FileList
58
- # Note: Does not work for case equality (<tt>===</tt>)
59
- def is_a?(klass)
60
- klass == Array || super(klass)
61
- end
62
- alias kind_of? is_a?
56
+ # Lie about our class. Borrowed from Rake::FileList
57
+ # Note: Does not work for case equality (<tt>===</tt>)
58
+ def is_a?(klass)
59
+ klass == Array || super(klass)
60
+ end
61
+ alias kind_of? is_a?
63
62
 
64
- def path_to_root(attrs)
65
- _parent.path_to_root(attrs)
66
- end
63
+ def path_to_root(attrs)
64
+ _parent.path_to_root(attrs)
65
+ end
67
66
 
68
- protected
67
+ protected
69
68
 
70
- def _propose_update_attributes(src, attrs, safe)
71
- src.errors.add(:base, 'update_attributes called through a has_many')
72
- false
73
- end
69
+ def _propose_update_attributes(src, attrs, safe)
70
+ src.errors.add(:base, 'update_attributes called through a has_many')
71
+ false
74
72
  end
75
73
  end
76
74
  end
data/lib/mongodoc.rb CHANGED
@@ -2,10 +2,12 @@ require 'rubygems'
2
2
 
3
3
  gem 'mongo', '0.16'
4
4
  gem 'durran-validatable', '1.8.2'
5
+ gem "leshill-will_paginate", "2.3.11"
5
6
 
6
7
  require 'mongo'
7
8
  require 'activesupport'
8
9
  require 'validatable'
10
+ require 'will_paginate/collection'
9
11
 
10
12
  module MongoDoc
11
13
  VERSION = '0.1'
@@ -14,4 +16,6 @@ module MongoDoc
14
16
  class NoDatabaseError < RuntimeError; end
15
17
  end
16
18
 
17
- require 'mongodoc/base'
19
+ require 'mongodoc/connection'
20
+ require 'mongodoc/collection'
21
+ require 'mongodoc/document'
data/mongodoc.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongodoc}
8
- s.version = "0.0.0"
8
+ s.version = "0.1.0"
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{2009-11-22}
12
+ s.date = %q{2009-12-07}
13
13
  s.description = %q{ODM for MongoDB}
14
14
  s.email = %q{leshill@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,16 +28,23 @@ Gem::Specification.new do |s|
28
28
  "features/saving_an_object.feature",
29
29
  "features/step_definitions/collection_steps.rb",
30
30
  "features/step_definitions/connect_steps.rb",
31
+ "features/step_definitions/criteria_steps.rb",
31
32
  "features/step_definitions/document_steps.rb",
33
+ "features/step_definitions/documents.rb",
32
34
  "features/step_definitions/json_steps.rb",
33
35
  "features/step_definitions/object_steps.rb",
36
+ "features/step_definitions/objects.rb",
34
37
  "features/step_definitions/util_steps.rb",
35
38
  "features/support/support.rb",
39
+ "features/using_criteria.feature",
36
40
  "lib/mongodoc.rb",
37
41
  "lib/mongodoc/attributes.rb",
38
- "lib/mongodoc/base.rb",
39
42
  "lib/mongodoc/bson.rb",
43
+ "lib/mongodoc/collection.rb",
40
44
  "lib/mongodoc/connection.rb",
45
+ "lib/mongodoc/criteria.rb",
46
+ "lib/mongodoc/cursor.rb",
47
+ "lib/mongodoc/document.rb",
41
48
  "lib/mongodoc/ext/array.rb",
42
49
  "lib/mongodoc/ext/binary.rb",
43
50
  "lib/mongodoc/ext/boolean_class.rb",
@@ -56,22 +63,22 @@ Gem::Specification.new do |s|
56
63
  "lib/mongodoc/parent_proxy.rb",
57
64
  "lib/mongodoc/proxy.rb",
58
65
  "lib/mongodoc/query.rb",
59
- "lib/mongodoc/value_equals.rb",
60
66
  "mongod.example.yml",
61
67
  "mongodoc.gemspec",
62
68
  "script/console",
63
69
  "spec/attributes_spec.rb",
64
- "spec/base_ext.rb",
65
- "spec/base_spec.rb",
66
70
  "spec/bson_matchers.rb",
67
71
  "spec/bson_spec.rb",
72
+ "spec/collection_spec.rb",
68
73
  "spec/connection_spec.rb",
74
+ "spec/criteria_spec.rb",
75
+ "spec/cursor_spec.rb",
76
+ "spec/document_ext.rb",
77
+ "spec/document_spec.rb",
69
78
  "spec/parent_proxy_spec.rb",
70
79
  "spec/query_spec.rb",
71
80
  "spec/spec.opts",
72
- "spec/spec_helper.rb",
73
- "spec/test_classes.rb",
74
- "spec/test_documents.rb"
81
+ "spec/spec_helper.rb"
75
82
  ]
76
83
  s.homepage = %q{http://github.com/leshill/mongodoc}
77
84
  s.rdoc_options = ["--charset=UTF-8"]
@@ -80,16 +87,17 @@ Gem::Specification.new do |s|
80
87
  s.summary = %q{ODM for MongoDB}
81
88
  s.test_files = [
82
89
  "spec/attributes_spec.rb",
83
- "spec/base_ext.rb",
84
- "spec/base_spec.rb",
85
90
  "spec/bson_matchers.rb",
86
91
  "spec/bson_spec.rb",
92
+ "spec/collection_spec.rb",
87
93
  "spec/connection_spec.rb",
94
+ "spec/criteria_spec.rb",
95
+ "spec/cursor_spec.rb",
96
+ "spec/document_ext.rb",
97
+ "spec/document_spec.rb",
88
98
  "spec/parent_proxy_spec.rb",
89
99
  "spec/query_spec.rb",
90
- "spec/spec_helper.rb",
91
- "spec/test_classes.rb",
92
- "spec/test_documents.rb"
100
+ "spec/spec_helper.rb"
93
101
  ]
94
102
 
95
103
  if s.respond_to? :specification_version then
@@ -1,8 +1,8 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "MongoDoc::Document::Attributes" do
3
+ describe "MongoDoc::Attributes" do
4
4
  context ".key" do
5
- class TestKeys < MongoDoc::Base
5
+ class TestKeys < MongoDoc::Document
6
6
  end
7
7
 
8
8
  it "adds its arguments to _keys" do
@@ -28,7 +28,7 @@ describe "MongoDoc::Document::Attributes" do
28
28
  end
29
29
 
30
30
  describe "used with inheritance" do
31
- class TestParent < MongoDoc::Base
31
+ class TestParent < MongoDoc::Document
32
32
  key :parent_attr
33
33
  end
34
34
 
@@ -47,18 +47,18 @@ describe "MongoDoc::Document::Attributes" do
47
47
  end
48
48
 
49
49
  context ".has_one" do
50
- class TestDoc < MongoDoc::Base
50
+ class TestDoc < MongoDoc::Document
51
51
  has_one :subdoc
52
52
  end
53
53
 
54
- class SubDoc < MongoDoc::Base
54
+ class SubDoc < MongoDoc::Document
55
55
  key :data
56
56
  end
57
57
 
58
58
  it "sets the subdocuments parent to the parent proxy" do
59
59
  subdoc = SubDoc.new
60
60
  doc = TestDoc.new(:subdoc => subdoc)
61
- MongoDoc::Document::ParentProxy.should === subdoc._parent
61
+ MongoDoc::ParentProxy.should === subdoc._parent
62
62
  subdoc._parent._parent.should == doc
63
63
  end
64
64
 
@@ -77,7 +77,7 @@ describe "MongoDoc::Document::Attributes" do
77
77
  subdoc._root.should == doc
78
78
  end
79
79
 
80
- class HasOneValidationTest < MongoDoc::Base
80
+ class HasOneValidationTest < MongoDoc::Document
81
81
  key :data
82
82
  validates_presence_of :data
83
83
  end
@@ -90,7 +90,7 @@ describe "MongoDoc::Document::Attributes" do
90
90
  end
91
91
 
92
92
  context "._attributes" do
93
- class TestHasOneDoc < MongoDoc::Base
93
+ class TestHasOneDoc < MongoDoc::Document
94
94
  key :key
95
95
  has_one :has_one
96
96
  end
@@ -102,20 +102,20 @@ describe "MongoDoc::Document::Attributes" do
102
102
 
103
103
  context ".has_many" do
104
104
 
105
- class SubHasManyDoc < MongoDoc::Base
105
+ class SubHasManyDoc < MongoDoc::Document
106
106
  key :data
107
107
  end
108
108
 
109
- class TestHasManyDoc < MongoDoc::Base
109
+ class TestHasManyDoc < MongoDoc::Document
110
110
  has_many :sub_docs, :class_name => 'SubHasManyDoc'
111
111
  end
112
112
 
113
- class TestImplicitHasManyDoc < MongoDoc::Base
113
+ class TestImplicitHasManyDoc < MongoDoc::Document
114
114
  has_many :sub_has_many_docs
115
115
  end
116
116
 
117
117
  it "uses a proxy" do
118
- MongoDoc::Document::Proxy.should === TestHasManyDoc.new.sub_docs
118
+ MongoDoc::Proxy.should === TestHasManyDoc.new.sub_docs
119
119
  end
120
120
 
121
121
  it "sets the subdocuments parent to the proxy" do
@@ -135,12 +135,12 @@ describe "MongoDoc::Document::Attributes" do
135
135
  doc = TestImplicitHasManyDoc.new(:sub_has_many_docs => [subdoc])
136
136
  end
137
137
 
138
- class HasManyValidationChild < MongoDoc::Base
138
+ class HasManyValidationChild < MongoDoc::Document
139
139
  key :data
140
140
  validates_presence_of :data
141
141
  end
142
142
 
143
- class HasManyValidationTest < MongoDoc::Base
143
+ class HasManyValidationTest < MongoDoc::Document
144
144
  has_many :subdocs, :class_name => 'HasManyValidationChild'
145
145
  end
146
146
 
data/spec/bson_spec.rb CHANGED
@@ -153,164 +153,44 @@ describe "BSON for Mongo (BSON)" do
153
153
  end
154
154
 
155
155
  describe "Extensions to Object" do
156
+ class Simple
157
+ attr_accessor :value
158
+ end
159
+
160
+ class Complex
161
+ attr_accessor :array_of_simple
162
+ end
163
+
156
164
  before do
157
- @movie = Movie.new
158
- @movie.title = 'Gone with the Wind'
159
- @movie.director = 'Victor Fleming'
160
- @movie.writers = ['Sidney Howard']
161
- @director = Director.new
162
- @director.name = 'Victor Fleming'
163
- @director.awards = ['1940 - Best Director']
164
- @movie.director = @director
165
+ @value1 = 'value1'
166
+ @simple1 = Simple.new
167
+ @simple1.value = @value1
168
+ @value2 = 'value2'
169
+ @simple2 = Simple.new
170
+ @simple2.value = @value2
171
+ @complex = Complex.new
172
+ @complex.array_of_simple = [@simple1, @simple2]
165
173
  end
166
174
 
167
175
  it "renders a json representation of a simple object" do
168
- @director.to_bson.should be_bson_eql({"name" => "Victor Fleming", MongoDoc::BSON::CLASS_KEY => "Director", "awards" => ["1940 - Best Director"]})
176
+ @simple1.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => Simple.name, "value" => @value1})
169
177
  end
170
178
 
171
179
  it "renders a json representation of an object with embedded objects" do
172
- @movie.to_bson.should be_bson_eql({"title" => "Gone with the Wind", MongoDoc::BSON::CLASS_KEY => "Movie", "writers" => ["Sidney Howard"], "director" => {"name" => "Victor Fleming", MongoDoc::BSON::CLASS_KEY => "Director", "awards" => ["1940 - Best Director"]}})
180
+ @complex.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => Complex.name, "array_of_simple" => [@simple1.to_bson, @simple2.to_bson]})
173
181
  end
174
182
 
175
183
  it "ignores a class hash when the :raw_json option is used" do
176
- Movie.bson_create(@movie.to_bson.except(MongoDoc::BSON::CLASS_KEY), :raw_json => true).director.should == @director.to_bson
184
+ Complex.bson_create(@complex.to_bson.except(MongoDoc::BSON::CLASS_KEY), :raw_json => true).array_of_simple.first.should == @simple1.to_bson
177
185
  end
178
186
 
179
187
  it "roundtrips the object" do
180
- MongoDoc::BSON.decode(@movie.to_bson).should be_kind_of(Movie)
181
- end
182
-
183
- it "allows for embedded objects" do
184
- movie_from_bson = MongoDoc::BSON.decode(@movie.to_bson)
185
- movie_from_bson.director.should be_kind_of(Director)
188
+ MongoDoc::BSON.decode(@complex.to_bson).should be_kind_of(Complex)
186
189
  end
187
190
 
188
191
  it "allows for embedded arrays of objects" do
189
- award = AcademyAward.new
190
- award.year = '1940'
191
- award.category = 'Best Director'
192
- @director.awards = [award]
193
- director_from_bson = MongoDoc::BSON.decode(@director.to_bson)
194
- director_from_bson.awards.each {|award| award.should be_kind_of(AcademyAward)}
195
- end
196
- end
197
-
198
- describe "MongoDoc::Base" do
199
- before do
200
- @address = Address.new
201
- @address.street = '320 1st Street North'
202
- @address.city = 'Jacksonville Beach'
203
- @address.state = 'FL'
204
- @address.zip_code = '32250'
205
- @location = Location.new
206
- @location.address = @address
207
- end
208
-
209
- it "encodes the class for the object" do
210
- atom = Automobile::Ariel.new
211
- atom.to_bson[MongoDoc::BSON::CLASS_KEY].should == Automobile::Ariel.name
212
- end
213
-
214
- it "renders a json representation of the object" do
215
- @location.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => "Location", "website" => nil, "address" => {"state" => "FL", MongoDoc::BSON::CLASS_KEY => "Address", "zip_code" => "32250", "street" => "320 1st Street North", "city" => "Jacksonville Beach"}})
216
- end
217
-
218
- it "includes the _id of the object" do
219
- @location._id = Mongo::ObjectID.new
220
- @location.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => "Location", "_id" => @location._id.to_bson, "website" => nil, "address" => {"state" => "FL", MongoDoc::BSON::CLASS_KEY => "Address", "zip_code" => "32250", "street" => "320 1st Street North", "city" => "Jacksonville Beach"}})
221
- end
222
-
223
- it "roundtrips the object" do
224
- MongoDoc::BSON.decode(@location.to_bson).should be_kind_of(Location)
225
- end
226
-
227
- it "ignores the class hash when the :raw_json option is used" do
228
- MongoDoc::BSON.decode(@location.to_bson.except(MongoDoc::BSON::CLASS_KEY), :raw_json => true)['address'].should == @address.to_bson
192
+ obj = MongoDoc::BSON.decode(@complex.to_bson)
193
+ obj.array_of_simple.each {|o| o.should be_kind_of(Simple)}
229
194
  end
230
-
231
- it "allows for embedded MongoDoc objects" do
232
- company_from_bson = MongoDoc::BSON.decode(@location.to_bson)
233
- company_from_bson.should be_kind_of(Location)
234
- company_from_bson.address.should be_kind_of(Address)
235
- end
236
-
237
- it "allows for derived classes" do
238
- wifi = WifiAccessible.new
239
- wifi.address = @address
240
- wifi.network_name = 'hashrocket'
241
- wifi_from_bson = MongoDoc::BSON.decode(wifi.to_bson)
242
- wifi_from_bson.should be_kind_of(WifiAccessible)
243
- wifi_from_bson.address.should be_kind_of(Address)
244
- end
245
-
246
- it "allows for embedded ruby objects" do
247
- website = WebSite.new
248
- website.url = 'http://hashrocket.com'
249
- wifi = WifiAccessible.new
250
- wifi.website = website
251
- wifi_from_bson = MongoDoc::BSON.decode(wifi.to_bson)
252
- wifi_from_bson.should be_kind_of(WifiAccessible)
253
- wifi_from_bson.website.should be_kind_of(WebSite)
254
- end
255
-
256
- context "associations" do
257
- context "has_one" do
258
- class TestHasOneBsonDoc < MongoDoc::Base
259
- has_one :subdoc
260
- end
261
-
262
- class SubHasOneBsonDoc < MongoDoc::Base
263
- key :attr
264
- end
265
-
266
- it "#to_bson renders a bson representation of the document" do
267
- doc = TestHasOneBsonDoc.new
268
- subdoc = SubHasOneBsonDoc.new(:attr => "value")
269
- bson = doc.to_bson
270
- bson["subdoc"] = subdoc.to_bson
271
- doc.subdoc = subdoc
272
- doc.to_bson.should == bson
273
- end
274
-
275
- it "roundtrips" do
276
- doc = TestHasOneBsonDoc.new
277
- subdoc = SubHasOneBsonDoc.new(:attr => "value")
278
- doc.subdoc = subdoc
279
- MongoDoc::BSON.decode(doc.to_bson).should == doc
280
- end
281
- end
282
-
283
- context "has_many" do
284
-
285
- class SubHasManyBsonDoc < MongoDoc::Base
286
- key :attr
287
- end
288
-
289
- class TestHasManyBsonDoc < MongoDoc::Base
290
- has_many :subdoc, :class_name => 'SubHasManyBsonDoc'
291
- end
292
-
293
- it "#to_bson renders a bson representation of the document" do
294
- doc = TestHasManyBsonDoc.new
295
- subdoc = SubHasManyBsonDoc.new(:attr => "value")
296
- bson = doc.to_bson
297
- bson["subdoc"] = [subdoc].to_bson
298
- doc.subdoc = subdoc
299
- doc.to_bson.should == bson
300
- end
301
-
302
- it "roundtrips" do
303
- doc = TestHasManyBsonDoc.new
304
- subdoc = SubHasManyBsonDoc.new(:attr => "value")
305
- doc.subdoc = subdoc
306
- MongoDoc::BSON.decode(doc.to_bson).should == doc
307
- end
308
-
309
- it "roundtrips the proxy" do
310
- doc = TestHasManyBsonDoc.new(:subdoc => SubHasManyBsonDoc.new(:attr => "value"))
311
- MongoDoc::Document::Proxy.should === MongoDoc::BSON.decode(doc.to_bson).subdoc
312
- end
313
- end
314
- end
315
- end
195
+ end
316
196
  end