mongoid 1.2.13 → 1.2.14

Sign up to get free protection for your applications and to get access to all the features.
data/.watchr CHANGED
@@ -23,7 +23,7 @@ watch("spec/.*/*_spec\.rb") do |match|
23
23
  spec(match[0])
24
24
  end
25
25
 
26
- watch('lib/(.*/.*)\.rb') do |match|
26
+ watch("lib/(.*/.*)\.rb") do |match|
27
27
  p match[1]
28
28
  spec("spec/unit/#{match[1]}_spec.rb")
29
29
  end
@@ -8,20 +8,13 @@ Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.
8
8
 
9
9
  * {Mongoid on Pivotal Tracker}[http://www.pivotaltracker.com/projects/27482]
10
10
  * {Mongoid Google Group}[http://groups.google.com/group/mongoid]
11
- * {Mongoid on CI Joe}[http://mongoid.org:4444/]
11
+ * {Mongoid on CI Joe}[http://ci.mongoid.org/]
12
12
  * {Mongoid Website and Documentation}[http://mongoid.org]
13
13
 
14
14
  == Compatibility
15
15
 
16
16
  Mongoid is developed against Ruby 1.8.6, 1.8.7, 1.9.1, 1.9.2
17
17
 
18
- Note API changes will be frequent until 1.0, releases will be
19
- frequent, and backwards compatibility will not be supported. Do not
20
- expect the gem to be of full production quality until that
21
- point. However, once 1.0 is released I will be providing backwards
22
- compatibility through each minor version upgrade, as well as detailed
23
- release notes and documentation with each release.
24
-
25
18
  = Documentation
26
19
 
27
20
  Please see the new Mongoid website for up-to-date documentation:
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  gem.authors = ["Durran Jordan"]
14
14
 
15
15
  gem.add_dependency("activesupport", "<= 2.3.5")
16
- gem.add_dependency("mongo", ">= 0.18.3")
16
+ gem.add_dependency("mongo", ">= 0.19.1")
17
17
  gem.add_dependency("durran-validatable", ">= 2.0.1")
18
18
  gem.add_dependency("will_paginate", "< 2.9")
19
19
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.13
1
+ 1.2.14
@@ -22,7 +22,7 @@
22
22
  require "rubygems"
23
23
 
24
24
  gem "activesupport", ">= 2.2.2", "<3.0.pre"
25
- gem "mongo", ">= 0.18.3"
25
+ gem "mongo", ">= 0.19.1"
26
26
  gem "durran-validatable", ">= 2.0.1"
27
27
  gem "will_paginate", ">= 2.3.11", "< 2.9"
28
28
 
@@ -20,11 +20,35 @@ module Mongoid #:nodoc:
20
20
  counts
21
21
  end
22
22
 
23
+ # Get the average value for the supplied field.
24
+ #
25
+ # Example:
26
+ #
27
+ # <tt>context.avg(:age)</tt>
28
+ #
29
+ # Returns:
30
+ #
31
+ # A numeric value that is the average.
32
+ def avg(field)
33
+ total = sum(field)
34
+ total ? (total.to_f / count) : nil
35
+ end
36
+
23
37
  # Gets the number of documents in the array. Delegates to size.
24
38
  def count
25
39
  @count ||= documents.size
26
40
  end
27
41
 
42
+ # Gets an array of distinct values for the supplied field across the
43
+ # entire array or the susbset given the criteria.
44
+ #
45
+ # Example:
46
+ #
47
+ # <tt>context.distinct(:title)</tt>
48
+ def distinct(field)
49
+ execute.collect { |doc| doc.send(field) }.uniq
50
+ end
51
+
28
52
  # Enumerable implementation of execute. Returns matching documents for
29
53
  # the selector, and adds options if supplied.
30
54
  #
@@ -23,6 +23,25 @@ module Mongoid #:nodoc:
23
23
  klass.collection.group(options[:fields], selector, { :count => 0 }, Javascript.aggregate, true)
24
24
  end
25
25
 
26
+ # Get the average value for the supplied field.
27
+ #
28
+ # This will take the internally built selector and options
29
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
30
+ # collection itself will be retrieved from the class provided, and once the
31
+ # query has returned it will provided a grouping of keys with averages.
32
+ #
33
+ # Example:
34
+ #
35
+ # <tt>context.avg(:age)</tt>
36
+ #
37
+ # Returns:
38
+ #
39
+ # A numeric value that is the average.
40
+ def avg(field)
41
+ total = sum(field)
42
+ total ? (total / count) : nil
43
+ end
44
+
26
45
  # Determine if the context is empty or blank given the criteria. Will
27
46
  # perform a quick has_one asking only for the id.
28
47
  #
@@ -48,6 +67,16 @@ module Mongoid #:nodoc:
48
67
  @count ||= klass.collection.find(selector, process_options).count
49
68
  end
50
69
 
70
+ # Gets an array of distinct values for the supplied field across the
71
+ # entire collection or the susbset given the criteria.
72
+ #
73
+ # Example:
74
+ #
75
+ # <tt>context.distinct(:title)</tt>
76
+ def distinct(field)
77
+ klass.collection.distinct(field, selector)
78
+ end
79
+
51
80
  # Execute the context. This will take the selector and options
52
81
  # and pass them on to the Ruby driver's +find()+ method on the collection. The
53
82
  # collection itself will be retrieved from the class provided, and once the
@@ -87,8 +116,7 @@ module Mongoid #:nodoc:
87
116
  options[:fields],
88
117
  selector,
89
118
  { :group => [] },
90
- Javascript.group,
91
- true
119
+ Javascript.group
92
120
  ).collect do |docs|
93
121
  docs["group"] = docs["group"].collect do |attrs|
94
122
  Mongoid::Factory.build(klass, attrs)
@@ -222,8 +250,7 @@ module Mongoid #:nodoc:
222
250
  nil,
223
251
  selector,
224
252
  { start => "start" },
225
- reduce.gsub("[field]", field),
226
- true
253
+ reduce.gsub("[field]", field)
227
254
  )
228
255
  collection.empty? ? nil : collection.first[start.to_s]
229
256
  end
@@ -26,13 +26,14 @@ module Mongoid #:nodoc:
26
26
  include Enumerable
27
27
 
28
28
  attr_reader :collection, :ids, :klass, :options, :selector
29
-
30
29
  attr_accessor :documents
31
30
 
32
31
  delegate \
33
32
  :aggregate,
33
+ :avg,
34
34
  :blank?,
35
35
  :count,
36
+ :distinct,
36
37
  :empty?,
37
38
  :execute,
38
39
  :first,
@@ -23,6 +23,7 @@ require "mongoid/extensions/string/conversions"
23
23
  require "mongoid/extensions/string/inflections"
24
24
  require "mongoid/extensions/symbol/inflections"
25
25
  require "mongoid/extensions/time/conversions"
26
+ require "mongoid/extensions/objectid/conversions"
26
27
 
27
28
  class Array #:nodoc
28
29
  include Mongoid::Extensions::Array::Accessors
@@ -92,3 +93,7 @@ end
92
93
  class Time #:nodoc
93
94
  extend Mongoid::Extensions::Time::Conversions
94
95
  end
96
+
97
+ class Mongo::ObjectID #:nodoc
98
+ extend Mongoid::Extensions::ObjectID::Conversions
99
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Extensions #:nodoc:
4
+ module ObjectID #:nodoc:
5
+ module Conversions #:nodoc:
6
+ def set(value)
7
+ value
8
+ end
9
+ def get(value)
10
+ value
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,7 +4,7 @@ module Mongoid #:nodoc:
4
4
 
5
5
  # Delegate to the criteria methods that are natural for creating a new
6
6
  # criteria.
7
- [ :all_in, :any_in, :excludes, :limit, :max, :min,
7
+ [ :all_in, :any_in, :avg, :excludes, :limit, :max, :min,
8
8
  :not_in, :only, :order_by, :skip, :sum, :where ].each do |name|
9
9
  define_method(name) do |*args|
10
10
  criteria.send(name, *args)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.2.13"
8
+ s.version = "1.2.14"
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-03-12}
12
+ s.date = %q{2010-03-17}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -85,6 +85,7 @@ Gem::Specification.new do |s|
85
85
  "lib/mongoid/extensions/integer/conversions.rb",
86
86
  "lib/mongoid/extensions/nil/assimilation.rb",
87
87
  "lib/mongoid/extensions/object/conversions.rb",
88
+ "lib/mongoid/extensions/objectid/conversions.rb",
88
89
  "lib/mongoid/extensions/proc/scoping.rb",
89
90
  "lib/mongoid/extensions/string/conversions.rb",
90
91
  "lib/mongoid/extensions/string/inflections.rb",
@@ -369,14 +370,14 @@ Gem::Specification.new do |s|
369
370
 
370
371
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
371
372
  s.add_runtime_dependency(%q<activesupport>, ["<= 2.3.5"])
372
- s.add_runtime_dependency(%q<mongo>, [">= 0.18.3"])
373
+ s.add_runtime_dependency(%q<mongo>, [">= 0.19.1"])
373
374
  s.add_runtime_dependency(%q<durran-validatable>, [">= 2.0.1"])
374
375
  s.add_runtime_dependency(%q<will_paginate>, ["< 2.9"])
375
376
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
376
377
  s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
377
378
  else
378
379
  s.add_dependency(%q<activesupport>, ["<= 2.3.5"])
379
- s.add_dependency(%q<mongo>, [">= 0.18.3"])
380
+ s.add_dependency(%q<mongo>, [">= 0.19.1"])
380
381
  s.add_dependency(%q<durran-validatable>, [">= 2.0.1"])
381
382
  s.add_dependency(%q<will_paginate>, ["< 2.9"])
382
383
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
@@ -384,7 +385,7 @@ Gem::Specification.new do |s|
384
385
  end
385
386
  else
386
387
  s.add_dependency(%q<activesupport>, ["<= 2.3.5"])
387
- s.add_dependency(%q<mongo>, [">= 0.18.3"])
388
+ s.add_dependency(%q<mongo>, [">= 0.19.1"])
388
389
  s.add_dependency(%q<durran-validatable>, [">= 2.0.1"])
389
390
  s.add_dependency(%q<will_paginate>, ["< 2.9"])
390
391
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
@@ -10,6 +10,27 @@ describe Mongoid::Criteria do
10
10
  Person.delete_all
11
11
  end
12
12
 
13
+ describe "#avg" do
14
+
15
+ context "without results" do
16
+ it "should return nil" do
17
+ Person.avg(:age).should == nil
18
+ end
19
+ end
20
+
21
+ context "with results" do
22
+ before do
23
+ 10.times do |n|
24
+ Person.create(:title => "Sir", :age => ((n + 1) * 10), :aliases => ["D", "Durran"], :ssn => "#{n}")
25
+ end
26
+ end
27
+
28
+ it "provides min for the field provided" do
29
+ Person.avg(:age).should == 55
30
+ end
31
+ end
32
+ end
33
+
13
34
  describe "#excludes" do
14
35
 
15
36
  before do
@@ -31,6 +31,13 @@ describe Mongoid::Contexts::Enumerable do
31
31
  end
32
32
  end
33
33
 
34
+ describe "#avg" do
35
+
36
+ it "returns the avg value for the supplied field" do
37
+ @context.avg(:number).should == 12.75
38
+ end
39
+ end
40
+
34
41
  describe "#count" do
35
42
 
36
43
  it "returns the size of the enumerable" do
@@ -39,6 +46,30 @@ describe Mongoid::Contexts::Enumerable do
39
46
 
40
47
  end
41
48
 
49
+ describe "#distinct" do
50
+
51
+ context "when the criteria is limited" do
52
+
53
+ it "returns an array of distinct values for the field" do
54
+ @context.distinct(:street).should == [ "Bourke Street" ]
55
+ end
56
+ end
57
+
58
+ context "when the criteria is not limited" do
59
+
60
+ before do
61
+ @criteria = Mongoid::Criteria.new(Address)
62
+ @criteria.documents = @docs
63
+ @context = Mongoid::Contexts::Enumerable.new(@criteria)
64
+ end
65
+
66
+ it "returns an array of distinct values for the field" do
67
+ @context.distinct(:street).should ==
68
+ [ "Bond Street", "Nan Jing Dong Lu", "Bourke Street", "Broadway" ]
69
+ end
70
+ end
71
+ end
72
+
42
73
  describe "#execute" do
43
74
 
44
75
  before do
@@ -22,9 +22,30 @@ describe Mongoid::Contexts::Mongo do
22
22
  @collection.expects(:group).with([:field1], {:_type => {'$in' => ['Doctor', 'Person']}}, {:count => 0}, @reduce, true)
23
23
  @context.aggregate
24
24
  end
25
+ end
26
+ end
25
27
 
28
+ describe "#avg" do
29
+
30
+ before do
31
+ @reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
32
+ @collection = mock
33
+ Person.expects(:collection).twice.returns(@collection)
34
+ @criteria = Mongoid::Criteria.new(Person)
35
+ @context = Mongoid::Contexts::Mongo.new(@criteria)
26
36
  end
27
37
 
38
+ it "calls group on the collection with the aggregate js" do
39
+ @collection.expects(:group).with(
40
+ nil,
41
+ {:_type => {'$in' => ['Doctor', 'Person']}},
42
+ {:sum => "start"},
43
+ @reduce
44
+ ).returns([{"sum" => 100.0}])
45
+ @cursor = mock(:count => 10)
46
+ @collection.expects(:find).returns(@cursor)
47
+ @context.avg(:age).should == 10
48
+ end
28
49
  end
29
50
 
30
51
  describe "blank?" do
@@ -102,6 +123,22 @@ describe Mongoid::Contexts::Mongo do
102
123
 
103
124
  end
104
125
 
126
+ describe "#distinct" do
127
+
128
+ before do
129
+ @criteria = Mongoid::Criteria.new(Person)
130
+ @criteria.where(:test => 'Testing')
131
+ @context = Mongoid::Contexts::Mongo.new(@criteria)
132
+ @collection = mock
133
+ Person.expects(:collection).returns(@collection)
134
+ end
135
+
136
+ it "returns delegates to distinct on the collection" do
137
+ @collection.expects(:distinct).with(:title, @criteria.selector).returns(["Sir"])
138
+ @context.distinct(:title).should == ["Sir"]
139
+ end
140
+ end
141
+
105
142
  describe "#execute" do
106
143
 
107
144
  let(:selector) { { :field => "value" } }
@@ -179,8 +216,7 @@ describe Mongoid::Contexts::Mongo do
179
216
  [:field1],
180
217
  {:_type => { "$in" => ["Doctor", "Person"] }},
181
218
  {:group => []},
182
- @reduce,
183
- true
219
+ @reduce
184
220
  ).returns(@grouping)
185
221
  @context.group
186
222
  end
@@ -323,7 +359,10 @@ describe Mongoid::Contexts::Mongo do
323
359
  @criteria = Mongoid::Criteria.new(Person)
324
360
  @criteria.order_by([[:title, :asc]])
325
361
  @context = Mongoid::Contexts::Mongo.new(@criteria)
326
- @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:title, :desc]] }).returns(
362
+ @collection.expects(:find_one).with(
363
+ {:_type => {'$in' => ['Doctor', 'Person']}},
364
+ { :sort => [[:title, :desc]] }
365
+ ).returns(
327
366
  { "title" => "Sir", "_type" => "Person" }
328
367
  )
329
368
  end
@@ -340,7 +379,10 @@ describe Mongoid::Contexts::Mongo do
340
379
  @criteria = Mongoid::Criteria.new(Person)
341
380
  @criteria.order_by([[:_id, :asc]])
342
381
  @context = Mongoid::Contexts::Mongo.new(@criteria)
343
- @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:_id, :desc]] }).returns(nil)
382
+ @collection.expects(:find_one).with(
383
+ {:_type => {'$in' => ['Doctor', 'Person']}},
384
+ { :sort => [[:_id, :desc]] }
385
+ ).returns(nil)
344
386
  end
