Giter Site home page Giter Site logo

binflip's Introduction

Binflip

Simple environmental feature toggling (that works well with Rollout)

Description

Keeping true to twelve-factor application principles http://12factor.net, this simple library uses environment variables, with a straightforward naming convention, to specify feature toggles.

Rollout Compatibility

If Rollout https://github.com/jamesgolick/rollout is present, it will pass through most rollout methods except active?, where a preliminary test is done to see if the environment toggle is active first.

Rationale

When using a continuous delivery process, it is important to try to get all code integrated into the mainline as soon as possible. Feature toggles are favored over feature branches (http://www.infoq.com/interviews/jez-humble-martin-fowler-cd, http://martinfowler.com/bliki/FeatureToggle.html, http://fournines.wordpress.com/2011/11/20/feature-branches-vs-feature-toggles/, and http://blog.jayfields.com/2010/10/experience-report-feature-toggle-over.html) during the dev process (to guard against incomplete features being deployed to production).

Twelve-factor tells us to keep app configuration setting in environment variables. Hence this library adds a tiny bit of convention on how feature toggle env vars are spelled.

As the ultimate acceptance of a feature is the market response, this library has been designed to work with rollout.

Installation

Add this line to your application's Gemfile:

gem 'binflip'

And then execute:

$ bundle

Or install it yourself as:

$ gem install binflip

Usage

Set an environment variable following a FEATURE_[name] pattern:

Procfile

FEATURE_CHAT = 1
FEATURE_UPLOAD_VIDEO = 1

(Or set the ENV vars manually)

Then in application setup / initialization:

$toggle = Binflip.new

Check for toggles, using just the [name]:

$toggle.active?(:chat)
$toggle.active?(:upload_video)

NOTE: Absence of feature toggle env variable means the feature is not active.

binflip.js

To use binflip.js, you need to include vendor/assets/javascripts/binflip.js, and have JSON.parse available (use https://github.com/douglascrockford/JSON-js/blob/master/json2.js if you are using an old browser). If you are using Rails 3.1 or higher, including the Binflip gem will make it available, just require 'binflip' in your application.js manifest.

The javascript pattern is to build the feature set (for the current user) once during page load, and store the set of features as a data attribute in the <body> tag.

So in your <body> tag, do something like this:

<body data-features='<%= $binflip.active_features(current_user).to_json %>'>

Then you can test for features in your javascript with:

if (Binflip.isActive('feature')) {
  alert("Feature is active");
}

Usage with Rollout

Procfile

FEATURE_CHAT = 1
FEATURE_UPLOAD_VIDEO = 1

(Or set the ENV vars manually)

Then in application setup / initialization:

$redis = Redis.new
$rollout = Rollout.new($redis)
$toggle = Binflip.new($rollout)

Check for toggles:

$toggle.active?(:chat, @user)
$toggle.active?(:upload_video, @user)

If Rollout is present, Binflip delegates all methods to it (such as activate_user).

Environment Keys and Case Sensitivity

As it is customary to set ENVIRONMENT_VARS in all upcase, environment feature keys are upcase'd. So you must set your env keys to all upcase -- e.g. FEATURE_MY_COOL_FEATURE. But you can test with upcase, lowercase or as a symbol:

$binflip.active?(:my_cool_feature)
$binflip.active?('my_cool_feature')
$binflip.active?('MY_COOL_FEATURE')

These are all equivalent and will test for FEATURE_MY_COOL_FEATURE in the ENV hash. NOTE: this convention is only for ENV and not for rollout.

License

Copyright (c) 2012 Brian Kaney

MIT License

binflip's People

Contributors

bkaney avatar megatux avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.