ohm 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ohm.rb +9 -5
  2. data/test/indices_test.rb +20 -0
  3. 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
- @redis
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
- @redis = Ohm::Redis.new(*options)
26
+ self.redis = Ohm::Redis.new(*options)
23
27
  end
24
28
 
25
29
  # Clear the database.
26
30
  def flush
27
- @redis.flushdb
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
- read_local(att)
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
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.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens