hella-redis 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  *.gem
2
2
  *.log
3
3
  *.rbc
4
+ .rbx/
4
5
  .bundle
5
6
  .config
6
7
  .yardoc
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  gem 'rake'
6
+ gem 'pry', "~> 0.9.0"
data/README.md CHANGED
@@ -4,18 +4,18 @@ It's-a hella-redis!
4
4
 
5
5
  ## Usage
6
6
 
7
- ### RedisConnection
7
+ ### Connection
8
8
 
9
9
  ```ruby
10
10
  # config and create a connection
11
- @config = OpenStruct.new({
11
+ @config = {
12
12
  :timeout => 1,
13
13
  :size => 5,
14
14
  :redis_ns => 'hella-redis-test',
15
15
  :driver => 'ruby',
16
16
  :url => 'redis://localhost:6379/0'
17
- })
18
- @conn = HellaRedis::RedisConnection.new(@config)
17
+ }
18
+ @conn = HellaRedis::Connection.new(@config)
19
19
 
20
20
  # it's actually a pool of connections
21
21
  @conn.with do |conn|
data/hella-redis.gemspec CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_development_dependency("assert")
21
21
 
22
- gem.add_dependency("redis", ["~> 3.0"])
23
- gem.add_dependency("redis-namespace", ["~> 1.0"])
22
+ gem.add_dependency("redis", ["~> 3.2"])
23
+ gem.add_dependency("redis-namespace", ["~> 1.5"])
24
24
  gem.add_dependency("connection_pool", ["= 0.9.2"]) # temp, for 1.8.7 support
25
25
 
26
26
  end
data/lib/hella-redis.rb CHANGED
@@ -1,5 +1,5 @@
1
- require "hella-redis/version"
2
- require "hella-redis/redis_connection"
1
+ require 'hella-redis/version'
2
+ require 'hella-redis/connection'
3
3
 
4
4
  module HellaRedis
5
5
  end
@@ -0,0 +1,35 @@
1
+ require 'connection_pool'
2
+ require 'redis'
3
+ require 'redis-namespace'
4
+
5
+ module HellaRedis
6
+
7
+ module Connection
8
+
9
+ def self.new(config)
10
+ config = Config.new(config) if config.kind_of?(::Hash)
11
+ ::ConnectionPool.new(:timeout => config.timeout, :size => config.size) do
12
+ ::Redis::Namespace.new(config.redis_ns, {
13
+ :redis => ::Redis.new({
14
+ :url => config.url,
15
+ :driver => config.driver
16
+ })
17
+ })
18
+ end
19
+ end
20
+
21
+ class Config
22
+ attr_reader :url, :driver, :redis_ns, :timeout, :size
23
+
24
+ def initialize(args)
25
+ @url = args[:url]
26
+ @driver = args[:driver]
27
+ @redis_ns = args[:ns]
28
+ @timeout = args[:timeout]
29
+ @size = args[:size] || 1
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,58 @@
1
+ require 'redis'
2
+ require 'hella-redis/connection'
3
+
4
+ module HellaRedis
5
+
6
+ class ConnectionSpy
7
+
8
+ attr_reader :config, :redis_spy, :with_calls
9
+
10
+ def initialize(config, redis_spy = nil)
11
+ @config = config
12
+ @with_calls = []
13
+ @redis_spy = redis_spy || RedisSpy.new(config)
14
+ end
15
+
16
+ def with(*args, &block)
17
+ @with_calls << WithCall.new(args, block)
18
+ block.call(@redis_spy)
19
+ end
20
+
21
+ def redis_calls
22
+ @redis_spy.calls
23
+ end
24
+
25
+ end
26
+
27
+ class RedisSpy
28
+
29
+ attr_reader :calls
30
+
31
+ def initialize(config)
32
+ # mimic the real conection behavior, accept hash or object
33
+ config = Connection::Config.new(config) if config.kind_of?(::Hash)
34
+ @instance = ::Redis.new({
35
+ :url => config.url,
36
+ :driver => config.driver
37
+ })
38
+ @calls = []
39
+ end
40
+
41
+ def method_missing(name, *args, &block)
42
+ if self.respond_to?(name)
43
+ @calls << RedisCall.new(name, args, block)
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ def respond_to?(*args)
50
+ super || @instance.respond_to?(*args)
51
+ end
52
+
53
+ end
54
+
55
+ WithCall = Struct.new(:args, :block)
56
+ RedisCall = Struct.new(:command, :args, :block)
57
+
58
+ end
@@ -1,3 +1,3 @@
1
1
  module HellaRedis
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/test/helper.rb CHANGED
@@ -3,3 +3,8 @@
3
3
 
4
4
  # add the root dir to the load path
5
5
  $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
6
+
7
+ # require pry for debugging (`binding.pry`)
8
+ require 'pry'
9
+
10
+ require 'test/support/factory'
@@ -0,0 +1,6 @@
1
+ require 'assert/factory'
2
+
3
+ module Factory
4
+ extend Assert::Factory
5
+
6
+ end
@@ -0,0 +1,100 @@
1
+ require 'assert'
2
+ require 'hella-redis/connection_spy'
3
+
4
+ class HellaRedis::ConnectionSpy
5
+
6
+ class UnitTests < Assert::Context
7
+ setup do
8
+ @config = {
9
+ :url => 'redis://localhost:6379/0',
10
+ :driver => 'ruby'
11
+ }
12
+ end
13
+
14
+ end
15
+
16
+ class ConnectionSpyTests < UnitTests
17
+ desc "HellaRedis::ConnectionSpy"
18
+ setup do
19
+ @connection_spy = HellaRedis::ConnectionSpy.new(@config)
20
+ end
21
+ subject{ @connection_spy }
22
+
23
+ should have_readers :config, :redis_spy, :with_calls
24
+ should have_imeths :with, :redis_calls
25
+
26
+ should "know its config and redis spy" do
27
+ assert_equal @config, subject.config
28
+ assert_instance_of HellaRedis::RedisSpy, subject.redis_spy
29
+ end
30
+
31
+ should "default its with calls" do
32
+ assert_equal [], subject.with_calls
33
+ end
34
+
35
+ should "know its redis spy's calls" do
36
+ assert_same subject.redis_spy.calls, subject.redis_calls
37
+ end
38
+
39
+ should "yield its redis spy using `with`" do
40
+ yielded = nil
41
+ subject.with{ |c| yielded = c }
42
+ assert_same subject.redis_spy, yielded
43
+ end
44
+
45
+ should "track calls to with" do
46
+ overrides = { :timeout => Factory.integer }
47
+ block = proc{ |c| Factory.string }
48
+ subject.with(overrides, &block)
49
+ call = subject.with_calls.last
50
+ assert_equal [overrides], call.args
51
+ assert_equal block, call.block
52
+ end
53
+
54
+ should "allow passing a custom redis spy" do
55
+ redis_spy = Factory.string
56
+ spy = HellaRedis::ConnectionSpy.new(@config, redis_spy)
57
+ assert_equal redis_spy, spy.redis_spy
58
+ end
59
+
60
+ end
61
+
62
+ class RedisSpyTests < UnitTests
63
+ desc "HellaRedis::RedisSpy"
64
+ setup do
65
+ @redis_spy = HellaRedis::RedisSpy.new(@config)
66
+ end
67
+ subject{ @redis_spy }
68
+
69
+ should have_readers :calls
70
+
71
+ should "default its calls" do
72
+ assert_equal [], subject.calls
73
+ end
74
+
75
+ should "allow passing a config object" do
76
+ config = OpenStruct.new(@config)
77
+ assert_nothing_raised{ HellaRedis::RedisSpy.new(config) }
78
+ end
79
+
80
+ should "track redis calls made to it" do
81
+ assert_true subject.respond_to?(:set)
82
+
83
+ key, value = [Factory.string, Factory.string]
84
+ subject.set(key, value)
85
+
86
+ call = subject.calls.first
87
+ assert_equal :set, call.command
88
+ assert_equal [key, value], call.args
89
+ end
90
+
91
+ should "raise no method errors for non-redis methods" do
92
+ assert_false subject.respond_to?(:super_awesome_set)
93
+ assert_raises(NoMethodError) do
94
+ subject.super_awesome_set(Factory.string, Factory.string)
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,75 @@
1
+ require 'assert'
2
+ require 'hella-redis/connection'
3
+
4
+ require 'connection_pool'
5
+ require 'ostruct'
6
+
7
+ module HellaRedis::Connection
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "HellaRedis::Connection"
11
+ setup do
12
+ @config_hash = {
13
+ :timeout => 1,
14
+ :size => 5,
15
+ :redis_ns => 'hella-redis-test',
16
+ :driver => 'ruby',
17
+ :url => 'redis://localhost:6379/0'
18
+ }
19
+ @conn = HellaRedis::Connection.new(@config_hash)
20
+ end
21
+ subject{ @conn }
22
+
23
+ should "build a connection pool" do
24
+ assert_instance_of ::ConnectionPool, subject
25
+ end
26
+
27
+ should "connect to the redis instance that was provided" do
28
+ assert_nothing_raised do
29
+ subject.with{ |c| c.info }
30
+ end
31
+ end
32
+
33
+ should "build a redis namespace and yield it using `with`" do
34
+ subject.with do |conn|
35
+ assert_instance_of ::Redis::Namespace, conn
36
+ end
37
+ end
38
+
39
+ should "allow passing a config object when building a connection" do
40
+ conn = nil
41
+ assert_nothing_raised do
42
+ config = OpenStruct.new(@config_hash)
43
+ conn = HellaRedis::Connection.new(config)
44
+ end
45
+ assert_instance_of ::ConnectionPool, conn
46
+ end
47
+
48
+ end
49
+
50
+ class ConfigTests < UnitTests
51
+ desc "Config"
52
+ setup do
53
+ @config = Config.new(@config_hash)
54
+ end
55
+ subject{ @config }
56
+
57
+ should have_readers :url, :driver, :redis_ns, :timeout, :size
58
+
59
+ should "know its attributes" do
60
+ assert_equal @config_hash[:url], subject.url
61
+ assert_equal @config_hash[:driver], subject.driver
62
+ assert_equal @config_hash[:ns], subject.redis_ns
63
+ assert_equal @config_hash[:timeout], subject.timeout
64
+ assert_equal @config_hash[:size], subject.size
65
+ end
66
+
67
+ should "default its size" do
68
+ @config_hash.delete(:size)
69
+ config = Config.new(@config_hash)
70
+ assert_equal 1, config.size
71
+ end
72
+
73
+ end
74
+
75
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hella-redis
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,12 +16,10 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-03-02 00:00:00 Z
19
+ date: 2014-12-30 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- type: :development
23
- name: assert
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ requirement: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
24
  requirements:
27
25
  - - ">="
@@ -30,42 +28,42 @@ dependencies:
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
33
- requirement: *id001
31
+ version_requirements: *id001
32
+ type: :development
33
+ name: assert
34
34
  prerelease: false
35
35
  - !ruby/object:Gem::Dependency
36
- type: :runtime
37
- name: redis
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ requirement: &id002 !ruby/object:Gem::Requirement
39
37
  none: false
40
38
  requirements:
41
39
  - - ~>
42
40
  - !ruby/object:Gem::Version
43
- hash: 7
41
+ hash: 3
44
42
  segments:
45
43
  - 3
46
- - 0
47
- version: "3.0"
48
- requirement: *id002
44
+ - 2
45
+ version: "3.2"
46
+ version_requirements: *id002
47
+ type: :runtime
48
+ name: redis
49
49
  prerelease: false
50
50
  - !ruby/object:Gem::Dependency
51
- type: :runtime
52
- name: redis-namespace
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
51
+ requirement: &id003 !ruby/object:Gem::Requirement
54
52
  none: false
55
53
  requirements:
56
54
  - - ~>
57
55
  - !ruby/object:Gem::Version
58
- hash: 15
56
+ hash: 5
59
57
  segments:
60
58
  - 1
61
- - 0
62
- version: "1.0"
63
- requirement: *id003
59
+ - 5
60
+ version: "1.5"
61
+ version_requirements: *id003
62
+ type: :runtime
63
+ name: redis-namespace
64
64
  prerelease: false
65
65
  - !ruby/object:Gem::Dependency
66
- type: :runtime
67
- name: connection_pool
68
- version_requirements: &id004 !ruby/object:Gem::Requirement
66
+ requirement: &id004 !ruby/object:Gem::Requirement
69
67
  none: false
70
68
  requirements:
71
69
  - - "="
@@ -76,7 +74,9 @@ dependencies:
76
74
  - 9
77
75
  - 2
78
76
  version: 0.9.2
79
- requirement: *id004
77
+ version_requirements: *id004
78
+ type: :runtime
79
+ name: connection_pool
80
80
  prerelease: false
81
81
  description: It's-a hella-redis!
82
82
  email:
@@ -96,11 +96,14 @@ files:
96
96
  - Rakefile
97
97
  - hella-redis.gemspec
98
98
  - lib/hella-redis.rb
99
- - lib/hella-redis/redis_connection.rb
99
+ - lib/hella-redis/connection.rb
100
+ - lib/hella-redis/connection_spy.rb
100
101
  - lib/hella-redis/version.rb
101
102
  - log/.gitkeep
102
103
  - test/helper.rb
103
- - test/unit/redis_connection_tests.rb
104
+ - test/support/factory.rb
105
+ - test/unit/connection_spy_tests.rb
106
+ - test/unit/connection_tests.rb
104
107
  - tmp/.gitkeep
105
108
  homepage: http://github.com/redding/hella-redis
106
109
  licenses: []
@@ -131,10 +134,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
134
  requirements: []
132
135
 
133
136
  rubyforge_project:
134
- rubygems_version: 1.8.25
137
+ rubygems_version: 1.8.29
135
138
  signing_key:
136
139
  specification_version: 3
137
140
  summary: It's-a hella-redis!
138
141
  test_files:
139
142
  - test/helper.rb
140
- - test/unit/redis_connection_tests.rb
143
+ - test/support/factory.rb
144
+ - test/unit/connection_spy_tests.rb
145
+ - test/unit/connection_tests.rb
@@ -1,19 +0,0 @@
1
- require 'redis'
2
- require 'redis-namespace'
3
- require 'connection_pool'
4
- require 'hella-redis/version'
5
-
6
- module HellaRedis::RedisConnection
7
-
8
- def self.new(config)
9
- @pool = ::ConnectionPool.new(:timeout => config.timeout, :size => config.size) do
10
- ::Redis::Namespace.new(config.redis_ns, {
11
- :redis => ::Redis.connect({
12
- :url => config.url,
13
- :driver => config.driver
14
- })
15
- })
16
- end
17
- end
18
-
19
- end
@@ -1,36 +0,0 @@
1
- require 'assert'
2
- require 'hella-redis/redis_connection'
3
- require 'connection_pool'
4
- require 'ostruct'
5
-
6
- module HellaRedis::RedisConnection
7
-
8
- class BaseTests < Assert::Context
9
- desc "a RedisConnection"
10
- setup do
11
- @config = OpenStruct.new({
12
- :timeout => 1,
13
- :size => 5,
14
- :redis_ns => 'hella-redis-test',
15
- :driver => 'ruby',
16
- :url => 'redis://localhost:6379/0'
17
- })
18
- @conn = HellaRedis::RedisConnection.new(@config)
19
- end
20
- subject{ @conn }
21
-
22
- should "be a connection pool with the configured size and timeout" do
23
- assert_kind_of ConnectionPool, subject
24
- end
25
-
26
- should "connect to the redis url" do
27
- assert_nothing_raised do
28
- subject.with do |conn|
29
- assert_kind_of Redis::Namespace, conn
30
- end
31
- end
32
- end
33
-
34
- end
35
-
36
- end