ohm 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ohm.rb CHANGED
@@ -69,15 +69,22 @@ module Ohm
69
69
  # @example Get five posts sorted by number of votes and starting from the number 5 (zero based):
70
70
  # @blog.posts.sort(Post, :by => :votes, :start => 5, :limit => 10")
71
71
  def sort(options = {})
72
+ return [] if empty?
72
73
  options[:start] ||= 0
73
74
  options[:limit] = [options[:start], options[:limit]] if options[:limit]
74
75
  instantiate(db.sort(key, options))
75
76
  end
76
77
 
78
+ # Sort the model instances by the given value.
77
79
  def sort_by(att, options = {})
78
80
  sort(options.merge(:by => model.key("*", att)))
79
81
  end
80
82
 
83
+ # @return [Ohm::Model, nil] Returns the first instance found or nil.
84
+ def first
85
+ sort(:limit => 1).first
86
+ end
87
+
81
88
  def to_ary
82
89
  all
83
90
  end
@@ -17,7 +17,12 @@ class IndicesTest < Test::Unit::TestCase
17
17
  end
18
18
 
19
19
  should "be able to find by the given attribute" do
20
- assert_equal [@user1], User.find(:email, "foo")
20
+ assert_equal @user1, User.find(:email, "foo").first
21
+ end
22
+
23
+ should "return nil if no results are found" do
24
+ assert User.find(:email, "foobar").empty?
25
+ assert_equal nil, User.find(:email, "foobar").first
21
26
  end
22
27
 
23
28
  should "update indices when changing attribute values" do
@@ -181,6 +181,11 @@ class TestRedis < Test::Unit::TestCase
181
181
 
182
182
  assert_equal %w[A B C D], Person.all.sort_by(:name, :order => "ALPHA").map { |person| person.name }
183
183
  end
184
+
185
+ should "return an empty array if there are no elements to sort" do
186
+ Ohm.flush
187
+ assert_equal [], Person.all.sort_by(:name)
188
+ end
184
189
  end
185
190
 
186
191
  context "Loading attributes" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-07-27 00:00:00 -03:00
13
+ date: 2009-07-28 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16