redised 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'redis', '~>2'
6
+ gem 'redis-namespace'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "shoulda", ">= 0"
12
+ gem "mocha"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.8.3"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.3)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.6.5)
11
+ metaclass (0.0.1)
12
+ mocha (0.10.3)
13
+ metaclass (~> 0.0.1)
14
+ rake (0.9.2.2)
15
+ rdoc (3.12)
16
+ json (~> 1.4)
17
+ redis (2.2.2)
18
+ redis-namespace (1.1.0)
19
+ redis (< 3.0.0)
20
+ shoulda (2.11.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.8.3)
28
+ mocha
29
+ redis (~> 2)
30
+ redis-namespace
31
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Aaron Quint, Paperless Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = redised
2
+
3
+ Redised provides a simple module for establishing reusable class level namespaced redis connections
4
+ in your vanilla Ruby classes.
5
+
6
+ == Why?
7
+
8
+ We use redis a lot throughout our apps. This pattern of having a class level `redis` method
9
+ thats already namespaced and connected to a specific server via a config has served us well.
10
+ It has allowed us to split different usages across different namespaces and eventually
11
+ different hosts/servers.
12
+
13
+ == Usage
14
+
15
+ You can include Redised as a module in your class.
16
+
17
+ class MyRedisClass
18
+ include Redised
19
+ end
20
+
21
+ This now gives you the power to assign the url of the redis server you want to connect to
22
+ as a string:
23
+
24
+ MyRedisClass.redis = 'localhost:6739:1/myredisclass' # hostname:port:db/namespace
25
+ # also accepts a Redis or Redis::Namespace object
26
+
27
+ This gets a little easier and more powerful when you setup a YAML config:
28
+
29
+ # in config/redis.yml
30
+ ---
31
+ mynamespace:
32
+ development: localhost:6379
33
+ production: redis01:6379/mynamespace
34
+ othernamespace:
35
+ development: localhost:6379
36
+ production: redis01:6379/othernamespace
37
+
38
+ You can tell redised where this config lives:
39
+
40
+ Redised.redised_config_path = File.join(Rails.root, 'config', 'redis.yml')
41
+
42
+ And what the 'env' is (will try to pull from RACK_ENV and RAILS_ENV):
43
+
44
+ Redised.redised_env = Rails.env #=> 'production'
45
+
46
+ Then in your class you tell it what namespace config this class points to:
47
+
48
+ class MyRedisClass
49
+ include Redised
50
+
51
+ redised_namespace 'mynamespace'
52
+
53
+ end
54
+
55
+ It will now automatically load the correct redis connection for your namespace/env:
56
+
57
+ MyRedisClass.redis #=> #<Redis::Namespace ..>
58
+ MyRedisClass.redis.client.host #=> 'redis01'
59
+ MyRedisClass.namespace #=> 'mynamespace'
60
+
61
+ The implementation of the `self.redis` method is very close to the `Resque.redis` method and is
62
+ actually a drop in replacement (if you want to load your connection settings from a config). Our
63
+ `config/initializers/resque.rb` looks like:
64
+
65
+ module Resque
66
+ include Redised
67
+ extend self
68
+
69
+ redised_namespace 'paperless'
70
+ end
71
+
72
+ == Acknowledgments
73
+
74
+ The original parsing of URL idea came from @defunkt/resque.
75
+
76
+ == Contributing to redised
77
+
78
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
79
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
80
+ * Fork the project.
81
+ * Start a feature/bugfix branch.
82
+ * Commit and push until you are happy with your contribution.
83
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
84
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
85
+
86
+ == Copyright
87
+
88
+ Copyright (c) 2012 Aaron Quint, Paperless Inc. See LICENSE.txt for further details.
89
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require './lib/redised'
15
+
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.name = "redised"
20
+ gem.version = Redised::VERSION
21
+ gem.homepage = "http://github.com/paperlesspost/redised"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{a simple module that allows you to setup your class or module with a redis connection}
24
+ gem.description = %Q{redised provides a single module `Redised` that when included in your class provides a self.redis= and self.redis methods. It also has the ability to load the urls from a YAML config file}
25
+ gem.email = "aaron@quirkey.com"
26
+ gem.authors = ["Aaron Quint"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :test
39
+
40
+ require 'rdoc/task'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "redised #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/lib/redised.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'redis/namespace'
2
+ # Redised allows for the common patter of module access to redis, when included
3
+ # a .redis and .redis= method are provided
4
+ module Redised
5
+ VERSION = '0.1.0'
6
+
7
+ # Get a reusable connection based on a set of params. The
8
+ # params are the same as the options you pass to `Redis.new`
9
+ #
10
+ # This ensures that an app doesnt try to open multiple connections
11
+ # to the same redis server.
12
+ def self.redis_connection(params)
13
+ @_redis_connections ||= {}
14
+ @_redis_connections[params] ||= Redis.new(params)
15
+ end
16
+
17
+ # Load/parse the YAML config setup at `redised_config_path`.
18
+ # If no config is setup, returns nil
19
+ #
20
+ # Configs are in the format:
21
+ #
22
+ # ---
23
+ # namespace:
24
+ # env: url
25
+ #
26
+ def self.redised_config
27
+ if @_redised_config_path
28
+ @_redised_config ||= YAML.load_file(@_redised_config_path)
29
+ end
30
+ end
31
+
32
+ # Return the config path for the YAML config.
33
+ def self.redised_config_path
34
+ @_redised_config_path
35
+ end
36
+
37
+ # Set the config path to load from.
38
+ def self.redised_config_path=(new_path)
39
+ @_redised_config_path = new_path
40
+ @_redised_config = nil
41
+ end
42
+
43
+ def self.redised_env
44
+ @_redised_env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || nil
45
+ end
46
+
47
+ def self.redised_env=(new_env)
48
+ @_redised_env = new_env
49
+ @_redised_config = nil
50
+ end
51
+
52
+ def self.included(klass)
53
+
54
+ klass.module_eval do
55
+
56
+ # Accepts:
57
+ # 1. A 'hostname:port' string
58
+ # 2. A 'hostname:port:db' string (to select the Redis db)
59
+ # 3. A 'hostname:port/namespace' string (to set the Redis namespace)
60
+ # 4. A redis URL string 'redis://host:port'
61
+ # 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
62
+ # or `Redis::Namespace`.
63
+ def self.redis=(server)
64
+ if server.respond_to? :split
65
+
66
+ if server =~ /redis\:\/\//
67
+ conn = ::Redised.redis_connection(:url => server)
68
+ else
69
+ server, namespace = server.split('/', 2)
70
+ host, port, db = server.split(':')
71
+ conn = ::Redised.redis_connection(:host => host, :port => port,
72
+ :thread_safe => true, :db => db)
73
+ end
74
+
75
+ @_redis = namespace ? Redis::Namespace.new(namespace, :redis => conn) : conn
76
+ else
77
+ @_redis = server
78
+ end
79
+ end
80
+
81
+ def self.redised_namespace(new_name = nil)
82
+ if new_name
83
+ @_namespace = new_name
84
+ @_redis = nil
85
+ else
86
+ @_namespace
87
+ end
88
+ end
89
+
90
+ # Returns the current Redis connection. If none has been created, will
91
+ # create a new one.
92
+ def self.redis
93
+ return @_redis if @_redis
94
+ if ::Redised.redised_config
95
+ self.redis = if redised_namespace
96
+ ::Redised.redised_config[redised_namespace][::Redised.redised_env]
97
+ else
98
+ ::Redised.redised_config[::Redis.redised_env]
99
+ end
100
+ else
101
+ self.redis = 'localhost:6379'
102
+ end
103
+ @_redis
104
+ rescue NoMethodError => e
105
+ raise("There was a problem setting up your redis for redised_namespace #{redised_namespace} (from file #{@_redised_config_path}): #{e}")
106
+ end
107
+
108
+ def redis
109
+ self.class.redis
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ end
data/redised.gemspec ADDED
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "redised"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Aaron Quint"]
12
+ s.date = "2012-02-06"
13
+ s.description = "redised provides a single module `Redised` that when included in your class provides a self.redis= and self.redis methods. It also has the ability to load the urls from a YAML config file"
14
+ s.email = "aaron@quirkey.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "lib/redised.rb",
27
+ "redised.gemspec",
28
+ "test/env_redised_config.yml",
29
+ "test/helper.rb",
30
+ "test/test_redised.rb"
31
+ ]
32
+ s.homepage = "http://github.com/paperlesspost/redised"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "a simple module that allows you to setup your class or module with a redis connection"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<redis>, ["~> 2"])
43
+ s.add_runtime_dependency(%q<redis-namespace>, [">= 0"])
44
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
45
+ s.add_development_dependency(%q<mocha>, [">= 0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
48
+ else
49
+ s.add_dependency(%q<redis>, ["~> 2"])
50
+ s.add_dependency(%q<redis-namespace>, [">= 0"])
51
+ s.add_dependency(%q<shoulda>, [">= 0"])
52
+ s.add_dependency(%q<mocha>, [">= 0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<redis>, ["~> 2"])
58
+ s.add_dependency(%q<redis-namespace>, [">= 0"])
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<mocha>, [">= 0"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,3 @@
1
+ ---
2
+ mynamespace:
3
+ production: localhost:6739/mynamespace
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ require 'redised'
17
+
18
+ class Test::Unit::TestCase
19
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ class TestRedised < Test::Unit::TestCase
4
+
5
+ class RedisedClass
6
+ include Redised
7
+
8
+ redised_namespace 'mynamespace'
9
+ end
10
+
11
+ class OtherRedisedClass
12
+ include Redised
13
+ end
14
+
15
+ context "Redised" do
16
+ setup do
17
+ @env_config_path = File.join(File.dirname(__FILE__), 'env_redised_config.yml')
18
+ end
19
+
20
+ should "be able to assign the redised_config_path" do
21
+ Redised.redised_config_path = @env_config_path
22
+ assert_equal @env_config_path, Redised.redised_config_path
23
+ assert Redised.redised_config['mynamespace']
24
+ end
25
+
26
+ should "pull default env from ENV" do
27
+ ENV['RAILS_ENV'] = 'production'
28
+ assert_equal 'production', Redised.redised_env
29
+ end
30
+
31
+ should "allow for setting the env" do
32
+ Redised.redised_env = 'dev'
33
+ assert_equal 'dev', Redised.redised_env
34
+ end
35
+
36
+ should "have class level redis connection" do
37
+ Redised.redised_config_path = @env_config_path
38
+ Redised.redised_env = 'production'
39
+ redis = RedisedClass.redis
40
+ assert_kind_of Redis::Namespace, redis
41
+ end
42
+
43
+ should "parse redis connection with namespace" do
44
+ RedisedClass.redis = 'localhost:5678:0/namespace'
45
+ redis = RedisedClass.redis
46
+ assert_equal 'localhost', redis.client.host
47
+ assert_equal 5678, redis.client.port
48
+ assert_equal 0, redis.client.db
49
+ assert_equal 'namespace', redis.namespace
50
+ end
51
+
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redised
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Quint
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &70189510863680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70189510863680
25
+ - !ruby/object:Gem::Dependency
26
+ name: redis-namespace
27
+ requirement: &70189510862940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70189510862940
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &70189510862180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70189510862180
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: &70189510861600 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70189510861600
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &70189510860720 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.0.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70189510860720
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &70189510858840 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70189510858840
80
+ description: redised provides a single module `Redised` that when included in your
81
+ class provides a self.redis= and self.redis methods. It also has the ability to
82
+ load the urls from a YAML config file
83
+ email: aaron@quirkey.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files:
87
+ - LICENSE.txt
88
+ - README.rdoc
89
+ files:
90
+ - .document
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - lib/redised.rb
97
+ - redised.gemspec
98
+ - test/env_redised_config.yml
99
+ - test/helper.rb
100
+ - test/test_redised.rb
101
+ homepage: http://github.com/paperlesspost/redised
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 421598399054906059
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.10
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: a simple module that allows you to setup your class or module with a redis
129
+ connection
130
+ test_files: []