Add Feature class and memory repository

master
Guillaume Dott 2014-08-14 17:43:42 +02:00
parent 7cd77196ca
commit 70c04b7ea8
4 changed files with 114 additions and 3 deletions

View File

@ -18,7 +18,48 @@ Or install it yourself as:
## Usage
TODO: Write usage instructions here
### Repositories
First, you need to configure where to store everything by setting a repository.
Available repositories are :
- `Flop::Repository::Memory`
To set the repository, create a new object and affect it to `Flop.repo`.
```ruby
Flop.repo = Flop::Repository::Memory.new
```
### Features
To access a feature, you can call the Flop[] method or create a new `Flop::Feature` object.
```ruby
Flop[:example_feature] # is the same as
Flop::Feature.new(:example_feature)
```
Available methods on `Flop::Feature` are :
- `feature.active?` returns true if the feature is active
- `feature.inactive?` returns true if the feature is inactive
- `feature.set(boolean)` sets the state of the feature to the passed value
- `feature.activate` activates the feature
- `feature.deactivate` deactivates the feature
- `feature.toggle` toggles the state of the feature
- `feature.with { }` executes the block if the feature is active
- `feature.without { }` executes the block if the feature is inactive
## Examples
```ruby
Flop.repo = Flop::Repository::Memory.new
Flop[:feature].active? # => false
Flop[:feature].toggle # => true
Flop[:feature].with do
# code
end
```
## Contributing

View File

@ -1,5 +1,14 @@
require "flop/version"
require 'flop/version'
require 'flop/feature'
require 'flop/repository/memory'
module Flop
# Your code goes here...
class << self
attr_accessor :repo
def [](feature)
Flop::Feature.new(feature)
end
end
end

View File

@ -0,0 +1,42 @@
module Flop
class Feature
attr_accessor :name, :repo
def initialize(name, repo = Flop.repo)
self.name = name.to_sym
self.repo = repo
end
def active?
repo.get(name)
end
def inactive?
!active?
end
def set(value)
repo.set(name, value)
end
def activate
set true
end
def deactivate
set false
end
def toggle
set !active?
end
def with(&block)
yield block if active?
end
def without(&block)
yield block if inactive?
end
end
end

View File

@ -0,0 +1,19 @@
module Flop
module Repository
class Memory
attr_accessor :features
def initialize
self.features = Hash.new(false)
end
def get(name)
features[name]
end
def set(name, value)
features[name] = value
end
end
end
end