orm_adapter 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- orm_adapter (0.3.0)
4
+ orm_adapter (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,3 +1,7 @@
1
+ == 0.4.0
2
+
3
+ * Adds :limit, and :offset options to #find_all [Fred Wu]
4
+
1
5
  == 0.3.0
2
6
 
3
7
  * Removes OrmAdapter::Base.model_classes and friends. This is a BC breaking change if you use .model_classes [Ian White]
@@ -51,7 +51,7 @@ orm_adapter is an extraction from {pickle}[http://github.com/ianwhite/pickle] by
51
51
 
52
52
  {Luke Cunningham}[http://github.com/icaruswings] contributes the Mongo Mapper adapter.
53
53
 
54
- {Fred Wu}[http://github.com/fredwu] contributes the #destroy methods.
54
+ {Fred Wu}[http://github.com/fredwu] contributes the #destroy methods, and :limit, :offset options for #find_all
55
55
 
56
56
  {Tim Galeckas}[http://github.com/timgaleckas]
57
57
 
@@ -19,14 +19,14 @@ module OrmAdapter
19
19
 
20
20
  # @see OrmAdapter::Base#find_first
21
21
  def find_first(options = {})
22
- conditions, order = extract_conditions_and_order!(options)
22
+ conditions, order = extract_conditions!(options)
23
23
  klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
24
24
  end
25
25
 
26
26
  # @see OrmAdapter::Base#find_all
27
27
  def find_all(options = {})
28
- conditions, order = extract_conditions_and_order!(options)
29
- klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).all
28
+ conditions, order, limit, offset = extract_conditions!(options)
29
+ klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).offset(offset).all
30
30
  end
31
31
 
32
32
  # @see OrmAdapter::Base#create!
@@ -24,14 +24,17 @@ module DataMapper
24
24
 
25
25
  # @see OrmAdapter::Base#find_first
26
26
  def find_first(options = {})
27
- conditions, order = extract_conditions_and_order!(options)
27
+ conditions, order = extract_conditions!(options)
28
28
  klass.first :conditions => conditions, :order => order_clause(order)
29
29
  end
30
30
 
31
31
  # @see OrmAdapter::Base#find_all
32
32
  def find_all(options = {})
33
- conditions, order = extract_conditions_and_order!(options)
34
- klass.all :conditions => conditions, :order => order_clause(order)
33
+ conditions, order, limit, offset = extract_conditions!(options)
34
+ opts = { :conditions => conditions, :order => order_clause(order) }
35
+ opts = opts.merge({ :limit => limit }) unless limit.nil?
36
+ opts = opts.merge({ :offset => offset }) unless offset.nil?
37
+ klass.all opts
35
38
  end
36
39
 
37
40
  # @see OrmAdapter::Base#create!
@@ -24,15 +24,17 @@ module MongoMapper
24
24
 
25
25
  # @see OrmAdapter::Base#find_first
26
26
  def find_first(conditions = {})
27
- conditions, order = extract_conditions_and_order!(conditions)
27
+ conditions, order = extract_conditions!(conditions)
28
28
  conditions = conditions.merge(:sort => order) unless order.nil?
29
29
  klass.first(conditions_to_fields(conditions))
30
30
  end
31
31
 
32
32
  # @see OrmAdapter::Base#find_all
33
33
  def find_all(conditions = {})
34
- conditions, order = extract_conditions_and_order!(conditions)
34
+ conditions, order, limit, offset = extract_conditions!(conditions)
35
35
  conditions = conditions.merge(:sort => order) unless order.nil?
36
+ conditions = conditions.merge(:limit => limit) unless limit.nil?
37
+ conditions = conditions.merge(:offset => offset) unless limit.nil? || offset.nil?
36
38
  klass.all(conditions_to_fields(conditions))
37
39
  end
38
40
 
@@ -24,14 +24,14 @@ module Mongoid
24
24
 
25
25
  # @see OrmAdapter::Base#find_first
26
26
  def find_first(options = {})
27
- conditions, order = extract_conditions_and_order!(options)
27
+ conditions, order = extract_conditions!(options)
28
28
  klass.limit(1).where(conditions_to_fields(conditions)).order_by(order).first
29
29
  end
30
30
 
31
31
  # @see OrmAdapter::Base#find_all
32
32
  def find_all(options = {})
33
- conditions, order = extract_conditions_and_order!(options)
34
- klass.where(conditions_to_fields(conditions)).order_by(order)
33
+ conditions, order, limit, offset = extract_conditions!(options)
34
+ klass.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
35
35
  end
36
36
 
37
37
  # @see OrmAdapter::Base#create!
@@ -88,11 +88,16 @@ module OrmAdapter
88
88
  key.is_a?(Array) ? key.first : key
89
89
  end
90
90
 
91
- # given an options hash, with optional :conditions and :order keys, returns conditions and normalized order
92
- def extract_conditions_and_order!(options = {})
93
- order = normalize_order(options.delete(:order))
91
+ # given an options hash,
92
+ # with optional :conditions, :order, :limit and :offset keys,
93
+ # returns conditions, normalized order, limit and offset
94
+ def extract_conditions!(options = {})
95
+ order = normalize_order(options.delete(:order))
96
+ limit = options.delete(:limit)
97
+ offset = options.delete(:offset)
94
98
  conditions = options.delete(:conditions) || options
95
- [conditions, order]
99
+
100
+ [conditions, order, limit, offset]
96
101
  end
97
102
 
98
103
  # given an order argument, returns an array of pairs, with each pair containing the attribute, and :asc or :desc
@@ -1,3 +1,3 @@
1
1
  module OrmAdapter
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -2,27 +2,45 @@ require 'spec_helper'
2
2
 
3
3
  describe OrmAdapter::Base do
4
4
  subject { OrmAdapter::Base.new(Object) }
5
-
6
- describe "#extract_conditions_and_order!" do
5
+
6
+ describe "#extract_conditions!" do
7
7
  let(:conditions) { {:foo => 'bar'} }
8
8
  let(:order) { [[:foo, :asc]] }
9
-
10
- it "(<conditions>) should return [<conditions>, []]" do
11
- subject.send(:extract_conditions_and_order!, conditions).should == [conditions, []]
9
+ let(:limit) { 1 }
10
+ let(:offset) { 2 }
11
+
12
+ it "(<conditions>)" do
13
+ subject.send(:extract_conditions!, conditions).should == [conditions, [], nil, nil]
14
+ end
15
+
16
+ it "(:conditions => <conditions>)" do
17
+ subject.send(:extract_conditions!, :conditions => conditions).should == [conditions, [], nil, nil]
18
+ end
19
+
20
+ it "(:order => <order>)" do
21
+ subject.send(:extract_conditions!, :order => order).should == [{}, order, nil, nil]
12
22
  end
13
-
14
- it "(:conditions => <conditions>) should return [<conditions>, []]" do
15
- subject.send(:extract_conditions_and_order!, :conditions => conditions).should == [conditions, []]
23
+
24
+ it "(:limit => <limit>)" do
25
+ subject.send(:extract_conditions!, :limit => limit).should == [{}, [], limit, nil]
16
26
  end
17
27
 
18
- it "(:order => <order>) should return [{}, <order>]" do
19
- subject.send(:extract_conditions_and_order!, :order => order).should == [{}, order]
28
+ it "(:offset => <offset>)" do
29
+ subject.send(:extract_conditions!, :offset => offset).should == [{}, [], nil, offset]
20
30
  end
21
-
22
- it "(:conditions => <conditions>, :order => <order>) should return [<conditions>, <order>]" do
23
- subject.send(:extract_conditions_and_order!, :conditions => conditions, :order => order).should == [conditions, order]
31
+
32
+ it "(:conditions => <conditions>, :order => <order>)" do
33
+ subject.send(:extract_conditions!, :conditions => conditions, :order => order).should == [conditions, order, nil, nil]
34
+ end
35
+
36
+ it "(:conditions => <conditions>, :limit => <limit>)" do
37
+ subject.send(:extract_conditions!, :conditions => conditions, :limit => limit).should == [conditions, [], limit, nil]
24
38
  end
25
-
39
+
40
+ it "(:conditions => <conditions>, :offset => <offset>)" do
41
+ subject.send(:extract_conditions!, :conditions => conditions, :offset => offset).should == [conditions, [], nil, offset]
42
+ end
43
+
26
44
  describe "#valid_object?" do
27
45
  it "determines whether an object is valid for the current model class" do
28
46
  subject.send(:valid_object?, Object.new).should be_true
@@ -34,27 +52,27 @@ describe OrmAdapter::Base do
34
52
  specify "(nil) returns []" do
35
53
  subject.send(:normalize_order, nil).should == []
36
54
  end
37
-
55
+
38
56
  specify ":foo returns [[:foo, :asc]]" do
39
57
  subject.send(:normalize_order, :foo).should == [[:foo, :asc]]
40
58
  end
41
-
59
+
42
60
  specify "[:foo] returns [[:foo, :asc]]" do
43
61
  subject.send(:normalize_order, [:foo]).should == [[:foo, :asc]]
44
62
  end
45
-
63
+
46
64
  specify "[:foo, :desc] returns [[:foo, :desc]]" do
47
65
  subject.send(:normalize_order, [:foo, :desc]).should == [[:foo, :desc]]
48
66
  end
49
-
67
+
50
68
  specify "[:foo, [:bar, :asc], [:baz, :desc], :bing] returns [[:foo, :asc], [:bar, :asc], [:baz, :desc], [:bing, :asc]]" do
51
69
  subject.send(:normalize_order, [:foo, [:bar, :asc], [:baz, :desc], :bing]).should == [[:foo, :asc], [:bar, :asc], [:baz, :desc], [:bing, :asc]]
52
70
  end
53
-
71
+
54
72
  specify "[[:foo, :wtf]] raises ArgumentError" do
55
73
  lambda { subject.send(:normalize_order, [[:foo, :wtf]]) }.should raise_error(ArgumentError)
56
74
  end
57
-
75
+
58
76
  specify "[[:foo, :asc, :desc]] raises ArgumentError" do
59
77
  lambda { subject.send(:normalize_order, [[:foo, :asc, :desc]]) }.should raise_error(ArgumentError)
60
78
  end
@@ -90,7 +90,7 @@ shared_examples_for "example app with orm_adapter" do
90
90
  it "should return nil if no conditions match" do
91
91
  user_adapter.find_first(:name => "Betty").should == nil
92
92
  end
93
-
93
+
94
94
  it 'should return the first model if no conditions passed' do
95
95
  user = create_model(user_class)
96
96
  create_model(user_class)
@@ -102,7 +102,7 @@ shared_examples_for "example app with orm_adapter" do
102
102
  note = create_model(note_class, :owner => user)
103
103
  note_adapter.find_first(:owner => user).should == note
104
104
  end
105
-
105
+
106
106
  it "understands :id as a primary key condition (allowing scoped finding)" do
107
107
  create_model(user_class, :name => "Fred")
108
108
  user = create_model(user_class, :name => "Fred")
@@ -173,6 +173,27 @@ shared_examples_for "example app with orm_adapter" do
173
173
  user_adapter.find_all(:conditions => {:name => "Fred"}, :order => [:rating, :desc]).should == [user2, user1]
174
174
  end
175
175
  end
176
+
177
+ describe "(:limit => <number of items>)" do
178
+ it "should return a limited set of matching models" do
179
+ user1 = create_model(user_class, :name => "Fred", :rating => 1)
180
+ user2 = create_model(user_class, :name => "Fred", :rating => 2)
181
+ user3 = create_model(user_class, :name => "Betty", :rating => 1)
182
+ user_adapter.find_all(:limit => 1).should == [user1]
183
+ user_adapter.find_all(:limit => 2).should == [user1, user2]
184
+ end
185
+ end
186
+
187
+ describe "(:offset => <offset number>) with limit (as DataMapper doesn't allow offset on its own)" do
188
+ it "should return an offset set of matching models" do
189
+ user1 = create_model(user_class, :name => "Fred", :rating => 1)
190
+ user2 = create_model(user_class, :name => "Fred", :rating => 2)
191
+ user3 = create_model(user_class, :name => "Betty", :rating => 1)
192
+ user_adapter.find_all(:limit => 3, :offset => 0).should == [user1, user2, user3]
193
+ user_adapter.find_all(:limit => 3, :offset => 1).should == [user2, user3]
194
+ user_adapter.find_all(:limit => 1, :offset => 1).should == [user2]
195
+ end
196
+ end
176
197
  end
177
198
 
178
199
  describe "#create!(attributes)" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orm_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-20 00:00:00.000000000 Z
13
+ date: 2012-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
17
- requirement: &70282396920860 !ruby/object:Gem::Requirement
17
+ requirement: &70358654083860 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70282396920860
25
+ version_requirements: *70358654083860
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: git
28
- requirement: &70282396919960 !ruby/object:Gem::Requirement
28
+ requirement: &70358654083320 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.2.5
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70282396919960
36
+ version_requirements: *70358654083320
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: yard
39
- requirement: &70282396919320 !ruby/object:Gem::Requirement
39
+ requirement: &70358654082820 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 0.6.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70282396919320
47
+ version_requirements: *70358654082820
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rake
50
- requirement: &70282396918380 !ruby/object:Gem::Requirement
50
+ requirement: &70358654082040 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 0.8.7
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70282396918380
58
+ version_requirements: *70358654082040
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: activerecord
61
- requirement: &70282396917120 !ruby/object:Gem::Requirement
61
+ requirement: &70358654080600 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 3.0.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70282396917120
69
+ version_requirements: *70358654080600
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: mongoid
72
- requirement: &70282396916500 !ruby/object:Gem::Requirement
72
+ requirement: &70358654079240 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 2.0.0.beta.20
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70282396916500
80
+ version_requirements: *70358654079240
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: mongo_mapper
83
- requirement: &70282396915800 !ruby/object:Gem::Requirement
83
+ requirement: &70358654078040 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 0.9.0
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *70282396915800
91
+ version_requirements: *70358654078040
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: bson_ext
94
- requirement: &70282396900760 !ruby/object:Gem::Requirement
94
+ requirement: &70358654077240 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: 1.3.0
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *70282396900760
102
+ version_requirements: *70358654077240
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rspec
105
- requirement: &70282396898120 !ruby/object:Gem::Requirement
105
+ requirement: &70358654076340 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: 2.4.0
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *70282396898120
113
+ version_requirements: *70358654076340
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: sqlite3
116
- requirement: &70282396896760 !ruby/object:Gem::Requirement
116
+ requirement: &70358654056980 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: 1.3.2
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *70282396896760
124
+ version_requirements: *70358654056980
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: datamapper
127
- requirement: &70282396896060 !ruby/object:Gem::Requirement
127
+ requirement: &70358654056260 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ! '>='
@@ -132,10 +132,10 @@ dependencies:
132
132
  version: '1.0'
133
133
  type: :development
134
134
  prerelease: false
135
- version_requirements: *70282396896060
135
+ version_requirements: *70358654056260
136
136
  - !ruby/object:Gem::Dependency
137
137
  name: dm-sqlite-adapter
138
- requirement: &70282396895200 !ruby/object:Gem::Requirement
138
+ requirement: &70358654055740 !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
141
141
  - - ! '>='
@@ -143,10 +143,10 @@ dependencies:
143
143
  version: '1.0'
144
144
  type: :development
145
145
  prerelease: false
146
- version_requirements: *70282396895200
146
+ version_requirements: *70358654055740
147
147
  - !ruby/object:Gem::Dependency
148
148
  name: dm-active_model
149
- requirement: &70282396894300 !ruby/object:Gem::Requirement
149
+ requirement: &70358654055220 !ruby/object:Gem::Requirement
150
150
  none: false
151
151
  requirements:
152
152
  - - ! '>='
@@ -154,7 +154,7 @@ dependencies:
154
154
  version: '1.0'
155
155
  type: :development
156
156
  prerelease: false
157
- version_requirements: *70282396894300
157
+ version_requirements: *70358654055220
158
158
  description: Provides a single point of entry for using basic features of ruby ORMs
159
159
  email: ian.w.white@gmail.com
160
160
  executables: []
@@ -201,7 +201,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  segments:
203
203
  - 0
204
- hash: 3963130271152065035
204
+ hash: 3652466131119741366
205
205
  required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  none: false
207
207
  requirements: