Permalink
Browse files

Added redis adapter.

  • Loading branch information...
jnunemaker committed Aug 3, 2012
1 parent 07b17fd commit bf03bb1f76ca8e01b3cfd00e1a7b8b1a13d39df4
Showing with 226 additions and 0 deletions.
  1. +18 −0 .gitignore
  2. +18 −0 Gemfile
  3. +18 −0 Guardfile
  4. +22 −0 LICENSE
  5. +25 −0 README.md
  6. +2 −0 Rakefile
  7. +18 −0 flipper-redis.gemspec
  8. +1 −0 lib/flipper-redis.rb
  9. +36 −0 lib/flipper/adapters/redis.rb
  10. +7 −0 lib/flipper/adapters/redis/version.rb
  11. +36 −0 spec/flipper/redis_spec.rb
  12. +25 −0 spec/helper.rb
View
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+log
View
18 Gemfile
@@ -0,0 +1,18 @@
+source 'https://rubygems.org'
+gemspec
+
+gem 'rake'
+gem 'flipper', :path => '/Users/jnunemaker/projects/flipper'
+
+group(:guard) do
+ gem 'guard'
+ gem 'guard-rspec'
+ gem 'guard-bundler'
+ gem 'growl'
+end
+
+group(:test) do
+ gem 'rspec'
+ gem 'redis'
+end
+
View
@@ -0,0 +1,18 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'bundler' do
+ watch('Gemfile')
+ watch(/^.+\.gemspec/)
+end
+
+rspec_options = {
+ :all_after_pass => false,
+ :all_on_start => false,
+}
+
+guard 'rspec', rspec_options do
+ watch(%r{^spec/.+_spec\.rb$}) { "spec" }
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
+ watch('spec/helper.rb') { "spec" }
+end
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 John Nunemaker
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,25 @@
+# Flipper Redis
+
+A redis adapter for [Flipper](https://github.com/jnunemaker/flipper), the feature flipping gems.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'flipper-redis'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself with:
+
+ $ gem install flipper-redis
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
View
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/flipper/adapters/redis/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["John Nunemaker"]
+ gem.email = ["nunemaker@gmail.com"]
+ gem.description = %q{Redis adapter for Flipper}
+ gem.summary = %q{Redis adapter for Flipper}
+ gem.homepage = "http://jnunemaker.github.com/flipper-redis"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "flipper-redis"
+ gem.require_paths = ["lib"]
+ gem.version = Flipper::Adapters::Redis::VERSION
+ gem.add_dependency 'flipper', '~> 0.1.0'
+end
View
@@ -0,0 +1 @@
+require 'flipper/adapters/redis'
@@ -0,0 +1,36 @@
+require 'set'
+require 'redis'
+
+module Flipper
+ module Adapters
+ class Redis
+ def initialize(client)
+ @client = client
+ end
+
+ def read(key)
+ @client.get key
+ end
+
+ def write(key, value)
+ @client.set key, value
+ end
+
+ def delete(key)
+ @client.del key
+ end
+
+ def set_add(key, value)
+ @client.sadd(key, value)
+ end
+
+ def set_delete(key, value)
+ @client.srem(key, value)
+ end
+
+ def set_members(key)
+ @client.smembers(key).map { |member| member.to_i }.to_set
+ end
+ end
+ end
+end
@@ -0,0 +1,7 @@
+module Flipper
+ module Adapters
+ class Redis
+ VERSION = "0.1"
+ end
+ end
+end
View
@@ -0,0 +1,36 @@
+require 'helper'
+require 'flipper/adapters/redis'
+require 'flipper/spec/shared_adapter_specs'
+
+describe Flipper::Adapters::Redis do
+ let(:client) { Redis.new }
+
+ subject { Flipper::Adapters::Redis.new(client) }
+
+ before do
+ client.flushdb
+ end
+
+ def read_key(key)
+ client.get key
+ rescue RuntimeError => e
+ if e.message =~ /wrong kind of value/
+ client.smembers(key).map { |member| member.to_i }.to_set
+ else
+ raise
+ end
+ end
+
+ def write_key(key, value)
+ case value
+ when Array, Set
+ value.each do |member|
+ client.sadd key, member
+ end
+ else
+ client.set key, value
+ end
+ end
+
+ it_should_behave_like 'a flipper adapter'
+end
View
@@ -0,0 +1,25 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+
+require 'pathname'
+require 'logger'
+
+root_path = Pathname(__FILE__).dirname.join('..').expand_path
+lib_path = root_path.join('lib')
+log_path = root_path.join('log')
+log_path.mkpath
+
+require 'rubygems'
+require 'bundler'
+
+Bundler.require(:default, :test)
+
+require 'flipper-redis'
+
+Logger.new(log_path.join('test.log'))
+
+RSpec.configure do |config|
+ config.filter_run :focused => true
+ config.alias_example_to :fit, :focused => true
+ config.alias_example_to :xit, :pending => true
+ config.run_all_when_everything_filtered = true
+end

0 comments on commit bf03bb1

Please sign in to comment.