mongoid 0.9.12 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.12
1
+ 0.10.0
@@ -40,6 +40,7 @@ require "mongoid/associations"
40
40
  require "mongoid/associations/options"
41
41
  require "mongoid/attributes"
42
42
  require "mongoid/commands"
43
+ require "mongoid/complex_criterion"
43
44
  require "mongoid/criteria"
44
45
  require "mongoid/dynamic_finder"
45
46
  require "mongoid/extensions"
@@ -0,0 +1,9 @@
1
+ module Mongoid #:nodoc:
2
+ class ComplexCriterion
3
+ attr_accessor :key, :operator
4
+
5
+ def initialize opts = {}
6
+ @key, @operator = opts[:key], opts[:operator]
7
+ end
8
+ end
9
+ end
@@ -465,7 +465,7 @@ module Mongoid #:nodoc:
465
465
  when String
466
466
  @selector.update("$where" => selector)
467
467
  else
468
- @selector.update(selector || {})
468
+ @selector.update(selector ? selector.expand_complex_criteria : {})
469
469
  end
470
470
  self
471
471
  end
@@ -10,6 +10,7 @@ require "mongoid/extensions/float/conversions"
10
10
  require "mongoid/extensions/hash/accessors"
11
11
  require "mongoid/extensions/hash/assimilation"
12
12
  require "mongoid/extensions/hash/conversions"
13
+ require "mongoid/extensions/hash/criteria_helpers"
13
14
  require "mongoid/extensions/integer/conversions"
14
15
  require "mongoid/extensions/object/conversions"
15
16
  require "mongoid/extensions/string/conversions"
@@ -44,6 +45,7 @@ class Hash #:nodoc
44
45
  include Mongoid::Extensions::Hash::Accessors
45
46
  include Mongoid::Extensions::Hash::Assimilation
46
47
  extend Mongoid::Extensions::Hash::Conversions
48
+ include Mongoid::Extensions::Hash::CriteriaHelpers
47
49
  end
48
50
 
49
51
  class Integer #:nodoc
@@ -0,0 +1,19 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Hash #:nodoc:
4
+ module CriteriaHelpers #:nodoc:
5
+ def expand_complex_criteria
6
+ hsh = {}
7
+ self.each_pair do |k,v|
8
+ if k.class == Mongoid::ComplexCriterion
9
+ hsh[k.key] = {"$#{k.operator}" => v}
10
+ else
11
+ hsh[k] = v
12
+ end
13
+ end
14
+ hsh
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -23,6 +23,13 @@ module Mongoid #:nodoc:
23
23
  to_s.plural?
24
24
  end
25
25
 
26
+ ["gt", "lt", "gte", "lte", "ne", "in", "nin", "mod", "all", "size", "exists"].each do |oper|
27
+ class_eval <<-OPERATORS
28
+ def #{oper}
29
+ ComplexCriterion.new(:key => self, :operator => "#{oper}")
30
+ end
31
+ OPERATORS
32
+ end
26
33
  end
27
34
  end
28
35
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.9.12"
8
+ s.version = "0.10.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{2009-12-21}
12
+ s.date = %q{2009-12-24}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
39
39
  "lib/mongoid/commands/destroy.rb",
40
40
  "lib/mongoid/commands/destroy_all.rb",
41
41
  "lib/mongoid/commands/save.rb",
42
+ "lib/mongoid/complex_criterion.rb",
42
43
  "lib/mongoid/criteria.rb",
43
44
  "lib/mongoid/document.rb",
44
45
  "lib/mongoid/dynamic_finder.rb",
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
55
56
  "lib/mongoid/extensions/hash/accessors.rb",
56
57
  "lib/mongoid/extensions/hash/assimilation.rb",
57
58
  "lib/mongoid/extensions/hash/conversions.rb",
59
+ "lib/mongoid/extensions/hash/criteria_helpers.rb",
58
60
  "lib/mongoid/extensions/integer/conversions.rb",
59
61
  "lib/mongoid/extensions/object/casting.rb",
60
62
  "lib/mongoid/extensions/object/conversions.rb",
@@ -69,6 +71,7 @@ Gem::Specification.new do |s|
69
71
  "mongoid.gemspec",
70
72
  "perf/benchmark.rb",
71
73
  "spec/integration/mongoid/associations_spec.rb",
74
+ "spec/integration/mongoid/criteria_spec.rb",
72
75
  "spec/integration/mongoid/document_spec.rb",
