redis_support 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redis_support.rb CHANGED
@@ -7,31 +7,8 @@ require 'redis_support/class_extensions'
7
7
  require 'redis_support/locks'
8
8
 
9
9
  module RedisSupport
10
- # Inspired/take from the redis= in Resque
11
- #
12
- # Accepts:
13
- # 1. A 'hostname:port' string
14
- # 2. A 'hostname:port:db' string (to select the Redis db)
15
- # 3. An instance of `Redis`, `Redis::Client`
16
- def self.redis=(connection)
17
- if connection.respond_to? :split
18
- host, port, db = connection.split(':')
19
- @redis = Redis.new(:host => host,:port => port,:thread_safe => true,:db => db)
20
- else
21
- @redis = connection
22
- end
23
- end
24
-
25
- def self.redis
26
- @redis
27
- end
28
-
29
- def redis=(redis)
30
- @redis = redis
31
- end
32
-
33
10
  def redis
34
- @redis || RedisSupport.redis
11
+ self.class.redis
35
12
  end
36
13
 
37
14
  def keys
@@ -42,6 +19,22 @@ module RedisSupport
42
19
 
43
20
  def self.included(model)
44
21
  model.extend ClassMethods
45
- model.extend RedisSupport
46
22
  end
23
+
24
+ # Inspired/take from the redis= in Resque
25
+ #
26
+ # Accepts:
27
+ # 1. A 'hostname:port' string
28
+ # 2. A 'hostname:port:db' string (to select the Redis db)
29
+ # 3. An instance of `Redis`, `Redis::Client`
30
+ def self.redis_connect(connection)
31
+ if connection.respond_to? :split
32
+ host, port, db = connection.split(':')
33
+ Redis.new(:host => host,:port => port,:thread_safe => true,:db => db)
34
+ else
35
+ connection
36
+ end
37
+ end
38
+
39
+ extend ClassMethods
47
40
  end
@@ -6,6 +6,14 @@ module RedisSupport
6
6
  VAR_PATTERN = /^[A-Z]+(_[A-Z]+)*$/
7
7
  STR_PATTERN = /^[a-z_]+$/
8
8
  module ClassMethods
9
+ def redis=(connection)
10
+ @redis = RedisSupport.redis_connect(connection)
11
+ end
12
+
13
+ def redis
14
+ @redis || RedisSupport.redis
15
+ end
16
+
9
17
  # Goal is to allow a class to declare a redis key/property
10
18
  # The key is a colon delimited string where variables
11
19
  # are listed are upper case (underscores inbetween) and
@@ -1,8 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- context "Redis Support setup" do
3
+ context "Redis Support Setup" do
4
4
  setup do
5
5
  RedisSupport.redis = "localhost:9736"
6
+ TestClass.redis = nil
7
+ SecondTest.redis = nil
6
8
  end
7
9
 
8
10
  test "redis is loaded correctly" do
@@ -20,6 +22,7 @@ end
20
22
  context "Redis Support" do
21
23
  setup do
22
24
  RedisSupport.redis = "localhost:9736"
25
+ TestClass.redis = nil
23
26
  TestClass.redis.flushall
24
27
  @test_class = TestClass.new
25
28
  end
@@ -31,10 +34,9 @@ context "Redis Support" do
31
34
  end
32
35
 
33
36
  test "redis connections changes as expected" do
34
- TestClass.redis = "localhost:6379"
35
- assert_equal @test_class.redis, TestClass.redis
36
- @test_class.redis = "localhost:9736"
37
- assert_equal @test_class.redis, TestClass.redis
37
+ TestClass.redis = "localhost:2345"
38
+ assert_equal @test_class.redis.client.port, RedisSupport.redis_connect("localhost:2345").client.port
39
+ assert_equal TestClass.redis.client.port, RedisSupport.redis_connect("localhost:2345").client.port
38
40
  end
39
41
 
40
42
  test "redis keys are created correctly in normal conditions" do
@@ -70,3 +72,44 @@ context "Redis Support" do
70
72
  end
71
73
  end
72
74
  end
75
+
76
+ context "Including Redis Support" do
77
+ setup do
78
+ class Foo
79
+ include RedisSupport
80
+ end
81
+
82
+ class Bar
83
+ include RedisSupport
84
+ end
85
+
86
+ Foo.redis = "localhost:2345"
87
+ Bar.redis = "localhost:3456"
88
+
89
+ @foo = Foo.new
90
+ end
91
+
92
+ test "return different server definitions when set" do
93
+ RedisSupport.redis = Redis.new(:port => 1234, :host => "localhost")
94
+ assert_equal Foo.redis, Foo.redis # I'm sane, really.
95
+ assert_not_equal Foo.redis, Bar.redis
96
+ assert_not_equal Foo.redis, RedisSupport.redis
97
+ assert_equal @foo.redis, Foo.redis
98
+
99
+ assert_raise(NoMethodError) do
100
+ @foo.redis = "localhost:3456"
101
+ end
102
+ end
103
+
104
+ test "same test runs twice in a row (return different server definitions when set)" do
105
+ RedisSupport.redis = Redis.new(:port => 1234, :host => "localhost")
106
+ assert_equal Foo.redis, Foo.redis # I'm sane, really.
107
+ assert_not_equal Foo.redis, Bar.redis
108
+ assert_not_equal Foo.redis, RedisSupport.redis
109
+ assert_equal @foo.redis, Foo.redis
110
+
111
+ assert_raise(NoMethodError) do
112
+ @foo.redis = "localhost:3456"
113
+ end
114
+ end
115
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_support
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 4
9
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
10
11
  platform: ruby
11
12
  authors:
12
13
  - Brian P O'Rourke
@@ -22,9 +23,11 @@ dependencies:
22
23
  name: redis
23
24
  prerelease: false
24
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
25
27
  requirements:
26
28
  - - ">="
27
29
  - !ruby/object:Gem::Version
30
+ hash: 31
28
31
  segments:
29
32
  - 1
30
33
  - 0
@@ -45,6 +48,8 @@ files:
45
48
  - lib/redis_support/class_extensions.rb
46
49
  - lib/redis_support/locks.rb
47
50
  - README.md
51
+ - test/helper.rb
52
+ - test/test_redis_support.rb
48
53
  has_rdoc: true
49
54
  homepage: http://github.com/dolores/redis_support
50
55
  licenses: []
@@ -55,23 +60,27 @@ rdoc_options:
55
60
  require_paths:
56
61
  - lib
57
62
  required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
58
64
  requirements:
59
65
  - - ">="
60
66
  - !ruby/object:Gem::Version
67
+ hash: 3
61
68
  segments:
62
69
  - 0
63
70
  version: "0"
64
71
  required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
65
73
  requirements:
66
74
  - - ">="
67
75
  - !ruby/object:Gem::Version
76
+ hash: 3
68
77
  segments:
69
78
  - 0
70
79
  version: "0"
71
80
  requirements: []
72
81
 
73
82
  rubyforge_project:
74
- rubygems_version: 1.3.6
83
+ rubygems_version: 1.3.7
75
84
  signing_key:
76
85
  specification_version: 3
77
86
  summary: A Redis Support module