public
Fork of ndemonner/stone
Description: Plug-and-play data persistence created for small Ruby web applications.
Homepage: http://stone.rubyforge.org
Clone URL: git://github.com/defunkt/stone.git
Search Repo:
Added result ordering functionality for Resource.all
ndemonner (author)
Wed Apr 16 10:18:49 -0700 2008
commit  3431f5c18675490648e8bf7fbc6f858580d838ea
tree    edd6ca3ea05c4957cd41eb07ace85f0d7abcda0c
parent  5b109b7d629198864cdffe8a298f7363bd1d4d07
...
58
59
60
61
 
62
63
64
...
58
59
60
 
61
62
63
64
0
@@ -58,7 +58,7 @@ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
   
0
   # == Optional
0
   p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
- p.extra_deps = [['english', '>= 0.2.0'], ['validatable', '>= 1.6.7'], ['facets', '>= 2.4.1'], ['fastercsv', '>= 1.2.3']]
0
+ p.extra_deps = [['english', '>= 0.2.0'], ['validatable', '>= 1.6.7'], ['facets', '>= 2.4.1']]
0
   
0
   #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
   
...
198
199
200
 
 
 
 
201
202
 
 
 
 
 
 
203
204
205
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
0
@@ -198,8 +198,18 @@ module Stone
0
           objs << o[1]
0
         end
0
       else
0
+ if conditions[:order]
0
+ order = conditions[:order].to_a.flatten
0
+ conditions.delete(:order)
0
+ end
0
         objs = find(conditions, self.to_s.make_key)
0
       end
0
+ if order
0
+ raise "Order should be passed with :asc or :desc, got #{order[1].inspect}" \
0
+ unless [:asc,:desc].include? order[1]
0
+ objs.sort! {|x,y| x.send(order[0]) <=> y.send(order[0])}
0
+ objs.reverse! if order[1] == :desc
0
+ end
0
       objs
0
     end
0
     
...
58
59
60
61
 
62
63
64
...
58
59
60
 
61
62
63
64
0
@@ -58,7 +58,7 @@ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
   
0
   # == Optional
0
   p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
- p.extra_deps = [['english', '>= 0.2.0'], ['validatable', '>= 1.6.7'], ['facets', '>= 2.4.1'], ['fastercsv', '>= 1.2.3']]
0
+ p.extra_deps = [['english', '>= 0.2.0'], ['validatable', '>= 1.6.7'], ['facets', '>= 2.4.1']]
0
   
0
   #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
   
...
198
199
200
 
 
 
 
201
202
 
 
 
 
 
 
203
204
205
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
0
@@ -198,8 +198,18 @@ module Stone
0
           objs << o[1]
0
         end
0
       else
0
+ if conditions[:order]
0
+ order = conditions[:order].to_a.flatten
0
+ conditions.delete(:order)
0
+ end
0
         objs = find(conditions, self.to_s.make_key)
0
       end
0
+ if order
0
+ raise "Order should be passed with :asc or :desc, got #{order[1].inspect}" \
0
+ unless [:asc,:desc].include? order[1]
0
+ objs.sort! {|x,y| x.send(order[0]) <=> y.send(order[0])}
0
+ objs.reverse! if order[1] == :desc
0
+ end
0
       objs
0
     end
0
     
...
186
187
188
 
 
 
 
 
 
 
 
 
189
190
191
...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
0
@@ -186,5 +186,14 @@ describe Stone::Resource do
0
     authors = Author.all(:name.matches => /o/, :created_at.lt => DateTime.now>>1)
0
     authors.size.should == 2
0
   end
0
+
0
+ it "should allow for ordering of query results" do
0
+ authors = Author.all(:created_at.lt => DateTime.now>>1, :order => {:created_at => :desc})
0
+ authors.last.name.should == "Nick DeMonner"
0
+ end
0
+
0
+ it "should only accept :asc or :desc for ordering" do
0
+ lambda {Author.all(:created_at.lt => DateTime.now>>1, :order => {:created_at => :cool})}.should raise_error
0
+ end
0
 
0
 end
0
\ No newline at end of file
...
148
149
150
 
 
 
 
151
152
153
...
148
149
150
151
152
153
154
155
156
157
0
@@ -148,6 +148,10 @@ h3(#gppd). Get, Post, Put, and Delete
0
   # brings back all authors whose email contains "gmail.com", and who
0
   # were created before today
0
   Author.all(:email.includes => "gmail.com", :created_at.lt => DateTime.now)
0
+
0
+ # brings back all Authors created before today, and orders them descending
0
+ # from most recent to least
0
+ Author.all(:created_at.lt => DateTime.now, :order => {:created_at => :desc})
0
 </pre>
0
 
0
 * Posting
...
102
103
104
 
105
106
 
107
108
109
...
114
115
116
 
117
118
119
...
102
103
104
105
106
 
107
108
109
110
...
115
116
117
118
119
120
121
0
@@ -102,8 +102,9 @@ pre, code {
0
 .expr { color: #daefa3; }
0
 
0
 #version {
0
+ width:250px;
0
   float: right;
0
- text-align: right;
0
+ text-align: center;
0
   font-family: sans-serif;
0
   font-weight: normal;
0
   background-color: #cb6b4e;
0
@@ -114,6 +115,7 @@ pre, code {
0
 }
0
 
0
 #version .numbers {
0
+ text-align:center;
0
   display: block;
0
   font-size: 4em;
0
   line-height: 0.8em;
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-15T16:30:05-07:00
0
+created_at: 2008-04-16T10:16:10-07:00
0
 email: nick@cladby.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 19
0
+favorite_number: 2
0
 id: 1
0
 name: Nick DeMonner
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-15T16:30:05-07:00
0
+created_at: 2008-04-16T10:16:10-07:00
0
 email: heyo@something.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 14
0
+favorite_number: 9
0
 id: 2
0
 name: Mike McMichaels
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-15T16:30:05-07:00
0
+created_at: 2008-04-16T10:16:10-07:00
0
 email: weyo@something.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 8
0
+favorite_number: 33
0
 id: 3
0
 name: Mary Poppins
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-15T16:30:05-07:00
0
+created_at: 2008-04-16T10:16:10-07:00
0
 email: nick@gmail.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 24
0
+favorite_number: 15
0
 id: 4
0
 name: Nick Hicklesby
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-15T16:30:05-07:00
0
+created_at: 2008-04-16T10:16:10-07:00
0
 email: chariot_guy@hotmail.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 47
0
+favorite_number: 24
0
 id: 5
0
 name: Ben Hurr
0
 times_validated_hash:
...
186
187
188
 
 
 
 
 
 
 
 
 
189
190
191
...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
0
@@ -186,5 +186,14 @@ describe Stone::Resource do
0
     authors = Author.all(:name.matches => /o/, :created_at.lt => DateTime.now>>1)
0
     authors.size.should == 2
0
   end
0
+
0
+ it "should allow for ordering of query results" do
0
+ authors = Author.all(:created_at.lt => DateTime.now>>1, :order => {:created_at => :desc})
0
+ authors.last.name.should == "Nick DeMonner"
0
+ end
0
+
0
+ it "should only accept :asc or :desc for ordering" do
0
+ lambda {Author.all(:created_at.lt => DateTime.now>>1, :order => {:created_at => :cool})}.should raise_error
0
+ end
0
 
0
 end
0
\ No newline at end of file
...
148
149
150
 
 
 
 
151
152
153
...
148
149
150
151
152
153
154
155
156
157
0
@@ -148,6 +148,10 @@ h3(#gppd). Get, Post, Put, and Delete
0
   # brings back all authors whose email contains "gmail.com", and who
0
   # were created before today
0
   Author.all(:email.includes => "gmail.com", :created_at.lt => DateTime.now)
0
+
0
+ # brings back all Authors created before today, and orders them descending
0
+ # from most recent to least
0
+ Author.all(:created_at.lt => DateTime.now, :order => {:created_at => :desc})
0
 </pre>
0
 
0
 * Posting
...
102
103
104
 
105
106
 
107
108
109
...
114
115
116
 
117
118
119
...
102
103
104
105
106
 
107
108
109
110
...
115
116
117
118
119
120
121
0
@@ -102,8 +102,9 @@ pre, code {
0
 .expr { color: #daefa3; }
0
 
0
 #version {
0
+ width:250px;
0
   float: right;
0
- text-align: right;
0
+ text-align: center;
0
   font-family: sans-serif;
0
   font-weight: normal;
0
   background-color: #cb6b4e;
0
@@ -114,6 +115,7 @@ pre, code {
0
 }
0
 
0
 #version .numbers {
0
+ text-align:center;
0
   display: block;
0
   font-size: 4em;
0
   line-height: 0.8em;

Comments

    No one has commented yet.