73
76
  "spec/integration/mongoid/finders_spec.rb",
74
77
  "spec/spec.opts",
@@ -104,6 +107,7 @@ Gem::Specification.new do |s|
104
107
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
105
108
  "spec/unit/mongoid/extensions/hash/assimilation_spec.rb",
106
109
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
110
+ "spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb",
107
111
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
108
112
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
109
113
  "spec/unit/mongoid/extensions/string/conversions_spec.rb",
@@ -123,6 +127,7 @@ Gem::Specification.new do |s|
123
127
  s.summary = %q{ODM framework for MongoDB}
124
128
  s.test_files = [
125
129
  "spec/integration/mongoid/associations_spec.rb",
130
+ "spec/integration/mongoid/criteria_spec.rb",
126
131
  "spec/integration/mongoid/document_spec.rb",
127
132
  "spec/integration/mongoid/finders_spec.rb",
128
133
  "spec/spec_helper.rb",
@@ -157,6 +162,7 @@ Gem::Specification.new do |s|
157
162
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
158
163
  "spec/unit/mongoid/extensions/hash/assimilation_spec.rb",
159
164
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
165
+ "spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb",
160
166
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
161
167
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
162
168
  "spec/unit/mongoid/extensions/string/conversions_spec.rb",
@@ -0,0 +1,109 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Criteria do
4
+
5
+ before do
6
+ @person = Person.create(:title => "Sir", :age => 33, :aliases => ["D", "Durran"])
7
+ end
8
+
9
+ after do
10
+ Person.delete_all
11
+ end
12
+
13
+ describe "#where" do
14
+
15
+ context "with complex criterion" do
16
+
17
+ context "#all" do
18
+
19
+ it "returns those matching an all clause" do
20
+ Person.criteria.where(:title.all => ["Sir"]).should == [@person]
21
+ end
22
+
23
+ end
24
+
25
+ context "#exists" do
26
+
27
+ it "returns those matching an exists clause" do
28
+ Person.criteria.where(:title.exists => true).should == [@person]
29
+ end
30
+
31
+ end
32
+
33
+ context "#gt" do
34
+
35
+ it "returns those matching a gt clause" do
36
+ Person.criteria.where(:age.gt => 30).should == [@person]
37
+ end
38
+
39
+ end
40
+
41
+ context "#gte" do
42
+
43
+ it "returns those matching a gte clause" do
44
+ Person.criteria.where(:age.gte => 33).should == [@person]
45
+ end
46
+
47
+ end
48
+
49
+ context "#in" do
50
+
51
+ it "returns those matching an in clause" do
52
+ Person.criteria.where(:title.in => ["Sir", "Madam"]).should == [@person]
53
+ end
54
+
55
+ end
56
+
57
+ context "#lt" do
58
+
59
+ it "returns those matching a lt clause" do
60
+ Person.criteria.where(:age.lt => 34).should == [@person]
61
+ end
62
+
63
+ end
64
+
65
+ context "#lte" do
66
+
67
+ it "returns those matching a lte clause" do
68
+ Person.criteria.where(:age.lte => 33).should == [@person]
69
+ end
70
+
71
+ end
72
+
73
+ context "#mod" do
74
+
75
+ it "returns those matching a mod clause" do
76
+ Person.criteria.where(:age.lte => 33).should == [@person]
77
+ end
78
+
79
+ end
80
+
81
+ context "#ne" do
82
+
83
+ it "returns those matching a ne clause" do
84
+ Person.criteria.where(:age.ne => 50).should == [@person]
85
+ end
86
+
87
+ end
88
+
89
+ context "#nin" do
90
+
91
+ it "returns those matching a nin clause" do
92
+ Person.criteria.where(:title.nin => ["Esquire", "Congressman"]).should == [@person]
93
+ end
94
+
95
+ end
96
+
97
+ context "#size" do
98
+
99
+ it "returns those matching a size clause" do
100
+ Person.criteria.where(:aliases.size => 2).should == [@person]
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -30,6 +30,7 @@ class Person < Mongoid::Document
30
30
  field :mixed_drink, :type => MixedDrink
31
31
  field :employer_id
32
32
  field :lunch_time, :type => Time
33
+ field :aliases, :type => Array
33
34
 
34
35
  has_many :addresses
35
36
  has_many :phone_numbers, :class_name => "Phone"
@@ -885,9 +885,23 @@ describe Mongoid::Criteria do
885
885
 
886
886
  context "when provided a hash" do
887
887
 
888
- it "adds the clause to the selector" do
889
- @criteria.where(:title => "Title", :text => "Text")
890
- @criteria.selector.should == { :title => "Title", :text => "Text" }
888
+ context "with simple hash keys" do
889
+
890
+ it "adds the clause to the selector" do
891
+ @criteria.where(:title => "Title", :text => "Text")
892
+ @criteria.selector.should == { :title => "Title", :text => "Text" }
893
+ end
894
+
895
+ end
896
+
897
+ context "with complex hash keys" do
898
+
899
+ it "adds the correct clause to the selector" do
900
+ dob = 40.years.ago
901
+ @criteria.where(:age.gt => 40, :title => "Title", :dob.lt => dob)
902
+ @criteria.selector.should == {:age => {"$gt" => 40}, :title => "Title", :dob => {"$lt" => dob}}
903
+ end
904
+
891
905
  end
892
906
 
893
907
  end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Extensions::Hash::CriteriaHelpers do
4
+
5
+ describe "#expand_complex_criteria" do
6
+
7
+ before do
8
+ @hash = {:age.gt => 40, :title => "Title"}
9
+ end
10
+
11
+ it "expands complex criteria to form a valid `where` hash" do
12
+ @hash.expand_complex_criteria.should == {:age => {"$gt" => 40}, :title => "Title"}
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -78,4 +78,14 @@ describe Mongoid::Extensions::Symbol::Inflections do
78
78
 
79
79
  end
80
80
 
81
+ describe "#gt" do
82
+
83
+ it 'returns :"foo $gt"' do
84
+ ret = :foo.gt
85
+ ret.key.should == :foo
86
+ ret.operator.should == "gt"
87
+ end
88
+
89
+ end
90
+
81
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-21 00:00:00 +01:00
12
+ date: 2009-12-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -115,6 +115,7 @@ files:
115
115
  - lib/mongoid/commands/destroy.rb
116
116
  - lib/mongoid/commands/destroy_all.rb
117
117
  - lib/mongoid/commands/save.rb
118
+ - lib/mongoid/complex_criterion.rb
118
119
  - lib/mongoid/criteria.rb
119
120
  - lib/mongoid/document.rb
120
121
  - lib/mongoid/dynamic_finder.rb
@@ -131,6 +132,7 @@ files:
131
132
  - lib/mongoid/extensions/hash/accessors.rb
132
133
  - lib/mongoid/extensions/hash/assimilation.rb
133
134
  - lib/mongoid/extensions/hash/conversions.rb
135
+ - lib/mongoid/extensions/hash/criteria_helpers.rb
134
136
  - lib/mongoid/extensions/integer/conversions.rb
135
137
  - lib/mongoid/extensions/object/casting.rb
136
138
  - lib/mongoid/extensions/object/conversions.rb
@@ -145,6 +147,7 @@ files:
145
147
  - mongoid.gemspec
146
148
  - perf/benchmark.rb
147
149
  - spec/integration/mongoid/associations_spec.rb
150
+ - spec/integration/mongoid/criteria_spec.rb
148
151
  - spec/integration/mongoid/document_spec.rb
149
152
  - spec/integration/mongoid/finders_spec.rb
150
153
  - spec/spec.opts
@@ -180,6 +183,7 @@ files:
180
183
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
181
184
  - spec/unit/mongoid/extensions/hash/assimilation_spec.rb
182
185
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
186
+ - spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb
183
187
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
184
188
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
185
189
  - spec/unit/mongoid/extensions/string/conversions_spec.rb
@@ -221,6 +225,7 @@ specification_version: 3
221
225
  summary: ODM framework for MongoDB
222
226
  test_files:
223
227
  - spec/integration/mongoid/associations_spec.rb
228
+ - spec/integration/mongoid/criteria_spec.rb
224
229
  - spec/integration/mongoid/document_spec.rb
225
230
  - spec/integration/mongoid/finders_spec.rb
226
231
  - spec/spec_helper.rb
@@ -255,6 +260,7 @@ test_files:
255
260
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
256
261
  - spec/unit/mongoid/extensions/hash/assimilation_spec.rb
257
262
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
263
+ - spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb
258
264
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
259
265
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
260
266
  - spec/unit/mongoid/extensions/string/conversions_spec.rb