ohm 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ohm.rb +9 -5
- data/test/indices_test.rb +20 -0
- metadata +1 -1
data/lib/ohm.rb
CHANGED
@@ -6,7 +6,11 @@ module Ohm
|
|
6
6
|
|
7
7
|
# Provides access to the Redis database. This is shared accross all models and instances.
|
8
8
|
def redis
|
9
|
-
|
9
|
+
Thread.current[:redis]
|
10
|
+
end
|
11
|
+
|
12
|
+
def redis=(connection)
|
13
|
+
Thread.current[:redis] = connection
|
10
14
|
end
|
11
15
|
|
12
16
|
# Connect to a redis database.
|
@@ -19,12 +23,12 @@ module Ohm
|
|
19
23
|
# @example Connect to a database in port 6380.
|
20
24
|
# Ohm.connect(:port => 6380)
|
21
25
|
def connect(*options)
|
22
|
-
|
26
|
+
self.redis = Ohm::Redis.new(*options)
|
23
27
|
end
|
24
28
|
|
25
29
|
# Clear the database.
|
26
30
|
def flush
|
27
|
-
|
31
|
+
redis.flushdb
|
28
32
|
end
|
29
33
|
|
30
34
|
# Join the parameters with ":" to create a key.
|
@@ -32,7 +36,7 @@ module Ohm
|
|
32
36
|
args.join(":")
|
33
37
|
end
|
34
38
|
|
35
|
-
module_function :key, :connect, :flush, :redis
|
39
|
+
module_function :key, :connect, :flush, :redis, :redis=
|
36
40
|
|
37
41
|
module Attributes
|
38
42
|
class Collection
|
@@ -490,7 +494,7 @@ module Ohm
|
|
490
494
|
|
491
495
|
def read_locals(attrs)
|
492
496
|
attrs.map do |att|
|
493
|
-
|
497
|
+
send(att)
|
494
498
|
end
|
495
499
|
end
|
496
500
|
|
data/test/indices_test.rb
CHANGED
@@ -5,6 +5,11 @@ class IndicesTest < Test::Unit::TestCase
|
|
5
5
|
attribute :email
|
6
6
|
|
7
7
|
index :email
|
8
|
+
index :email_provider
|
9
|
+
|
10
|
+
def email_provider
|
11
|
+
email.split("@").last
|
12
|
+
end
|
8
13
|
end
|
9
14
|
|
10
15
|
context "A model with an indexed attribute" do
|
@@ -43,4 +48,19 @@ class IndicesTest < Test::Unit::TestCase
|
|
43
48
|
assert_equal [@user3], User.find(:email, "baz qux")
|
44
49
|
end
|
45
50
|
end
|
51
|
+
|
52
|
+
context "Indexing arbitrary attributes" do
|
53
|
+
setup do
|
54
|
+
Ohm.flush
|
55
|
+
|
56
|
+
@user1 = User.create(:email => "foo@gmail.com")
|
57
|
+
@user2 = User.create(:email => "bar@gmail.com")
|
58
|
+
@user3 = User.create(:email => "bazqux@yahoo.com")
|
59
|
+
end
|
60
|
+
|
61
|
+
should "allow indexing by an arbitrary attribute" do
|
62
|
+
assert_equal [@user1, @user2], User.find(:email_provider, "gmail.com").to_a.sort_by { |u| u.id }
|
63
|
+
assert_equal [@user3], User.find(:email_provider, "yahoo.com")
|
64
|
+
end
|
65
|
+
end
|
46
66
|
end
|