345
387
 
346
388
  it "returns nil" do
@@ -355,7 +397,10 @@ describe Mongoid::Contexts::Mongo do
355
397
  @criteria = Mongoid::Criteria.new(Person)
356
398
  @criteria.order_by([[:_id, :asc]])
357
399
  @context = Mongoid::Contexts::Mongo.new(@criteria)
358
- @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:_id, :desc]] }).returns(
400
+ @collection.expects(:find_one).with(
401
+ {:_type => {'$in' => ['Doctor', 'Person']}},
402
+ { :sort => [[:_id, :desc]] }
403
+ ).returns(
359
404
  { "title" => "Sir", "_type" => "Person" }
360
405
  )
361
406
  end
@@ -383,8 +428,7 @@ describe Mongoid::Contexts::Mongo do
383
428
  nil,
384
429
  {:_type => {'$in' => ['Doctor', 'Person']}},
385
430
  {:max => "start"},
386
- @reduce,
387
- true
431
+ @reduce
388
432
  ).returns([{"max" => 200.0}])
389
433
  @context.max(:age).should == 200.0
390
434
  end
@@ -406,8 +450,7 @@ describe Mongoid::Contexts::Mongo do
406
450
  nil,
407
451
  {:_type => {'$in' => ['Doctor', 'Person']}},
408
452
  {:min => "start"},
409
- @reduce,
410
- true
453
+ @reduce
411
454
  ).returns([{"min" => 4.0}])
412
455
  @context.min(:age).should == 4.0
413
456
  end
@@ -518,8 +561,7 @@ describe Mongoid::Contexts::Mongo do
518
561
  nil,
519
562
  {:_type => {'$in' => ['Doctor', 'Person']}},
520
563
  {:sum => "start"},
521
- @reduce,
522
- true
564
+ @reduce
523
565
  ).returns([{"sum" => 50.0}])
524
566
  @context.sum(:age).should == 50.0
525
567
  end
@@ -120,6 +120,19 @@ describe Mongoid::Criteria do
120
120
 
121
121
  end
122
122
 
123
+ describe "#avg" do
124
+
125
+ before do
126
+ @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate)
127
+ @criteria.instance_variable_set(:@context, @context)
128
+ end
129
+
130
+ it "delegates to the context" do
131
+ @context.expects(:avg).with(:age)
132
+ @criteria.avg(:age)
133
+ end
134
+ end
135
+
123
136
  describe "#blank?" do
124
137
 
125
138
  before do
@@ -200,6 +213,19 @@ describe Mongoid::Criteria do
200
213
 
201
214
  end
202
215
 
