diff --git a/README.md b/README.md index ea5b3e5..67fa47e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/flop.rb b/lib/flop.rb index 7601d76..0fe36eb 100644 --- a/lib/flop.rb +++ b/lib/flop.rb @@ -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 diff --git a/lib/flop/repository/yaml.rb b/lib/flop/repository/yaml.rb new file mode 100644 index 0000000..20f924a --- /dev/null +++ b/lib/flop/repository/yaml.rb @@ -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 + diff --git a/test/fixtures/features.yml b/test/fixtures/features.yml new file mode 100644 index 0000000..1fd10e2 --- /dev/null +++ b/test/fixtures/features.yml @@ -0,0 +1,6 @@ +--- +flop: + blog: true + wiki: false +custom_namespace: + chat: true diff --git a/test/flop/repository/yaml_test.rb b/test/flop/repository/yaml_test.rb new file mode 100644 index 0000000..bf25f93 --- /dev/null +++ b/test/flop/repository/yaml_test.rb @@ -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