feature_definitions¶ ↑
<img src=“https://travis-ci.org/rmg/feature_definitions.png?branch=master” alt=“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¶ ↑
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright¶ ↑
Copyright © 2013 Ryan Graham. See LICENSE.txt for further details. (TL;DR: MIT license)