216
+ describe "#distinct" do
217
+
218
+ before do
219
+ @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate)
220
+ @criteria.instance_variable_set(:@context, @context)
221
+ end
222
+
223
+ it "delegates to the context" do
224
+ @context.expects(:distinct).with(:title)
225
+ @criteria.distinct(:title)
226
+ end
227
+ end
228
+
203
229
  describe "#entries" do
204
230
 
205
231
  context "filtering" do
@@ -6,7 +6,14 @@ describe Mongoid::Extensions::Object::Conversions do
6
6
 
7
7
  it "returns its attributes" do
8
8
  Person.new(:_id => 1, :title => "Sir").mongoidize.should ==
9
- { "_id" => 1, "title" => "Sir", "age" => 100, "_type" => "Person", "blood_alcohol_content" => 0.0, "pets" => false}
9
+ {
10
+ "_id" => 1,
11
+ "title" => "Sir",
12
+ "age" => 100,
13
+ "_type" => "Person",
14
+ "blood_alcohol_content" => 0.0,
15
+ "pets" => false
16
+ }
10
17
  end
11
18
 
12
19
  end
@@ -28,7 +35,14 @@ describe Mongoid::Extensions::Object::Conversions do
28
35
  context "when object has attributes" do
29
36
 
30
37
  before do
