From e4b40638223101ada06c78f574ff2d9399793e22 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Thu, 14 Aug 2014 18:00:15 +0200 Subject: [PATCH] Add Redis repository --- README.md | 5 +++++ lib/flop.rb | 1 + lib/flop/feature.rb | 1 + lib/flop/repository/redis.rb | 24 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 lib/flop/repository/redis.rb diff --git a/README.md b/README.md index 4239333..79a2d12 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,15 @@ First, you need to configure where to store everything by setting a repository. Available repositories are : - `Flop::Repository::Memory` +- `Flop::Repository::Redis` To set the repository, create a new object and affect it to `Flop.repo`. ```ruby Flop.repo = Flop::Repository::Memory.new + +require 'redis' + +Flop.repo = Flop::Repository::Redis.new(Redis.new) ``` ### Features diff --git a/lib/flop.rb b/lib/flop.rb index 0178195..955939b 100644 --- a/lib/flop.rb +++ b/lib/flop.rb @@ -2,6 +2,7 @@ require 'flop/version' require 'flop/feature' require 'flop/repository/memory' +require 'flop/repository/redis' module Flop class << self diff --git a/lib/flop/feature.rb b/lib/flop/feature.rb index 765ebad..223718e 100644 --- a/lib/flop/feature.rb +++ b/lib/flop/feature.rb @@ -17,6 +17,7 @@ module Flop def set(value) repo.set(name, value) + value end def activate diff --git a/lib/flop/repository/redis.rb b/lib/flop/repository/redis.rb new file mode 100644 index 0000000..6f0d253 --- /dev/null +++ b/lib/flop/repository/redis.rb @@ -0,0 +1,24 @@ +module Flop + module Repository + class Redis + def initialize(redis, namespace = '') + @redis = redis + @namespace = namespace + end + + def get(name) + @redis.get(key(name)).to_i == 1 + end + + def set(name, value) + @redis.set(key(name), value ? 1 : 0) + end + + private + + def key(name) + [@namespace, name].join(':') + end + end + end +end