Class: Shamu::Features::Toggle

Inherits:
Entities::Entity show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entities::Entity

#empty?, #id, model, model_name, null_entity, #persisted?, #present?, #redact, #redact!, #to_entity

Methods included from ToModelIdExtension::Models

#to_model_id

Methods included from Attributes::Equality

#==, #hash

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

#descriptionString

Returns human friendly description of the toggle.

Returns:

  • (String)

    human friendly description of the toggle.



32
# File 'lib/shamu/features/toggle.rb', line 32

attribute :description

#nameString

Returns name of the toggle, namespaced using paths.

Returns:

  • (String)

    name of the toggle, namespaced using paths.



28
# File 'lib/shamu/features/toggle.rb', line 28

attribute :name

#retire_atTime

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.

Returns:

  • (Time)

    When the feature toggle should be at 100% and removed from the code base.



48
# File 'lib/shamu/features/toggle.rb', line 48

attribute :retire_at

#selectorsArray<Selector>

Returns selectors used to match environment conditions to determine if the flag should be enabled.

Returns:

  • (Array<Selector>)

    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

#typeString

Returns type of the toggle. Offers a hint at acceptable caching strategies.

Returns:

  • (String)

    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.

Parameters:

  • path. (String)

Returns:

  • (Hash<String,Toggle>)

    of toggles by name.



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.

Parameters:

  • context (Context)

    the feature evaluation context.

Returns:

  • (Boolean)

    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.

Parameters:

  • context (Context)

    the feature evaluation context.

Returns:

  • (Boolean)

    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