feature_definitions

Build Status

A minimal useful feature toggle mechanism.

Thanks to Travis-CI, tested on:

- "1.8.7"  ***
- "1.9.2"
- "1.9.3"
- "2.0.0"
- jruby-18mode # JRuby in 1.8 mode  ***
- jruby-19mode # JRuby in 1.9 mode
- rbx-18mode  ***
- rbx-19mode

Note on 1.8

I’ve given up trying to find a cross version way of using instance_eval/exec to give a nicer syntax to feature evaluation blocks, but it’s just not working out. If you’re using 1.8, you’re block will just have to take the context as an argument.

Installation

gem 'feature_definitions'

Example

Using constructor

lib/features.rb:

class Features < FeatureDefinitions
  AWESOME = self.new {|flags| flags.is_awesome? }
  # > 1.8 only, evaluated in context of Features.context
  AWESOME = self.new { is_awesome? }
end

app/main.rb:

Features.context = args_to_flags

app/elsewhere.rb

def some_method
  Features.AWESOME.enabled? do
    # do feature
  end
  # else noop
end

def some_other_method
  Features.AWESOME do
    # declarative form!
  end
end

Rails (and define_feature helper)

Rails is NOT required, but this is how you would use this gem with Rails.

app/model/features.rb:

class Features < FeatureDefinitions
  define_feature :AWESOME do |user|
    user.is_awesome?
  end
  # > 1.8 only, evaluated in context of Features.context
  define_feature :AWESOMER do
    is_awesome?
  end
end

app/controller/application_controller.rb:

class ApplicationController < ActionController::Base
  before_filter { |controller| Features.context = controller.current_user }
end

app/views/some/view.erb.html:

<% if Features.AWESOME.enabled? %>
  You have the awesome feature enabled!
<% end %>
<% Features.AWESOME.enabled? do %>
  You have awesome blocks enabled!
<% end %>
<% Features.AWESOMER do %>
  Declarative feature blocks!
<% end %>

Contributing to feature_definitions

Copyright © 2013 Ryan Graham. See LICENSE.txt for further details. (TL;DR: MIT license)