mongoid 0.12.0 → 1.0.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.
@@ -28,6 +28,14 @@ module Mongoid #:nodoc:
28
28
  def plural?
29
29
  pluralize == self
30
30
  end
31
+
32
+ def reader
33
+ writer? ? gsub("=", "") : self
34
+ end
35
+
36
+ def writer?
37
+ include?("=")
38
+ end
31
39
  end
32
40
  end
33
41
  end
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Fields #:nodoc
4
+ def self.included(base)
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ # Set up the class attributes that must be available to all subclasses.
8
+ # These include defaults, fields
9
+ class_inheritable_accessor :defaults, :fields
10
+
11
+ self.defaults = {}.with_indifferent_access
12
+ self.fields = {}.with_indifferent_access
13
+
14
+ delegate :defaults, :fields, :to => "self.class"
15
+ end
16
+ end
17
+
18
+ module ClassMethods #:nodoc
19
+ # Defines all the fields that are accessable on the Document
20
+ # For each field that is defined, a getter and setter will be
21
+ # added as an instance method to the Document.
22
+ #
23
+ # Options:
24
+ #
25
+ # name: The name of the field, as a +Symbol+.
26
+ # options: A +Hash+ of options to supply to the +Field+.
27
+ #
28
+ # Example:
29
+ #
30
+ # <tt>field :score, :default => 0</tt>
31
+ def field(name, options = {})
32
+ set_field(name, options)
33
+ set_default(name, options)
34
+ end
35
+
36
+ protected
37
+ # Define a field attribute for the +Document+.
38
+ def set_field(name, options = {})
39
+ meth = options.delete(:as) || name
40
+ fields[name] = Field.new(name.to_s, options)
41
+ create_accessors(name, meth, options)
42
+ end
43
+
44
+ # Create the field accessors.
45
+ def create_accessors(name, meth, options = {})
46
+ define_method(meth) { read_attribute(name) }
47
+ define_method("#{meth}=") { |value| write_attribute(name, value) }
48
+ define_method("#{meth}?") { read_attribute(name) == true } if options[:type] == Boolean
49
+ end
50
+
51
+ # Set up a default value for a field.
52
+ def set_default(name, options = {})
53
+ value = options[:default]
54
+ defaults[name] = value if value
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Indexes #:nodoc
4
+ def self.included(base)
5
+ base.class_eval do
6
+ extend ClassMethods
7
+
8
+ cattr_accessor :indexed
9
+ self.indexed = false
10
+ end
11
+ end
12
+
13
+ module ClassMethods #:nodoc
14
+ # Add the default indexes to the root document if they do not already
15
+ # exist. Currently this is only _type.
16
+ def add_indexes
17
+ unless indexed
18
+ self._collection.create_index(:_type, false)
19
+ self.indexed = true
20
+ end
21
+ end
22
+
23
+ # Adds an index on the field specified. Options can be :unique => true or
24
+ # :unique => false. It will default to the latter.
25
+ def index(name, options = { :unique => false })
26
+ collection.create_index(name, options[:unique])
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,21 +5,21 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.12.0"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2010-01-08}
12
+ s.date = %q{2010-01-09}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
- "README.textile"
15
+ "README.rdoc"
16
16
  ]
