Class: Unleash::FeatureToggle
- Inherits:
-
Object
- Object
- Unleash::FeatureToggle
- Defined in:
- lib/unleash/feature_toggle.rb
Instance Attribute Summary collapse
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#choices_lock ⇒ Object
Returns the value of attribute choices_lock.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#name ⇒ Object
Returns the value of attribute name.
-
#strategies ⇒ Object
Returns the value of attribute strategies.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ FeatureToggle
constructor
A new instance of FeatureToggle.
- #is_enabled?(context, default_result) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ FeatureToggle
Returns a new instance of FeatureToggle.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/unleash/feature_toggle.rb', line 7 def initialize(params={}) self.name = params['name'] || nil self.enabled = params['enabled'] || false self.strategies = params['strategies'] .select{|s| ( s.key?('name') && Unleash::STRATEGIES.key?(s['name'].to_sym) ) } .map{|s| ActivationStrategy.new(s['name'], s['parameters'])} || [] # Unleash.logger.debug "FeatureToggle params: #{params}" # Unleash.logger.debug "strategies: #{self.strategies}" end |
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
5 6 7 |
# File 'lib/unleash/feature_toggle.rb', line 5 def choices @choices end |
#choices_lock ⇒ Object
Returns the value of attribute choices_lock.
5 6 7 |
# File 'lib/unleash/feature_toggle.rb', line 5 def choices_lock @choices_lock end |
#enabled ⇒ Object
Returns the value of attribute enabled.
5 6 7 |
# File 'lib/unleash/feature_toggle.rb', line 5 def enabled @enabled end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/unleash/feature_toggle.rb', line 5 def name @name end |
#strategies ⇒ Object
Returns the value of attribute strategies.
5 6 7 |
# File 'lib/unleash/feature_toggle.rb', line 5 def strategies @strategies end |
Instance Method Details
#is_enabled?(context, default_result) ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/unleash/feature_toggle.rb', line 23 def is_enabled?(context, default_result) if not ['NilClass', 'Unleash::Context'].include? context.class.name Unleash.logger.error "Provided context is not of the correct type #{context.class.name}, please use Unleash::Context" context = nil end result = self.enabled && self.strategies.select{ |s| strategy = Unleash::STRATEGIES.fetch(s.name.to_sym, :unknown) r = strategy.is_enabled?(s.params, context) Unleash.logger.debug "Strategy #{s.name} returned #{r} with context: #{context}" #"for params #{s.params} " r }.any? result ||= default_result Unleash.logger.debug "FeatureToggle (enabled:#{self.enabled} default_result:#{default_result} and Strategies combined returned #{result})" choice = result ? :yes : :no Unleash.toggle_metrics.increment(name, choice) unless Unleash.configuration.disable_metrics return result end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/unleash/feature_toggle.rb', line 19 def to_s "<FeatureToggle: name=#{self.name},enabled=#{self.enabled},choices=#{self.choices},strategies=#{self.strategies}>" end |