Add Yaml repository

master
Guillaume Dott 2014-09-01 17:50:35 +02:00
parent 30d1958e46
commit bb89807477
5 changed files with 59 additions and 0 deletions

View File

@ -26,6 +26,7 @@ First, you need to configure where to store everything by setting a repository.
Available repositories are :
- `Flop::Repository::Memory`
- `Flop::Repository::Redis`
- `Flop::Repository::Yaml`
To set the repository, create a new object and affect it to `Flop.repo`.
```ruby
@ -49,6 +50,15 @@ Flop::Repository::Redis.new(Redis.new, [:flop, :namespace])
Flop::Repository::Redis.new(Redis.new, -> { ENV['NAMESPACE'] })
```
### YAML
The `Yaml` repository requires a yaml file as its first parameter.
This repository is read-only so methods like `activate`, `toggle`, ... can not be used.
```ruby
Flop::Repository::Yaml.new('my_file.yml')
```
## Features
To access a feature, you can call the Flop[] method or create a new `Flop::Feature` object.

View File

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

View File

@ -0,0 +1,20 @@
require 'yaml'
module Flop
class Repository
class Yaml < Flop::Repository
attr_reader :yaml, :namespace
def initialize(yaml_file, namespace = 'flop')
@yaml = YAML.load_file(yaml_file)
@namespace = namespace
end
def get(name)
name = name.to_s
yaml[namespace].key?(name) ? yaml[namespace][name] : false
end
end
end
end

6
test/fixtures/features.yml vendored 100644
View File

@ -0,0 +1,6 @@
---
flop:
blog: true
wiki: false
custom_namespace:
chat: true

View File

@ -0,0 +1,22 @@
require 'test_helper'
class Flop::Repository::YamlTest < Minitest::Test
def setup
@repo = Flop::Repository::Yaml.new('test/fixtures/features.yml')
end
def test_set_raises_exception
assert_raises(Flop::Repository::SetNotImplemented) do
@repo.set(:test, true)
end
end
def test_default_value_is_false
refute @repo.get(:unknown_key), 'default value is not false'
end
def test_get_value_with_default_namespace
assert_equal true, @repo.get(:blog)
assert_equal false, @repo.get(:wiki)
end
end