Class: SimpleToggle::Toggle
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SimpleToggle::Toggle
- Defined in:
- lib/simple_toggle.rb
Overview
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
Class Method Summary collapse
-
.active?(feature) ⇒ Boolean
Checks whether a given ‘feature` is active.
-
.require(feature) ⇒ Object
Raises an exception if
feature
does not exist. -
.when_active(feature) ⇒ Object
Execute the given block if the given
feature
exists and #active? returns true.
Class Method Details
.active?(feature) ⇒ Boolean
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
59 60 61 |
# File 'lib/simple_toggle.rb', line 59 def self.active?(feature) !!Toggle.where(name: feature, active: true).first end |
.require(feature) ⇒ Object
Raises an exception if feature
does not exist.
SimpleToggle::Toggle.require(:missing_feature)
# raises SimpleToggle::SimpleToggleError
45 46 47 |
# 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) ⇒ Object
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
37 38 39 |
# File 'lib/simple_toggle.rb', line 37 def self.when_active(feature) yield if Toggle.active?(feature) end |