class SimpleToggle::Toggle

SimpleToggle - A simple framework for feature toggles.

SimpleToggle provides feature toggle function that allow a developer to enable or disable code in an environment without deploying code. This decouples the deployment of the code from the delivery of the feature.

A toggle is a simple ActiveRecord model that can be created, modified, or destroyed like any other. The toggle will grant access to blocks of code if the given record exists and is active.

Create a toggle

SimpleToggle::Toggle.create name: :active_feature, active: true

Public Class Methods

active?(feature) click to toggle source

Checks whether a given ‘feature` is active. To be considered active, a toggle must be both present and active.

SimpleToggle::Toggle.active?(:active_feature)
  # => true
SimpleToggle::Toggle.active?(:inactive_feature)
  # => false
SimpleToggle::Toggle.active?(:missing_feature)
  # => false
# File lib/simple_toggle.rb, line 59
def self.active?(feature)
  !!Toggle.where(name: feature, active: true).first
end
require(feature) click to toggle source

Raises an exception if feature does not exist.

SimpleToggle::Toggle.require(:missing_feature)
  # raises SimpleToggle::SimpleToggleError
# File lib/simple_toggle.rb, line 45
def self.require(feature)
  raise SimpleToggle::ToggleError.new('Feature does not exist') unless Toggle.active?(feature)
end
when_active(feature) { || ... } click to toggle source

Execute the given block if the given feature exists and active? returns true.

SimpleToggle::Toggle.when_active(:active_feature) { puts 'feature is active' }
  # feature is active
  # => nil
SimpleToggle::Toggle.when_active(:inactive_feature) { puts 'feature is active' }
  # => nil
SimpleToggle::Toggle.when_active(:missing_feature) { puts 'feature is active' }
  # => nil
# File lib/simple_toggle.rb, line 37
def self.when_active(feature)
  yield if Toggle.active?(feature)
end