From 70c04b7ea8b7a06349eea54156045f1fb310ebf2 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Thu, 14 Aug 2014 17:43:42 +0200 Subject: [PATCH] Add Feature class and memory repository --- README.md | 43 ++++++++++++++++++++++++++++++++++- lib/flop.rb | 13 +++++++++-- lib/flop/feature.rb | 42 ++++++++++++++++++++++++++++++++++ lib/flop/repository/memory.rb | 19 ++++++++++++++++ 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 lib/flop/feature.rb create mode 100644 lib/flop/repository/memory.rb diff --git a/README.md b/README.md index cc197e4..4239333 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/flop.rb b/lib/flop.rb index 132dcfb..0178195 100644 --- a/lib/flop.rb +++ b/lib/flop.rb @@ -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 diff --git a/lib/flop/feature.rb b/lib/flop/feature.rb new file mode 100644 index 0000000..765ebad --- /dev/null +++ b/lib/flop/feature.rb @@ -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 diff --git a/lib/flop/repository/memory.rb b/lib/flop/repository/memory.rb new file mode 100644 index 0000000..bfd6c6c --- /dev/null +++ b/lib/flop/repository/memory.rb @@ -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