Add Redis repository
parent
1d2ffcb691
commit
e4b4063822
|
@ -24,10 +24,15 @@ First, you need to configure where to store everything by setting a repository.
|
||||||
|
|
||||||
Available repositories are :
|
Available repositories are :
|
||||||
- `Flop::Repository::Memory`
|
- `Flop::Repository::Memory`
|
||||||
|
- `Flop::Repository::Redis`
|
||||||
|
|
||||||
To set the repository, create a new object and affect it to `Flop.repo`.
|
To set the repository, create a new object and affect it to `Flop.repo`.
|
||||||
```ruby
|
```ruby
|
||||||
Flop.repo = Flop::Repository::Memory.new
|
Flop.repo = Flop::Repository::Memory.new
|
||||||
|
|
||||||
|
require 'redis'
|
||||||
|
|
||||||
|
Flop.repo = Flop::Repository::Redis.new(Redis.new)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'flop/version'
|
||||||
|
|
||||||
require 'flop/feature'
|
require 'flop/feature'
|
||||||
require 'flop/repository/memory'
|
require 'flop/repository/memory'
|
||||||
|
require 'flop/repository/redis'
|
||||||
|
|
||||||
module Flop
|
module Flop
|
||||||
class << self
|
class << self
|
||||||
|
|
|
@ -17,6 +17,7 @@ module Flop
|
||||||
|
|
||||||
def set(value)
|
def set(value)
|
||||||
repo.set(name, value)
|
repo.set(name, value)
|
||||||
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def activate
|
def activate
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue