Add Yaml repository
parent
30d1958e46
commit
bb89807477
10
README.md
10
README.md
|
@ -26,6 +26,7 @@ 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`
|
- `Flop::Repository::Redis`
|
||||||
|
- `Flop::Repository::Yaml`
|
||||||
|
|
||||||
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
|
||||||
|
@ -49,6 +50,15 @@ Flop::Repository::Redis.new(Redis.new, [:flop, :namespace])
|
||||||
Flop::Repository::Redis.new(Redis.new, -> { ENV['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
|
## Features
|
||||||
|
|
||||||
To access a feature, you can call the Flop[] method or create a new `Flop::Feature` object.
|
To access a feature, you can call the Flop[] method or create a new `Flop::Feature` object.
|
||||||
|
|
|
@ -5,6 +5,7 @@ require 'flop/feature'
|
||||||
require 'flop/repository'
|
require 'flop/repository'
|
||||||
require 'flop/repository/memory'
|
require 'flop/repository/memory'
|
||||||
require 'flop/repository/redis'
|
require 'flop/repository/redis'
|
||||||
|
require 'flop/repository/yaml'
|
||||||
|
|
||||||
module Flop
|
module Flop
|
||||||
class << self
|
class << self
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
flop:
|
||||||
|
blog: true
|
||||||
|
wiki: false
|
||||||
|
custom_namespace:
|
||||||
|
chat: true
|
|
@ -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
|
Loading…
Reference in New Issue