31
- @attributes = { "_id" => "test", "title" => "Sir", "age" => 100, "_type" => "Person", "blood_alcohol_content" => 0.0, "pets" => false }
38
+ @attributes = {
39
+ "_id" => "test",
40
+ "title" => "Sir",
41
+ "age" => 100,
42
+ "_type" => "Person",
43
+ "blood_alcohol_content" => 0.0,
44
+ "pets" => false
45
+ }
32
46
  @person = Person.new(@attributes)
33
47
  end
34
48
 
@@ -13,6 +13,7 @@ describe Mongoid::Fields do
13
13
  describe "#defaults" do
14
14
 
15
15
  context "with defaults specified as a non-primitive" do
16
+
16
17
  before do
17
18
  Person.field(:hash_testing, :type => Hash, :default => {})
18
19
  Person.field(:array_testing, :type => Array, :default => [])
@@ -20,6 +21,11 @@ describe Mongoid::Fields do
20
21
  @second_person = Person.new
21
22
  end
22
23
 
24
+ after do
25
+ Person.fields.delete("hash_testing")
26
+ Person.fields.delete("array_testing")
27
+ end
28
+
23
29
  it "should not return the same object when calling defaults with a default hash" do
24
30
  @first_person.hash_testing.object_id.should_not == @second_person.hash_testing.object_id
25
31
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 13
9
- version: 1.2.13
8
+ - 14
9
+ version: 1.2.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - Durran Jordan
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-12 00:00:00 -05:00
17
+ date: 2010-03-17 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -40,9 +40,9 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
42
  - 0
43
- - 18
44
- - 3
45
- version: 0.18.3
43
+ - 19
44
+ - 1
45
+ version: 0.19.1
46
46
  type: :runtime
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
@@ -179,6 +179,7 @@ files:
179
179
  - lib/mongoid/extensions/integer/conversions.rb
180
180
  - lib/mongoid/extensions/nil/assimilation.rb
181
181
  - lib/mongoid/extensions/object/conversions.rb
182
+ - lib/mongoid/extensions/objectid/conversions.rb
182
183
  - lib/mongoid/extensions/proc/scoping.rb
183
184
  - lib/mongoid/extensions/string/conversions.rb
184
185
  - lib/mongoid/extensions/string/inflections.rb