17
17
  s.files = [
18
18
  ".gitignore",
19
19
  ".watchr",
20
20
  "HISTORY",
21
21
  "MIT_LICENSE",
22
- "README.textile",
22
+ "README.rdoc",
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lib/mongoid.rb",
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "lib/mongoid/associations/options.rb",
34
34
  "lib/mongoid/associations/proxy.rb",
35
35
  "lib/mongoid/attributes.rb",
36
+ "lib/mongoid/callbacks.rb",
36
37
  "lib/mongoid/commands.rb",
37
38
  "lib/mongoid/commands/create.rb",
38
39
  "lib/mongoid/commands/delete.rb",
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  "lib/mongoid/commands/destroy_all.rb",
43
44
  "lib/mongoid/commands/save.rb",
44
45
  "lib/mongoid/complex_criterion.rb",
46
+ "lib/mongoid/components.rb",
45
47
  "lib/mongoid/config.rb",
46
48
  "lib/mongoid/criteria.rb",
47
49
  "lib/mongoid/document.rb",
@@ -66,8 +68,10 @@ Gem::Specification.new do |s|
66
68
  "lib/mongoid/extensions/string/inflections.rb",
67
69
  "lib/mongoid/extensions/symbol/inflections.rb",
68
70
  "lib/mongoid/extensions/time/conversions.rb",
69
- "lib/mongoid/field.rb",
71
+ "lib/mongoid/fields.rb",
72
+ "lib/mongoid/fields/field.rb",
70
73
  "lib/mongoid/finders.rb",
74
+ "lib/mongoid/indexes.rb",
71
75
  "lib/mongoid/memoization.rb",
72
76
  "lib/mongoid/timestamps.rb",
73
77
  "lib/mongoid/versioning.rb",
@@ -90,6 +94,7 @@ Gem::Specification.new do |s|
90
94
  "spec/unit/mongoid/associations/options_spec.rb",
91
95
  "spec/unit/mongoid/associations_spec.rb",
92
96
  "spec/unit/mongoid/attributes_spec.rb",
97
+ "spec/unit/mongoid/callbacks_spec.rb",
93
98
  "spec/unit/mongoid/commands/create_spec.rb",
94
99
  "spec/unit/mongoid/commands/delete_all_spec.rb",
95
100
  "spec/unit/mongoid/commands/delete_spec.rb",
@@ -121,7 +126,9 @@ Gem::Specification.new do |s|
121
126
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
122
127
  "spec/unit/mongoid/extensions/time/conversions_spec.rb",
123
128
  "spec/unit/mongoid/field_spec.rb",
129
+ "spec/unit/mongoid/fields_spec.rb",
124
130
  "spec/unit/mongoid/finders_spec.rb",
131
+ "spec/unit/mongoid/indexes_spec.rb",
125
132
  "spec/unit/mongoid/memoization_spec.rb",
126
133
  "spec/unit/mongoid/timestamps_spec.rb",
127
134
  "spec/unit/mongoid/versioning_spec.rb",
@@ -149,6 +156,7 @@ Gem::Specification.new do |s|
149
156
  "spec/unit/mongoid/associations/options_spec.rb",
150
157
  "spec/unit/mongoid/associations_spec.rb",
151
158
  "spec/unit/mongoid/attributes_spec.rb",
159
+ "spec/unit/mongoid/callbacks_spec.rb",
152
160
  "spec/unit/mongoid/commands/create_spec.rb",
153
161
  "spec/unit/mongoid/commands/delete_all_spec.rb",
154
162
  "spec/unit/mongoid/commands/delete_spec.rb",
@@ -180,7 +188,9 @@ Gem::Specification.new do |s|
180
188
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
181
189
  "spec/unit/mongoid/extensions/time/conversions_spec.rb",
182
190
  "spec/unit/mongoid/field_spec.rb",
191
+ "spec/unit/mongoid/fields_spec.rb",
183
192
  "spec/unit/mongoid/finders_spec.rb",
193
+ "spec/unit/mongoid/indexes_spec.rb",
184
194
  "spec/unit/mongoid/memoization_spec.rb",
185
195
  "spec/unit/mongoid/timestamps_spec.rb",
186
196
  "spec/unit/mongoid/versioning_spec.rb",
@@ -194,14 +204,14 @@ Gem::Specification.new do |s|
194
204
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
195
205
  s.add_runtime_dependency(%q<activesupport>, [">= 2.2.2"])
196
206
  s.add_runtime_dependency(%q<mongo>, [">= 0.18.2"])
197
- s.add_runtime_dependency(%q<durran-validatable>, [">= 1.8.4"])
207
+ s.add_runtime_dependency(%q<durran-validatable>, [">= 2.0.0"])
198
208
  s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
199
209
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
200
210
  s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
201
211
  else
202
212
  s.add_dependency(%q<activesupport>, [">= 2.2.2"])
203
213
  s.add_dependency(%q<mongo>, [">= 0.18.2"])
204
- s.add_dependency(%q<durran-validatable>, [">= 1.8.4"])
214
+ s.add_dependency(%q<durran-validatable>, [">= 2.0.0"])
205
215
  s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
206
216
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
207
217
  s.add_dependency(%q<mocha>, [">= 0.9.8"])
@@ -209,7 +219,7 @@ Gem::Specification.new do |s|
209
219
  else
210
220
  s.add_dependency(%q<activesupport>, [">= 2.2.2"])
211
221
  s.add_dependency(%q<mongo>, [">= 0.18.2"])
212
- s.add_dependency(%q<durran-validatable>, [">= 1.8.4"])
222
+ s.add_dependency(%q<durran-validatable>, [">= 2.0.0"])
213
223
  s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
214
224
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
215
225
  s.add_dependency(%q<mocha>, [">= 0.9.8"])
@@ -6,6 +6,21 @@ describe Mongoid::Document do
6
6
  Mongoid.database.collection(:people).drop
7
7
  end
8
8
 
9
+ context "when document contains a hash field" do
10
+
11
+ before do
12
+ @map = { "first" => 10, "second" => "Blah" }
13
+ @person = Person.create(:map => @map)
14
+ end
15
+
16
+ it "properly gets and sets the has attributes" do
17
+ @person.map.should == @map
18
+ @from_db = Person.find(@person.id)
19
+ @from_db.map.should == @map
20
+ end
21
+
22
+ end
23
+
9
24
  describe ".collection" do
10
25
 
11
26
  context "on a subclass of a root document" do
@@ -89,6 +89,57 @@ describe Mongoid::Attributes do
89
89
 
90
90
  end
91
91
 
92
+ describe "#_id" do
93
+
94
+ before do
95
+ @person = Person.new
96
+ end
97
+
98
+ it "delegates to #id" do
99
+ @person._id.should == @person.id
100
+ end
101
+
102
+ end
103
+
104
+ describe "#_id=" do
105
+
106
+ before do
107
+ @person = Person.new
108
+ end
109
+
110
+ it "delegates to #id=" do
111
+ @id = Mongo::ObjectID.new.to_s
112
+ @person._id = @id
113
+ @person.id.should == @id
114
+ end
115
+
116
+ end
117
+
118
+ describe "#method_missing" do
119
+
120
+ before do
121
+ Mongoid.allow_dynamic_fields = true
122
+ @attributes = {
123
+ :testing => "Testing"
124
+ }
125
+ @person = Person.new(@attributes)
126
+ end
127
+
128
+ context "when an attribute exists" do
129
+
130
+ it "allows the getter" do
131
+ @person.testing.should == "Testing"
132
+ end
133
+
134
+ it "allows the setter" do
135
+ @person.testing = "Test"
136
+ @person.testing.should == "Test"
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+
92
143
  describe "#process" do
93
144
 
94
145
  context "when attributes dont have fields defined" do
@@ -110,7 +161,7 @@ describe Mongoid::Attributes do
110
161
 
111
162
  context "when attribute is a string" do
112
163
 
113
- it "adds a string field" do
164
+ it "adds the string to the attributes" do
114
165
  @person.attributes[:nofieldstring].should == "Testing"
115
166
  end
116
167
 
@@ -118,21 +169,12 @@ describe Mongoid::Attributes do
118
169
 
119
170
  context "when attribute is not a string" do
120
171
 
121
- it "adds a properly typed field" do
172
+ it "adds a cast value to the attributes" do
122
173
  @person.attributes[:nofieldint].should == 5
123
174
  end
124
175
 
125
176
  end
126
177
 
127
- context "when a method has been defined for the attribute" do
128
-
129
- it "does not create the field" do
130
- @person.fields.keys.should_not include("employer")
131
- @person.fields.keys.should_not include("employer=")
132
- end
133
-
134
- end
135
-
136
178
  end
137
179
 
138
180
  context "when not allowing dynamic fields" do
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Callbacks do
4
+
5
+ describe ".included" do
6
+
7
+ before do
8
+ @class = Class.new do
9
+ include Mongoid::Callbacks
10
+ end
11
+ end
12
+
13
+ it "includes the before_create callback" do
14
+ @class.should respond_to(:before_create)
15
+ end
16
+
17
+ it "includes the after_create callback" do
18
+ @class.should respond_to(:after_create)
19
+ end
20
+
21
+ it "includes the before_destroy callback" do
22
+ @class.should respond_to(:before_destroy)
23
+ end
24
+
25
+ it "includes the after_destroy callback" do
26
+ @class.should respond_to(:after_destroy)
27
+ end
28
+
29
+ it "includes the before_save callback" do
30
+ @class.should respond_to(:before_save)
31
+ end
32
+
33
+ it "includes the after_save callback" do
34
+ @class.should respond_to(:after_save)
35
+ end
36
+
37
+ it "includes the before_update callback" do
38
+ @class.should respond_to(:before_update)
39
+ end
40
+
41
+ it "includes the after_update callback" do
42
+ @class.should respond_to(:after_update)
43
+ end
44
+
45
+ it "includes the before_validation callback" do
46
+ @class.should respond_to(:before_validation)
47
+ end
48
+
49
+ it "includes the after_validation callback" do
50
+ @class.should respond_to(:after_validation)
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -215,7 +215,6 @@ describe Mongoid::Commands do
215
215
  end
216
216
 
217
217
  it "runs the validation callbacks" do
218
- @comment.expects(:run_callbacks).with(:validate)
219
218
  @comment.expects(:run_callbacks).with(:before_validation)
220
219
  @comment.expects(:run_callbacks).with(:after_validation)
221
220
  @comment.valid?
@@ -164,42 +164,6 @@ describe Mongoid::Document do
164
164
 
165
165
  end
166
166
 
167
- describe ".defaults" do
168
-
169
- it "returns a hash of all the default values" do
170
- Game.defaults.should == { "high_score" => 500, "score" => 0 }
171
- end
172
-
173
- end
174
-
175
- describe "#defaults" do
176
-
177
- context "on parent classes" do
178
-
179
- before do
180
- @shape = Shape.new
181
- end
182
-
183
- it "does not return subclass defaults" do
184
- @shape.defaults.should == { "x" => 0, "y" => 0 }
185
- end
186
-
187
- end
188
-
189
- context "on subclasses" do
190
-
191
- before do
192
- @circle = Circle.new
193
- end
194
-
195
- it "has the parent and child defaults" do
196
- @circle.defaults.should == { "x" => 0, "y" => 0, "radius" => 0 }
197
- end
198
-
199
- end
200
-
201
- end
202
-
203
167
  describe ".embedded?" do
204
168
 
205
169
  context "when the document is embedded" do
@@ -251,113 +215,6 @@ describe Mongoid::Document do
251
215
 
252
216
  end
253
217
 
254
- describe ".field" do
255
-
256
- context "with no options" do
257
-
258
- before do
259
- Person.field(:testing)
260
- end
261
-
262
- it "adds a reader for the fields defined" do
263
- @person = Person.new(:testing => "Test")
264
- @person.testing.should == "Test"
265
- end
266
-
267
- it "adds a writer for the fields defined" do
268
- @person = Person.new(:testing => "Test")
269
- @person.testing = "Testy"
270
- @person.testing.should == "Testy"
271
- end
272
-
273
- end
274
-
275
- context "when type is an object" do
276
-
277
- before do
278
- @person = Person.new
279
- @drink = MixedDrink.new(:name => "Jack and Coke")
280
- @person.mixed_drink = @drink
281
- end
282
-
283
- it "allows proper access to the object" do
284
- @person.mixed_drink.should == @drink
285
- @person.attributes[:mixed_drink].except(:_id).except(:_type).should ==
286
- { "name" => "Jack and Coke" }
287
- end
288
-
289
- end
290
-
291
- context "when type is a boolean" do
292
-
293
- before do
294
- @person = Person.new(:terms => true)
295
- end
296
-
297
- it "adds an accessor method with a question mark" do
298
- @person.terms?.should be_true
299
- end
300
-
301
- end
302
-
303
- context "when as is specified" do
304
-
305
- before do
306
- Person.field :aliased, :as => :alias, :type => Boolean
307
- @person = Person.new(:alias => true)
308
- end
309
-
310
- it "uses the alias to write the attribute" do
311
- @person.expects(:write_attribute).with(:aliased, true)
312
- @person.alias = true
313
- end
314
-
315
- it "uses the alias to read the attribute" do
316
- @person.expects(:read_attribute).with(:aliased)
317
- @person.alias
318
- end
319
-
320
- it "uses the alias for the query method" do
321
- @person.expects(:read_attribute).with(:aliased)
322
- @person.alias?
323
- end
324
-
325
- end
326
-
327
- end
328
-
329
- describe "#fields" do
330
-
331
- context "on parent classes" do
332
-
333
- before do
334
- @shape = Shape.new
335
- end
336
-
337
- it "does not return subclass fields" do
338
- @shape.fields.keys.should include("x")
339
- @shape.fields.keys.should include("y")
340
- @shape.fields.keys.should_not include("radius")
341
- end
342
-
343
- end
344
-
345
- context "on subclasses" do
346
-
347
- before do
348
- @circle = Circle.new
349
- end
350
-
351
- it "has the parent and child fields" do
352
- @circle.fields.keys.should include("x")
353
- @circle.fields.keys.should include("y")
354
- @circle.fields.keys.should include("radius")
355
- end
356
-
357
- end
358
-
359
- end
360
-
361
218
  describe ".human_name" do
362
219
 
363
220
  it "returns the class name underscored and humanized" do
@@ -366,49 +223,6 @@ describe Mongoid::Document do
366
223
 
367
224
  end
368
225
 
369
- describe "#_id" do
370
-
371
- before do
372
- @person = Person.new
373
- end
374
-
375
- it "delegates to #id" do
376
- @person._id.should == @person.id
377
- end
378
-
379
- end
380
-
381
- describe ".index" do
382
-
383
- context "when unique options are not provided" do
384
-
385
- it "delegates to collection with unique => false" do
386
- @collection.expects(:create_index).with(:title, false)
387
- Person.index :title
388
- end
389
-
390
- end
391
-
392
- context "when unique option is provided" do
393
-
394
- it "delegates to collection with unique option" do
395
- @collection.expects(:create_index).with(:title, true)
396
- Person.index :title, :unique => true
397
- end
398
-
399
- end
400
-
401
- context "when indexing a subclass" do
402
-
403
- it "sets the index on the root class collection" do
404
- @canvas_collection.expects(:create_index).with(:name, true)
405
- Firefox.index :name, :unique => true
406
- end
407
-
408
- end
409
-
410
- end
411
-
412
226
  describe ".instantiate" do
413
227
 
414
228
  before do