Add Redis repository

master
Guillaume Dott 2014-08-14 18:00:15 +02:00
parent 1d2ffcb691
commit e4b4063822
4 changed files with 31 additions and 0 deletions

View File

@ -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

View File

@ -2,6 +2,7 @@ require 'flop/version'
require 'flop/feature'
require 'flop/repository/memory'
require 'flop/repository/redis'
module Flop
class << self

View File

@ -17,6 +17,7 @@ module Flop
def set(value)
repo.set(name, value)
value
end
def activate

View File

@ -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