Class: Shamu::Features::Toggle
- Inherits:
-
Entities::Entity
- Object
- Entities::Entity
- Shamu::Features::Toggle
- Defined in:
- lib/shamu/features/toggle.rb
Overview
A configured feature toggle.
Defined Under Namespace
Classes: ParsingState
Constant Summary collapse
- TYPES =
[ "release", # Feature is expected to become permanent and is used to # decouple deployment from production release. Relatively # short lived. "ops", # Controlled by operations as a kill-switch or tuning # option. Cohorts are typically not dynamic and apply to # all users. "experiment", # Used to explore the efficacy of an option by testing it # on a subset of the total users. "segment", # Long-lived toggle used to control access to a feature # based on some sort of user segmentation (e.g. dogfood, # internal, premium, etc.). ].freeze
Attributes collapse
-
#description ⇒ String
Human friendly description of the toggle.
-
#name ⇒ String
Name of the toggle, namespaced using paths.
-
#retire_at ⇒ Time
Toggles, and the code selected by them should be removed as soon as possible so that you don't have to maintain unused code.
-
#selectors ⇒ Array<Selector>
Selectors used to match environment conditions to determine if the flag should be enabled.
-
#type ⇒ String
Type of the toggle.
Class Method Summary collapse
-
.load(path) ⇒ Hash<String,Toggle>
Loads all the toggles from the YAML file at the given path.
Instance Method Summary collapse
-
#enabled?(context) ⇒ Boolean
True if the toggle should be enabled.
-
#initialize(attributes) ⇒ Toggle
constructor
A new instance of Toggle.
-
#retired?(context) ⇒ Boolean
True if the toggle is retired and should be always on.
Methods inherited from Entities::Entity
#empty?, #id, model, model_name, null_entity, #persisted?, #present?, #redact, #redact!, #to_entity
Methods included from ToModelIdExtension::Models
Methods included from Attributes::Equality
Methods included from Attributes
#[], #as_json, #assign_attributes, association, associations, attribute, #attribute?, attributes, #pretty_print, #set?, #slice, #to_attributes, #to_json
Constructor Details
#initialize(attributes) ⇒ Toggle
Returns a new instance of Toggle.
79 80 81 82 83 |
# File 'lib/shamu/features/toggle.rb', line 79 def initialize( attributes ) fail ArgumentError, "Must provide a retire_at attribute for '#{ attributes[ 'name' ] }' toggle." unless attributes["retire_at"] # rubocop:disable Metrics/LineLength fail ArgumentError, "Type must be one of #{ TYPES } for '#{ attributes[ 'name' ] }' toggle." unless TYPES.include?( attributes["type"] ) # rubocop:disable Metrics/LineLength super end |
Instance Attribute Details
#description ⇒ String
Returns human friendly description of the toggle.
32 |
# File 'lib/shamu/features/toggle.rb', line 32 attribute :description |
#name ⇒ String
Returns name of the toggle, namespaced using paths.
28 |
# File 'lib/shamu/features/toggle.rb', line 28 attribute :name |
#retire_at ⇒ Time
Toggles, and the code selected by them should be removed as soon as possible so that you don't have to maintain unused code. By explicitly setting a date when the toggle is no longer to be used, the system can help inform you when the code is no longer needed and can safely be removed.
48 |
# File 'lib/shamu/features/toggle.rb', line 48 attribute :retire_at |
#selectors ⇒ Array<Selector>
Returns selectors used to match environment conditions to determine if the flag should be enabled.
53 54 55 56 57 |
# File 'lib/shamu/features/toggle.rb', line 53 attribute :selectors do Array( select ).map do |config| Selector.new( self, config ) end end |
#type ⇒ String
Returns type of the toggle. Offers a hint at acceptable caching strategies.
37 |
# File 'lib/shamu/features/toggle.rb', line 37 attribute :type |
Class Method Details
.load(path) ⇒ Hash<String,Toggle>
Loads all the toggles from the YAML file at the given path.
97 98 99 100 101 102 |
# File 'lib/shamu/features/toggle.rb', line 97 def load( path ) toggles = {} load_from_path( path, toggles, ParsingState.new( nil, nil ) ) toggles end |
Instance Method Details
#enabled?(context) ⇒ Boolean
Returns true if the toggle should be enabled.
67 68 69 70 71 |
# File 'lib/shamu/features/toggle.rb', line 67 def enabled?( context ) if selector = matching_selector( context ) !selector.reject end end |
#retired?(context) ⇒ Boolean
Returns true if the toggle is retired and should be always on.
75 76 77 |
# File 'lib/shamu/features/toggle.rb', line 75 def retired?( context ) !retire_at || context.time > retire_at end |