Class: Unleash::FeatureToggle
- Inherits:
-
Object
- Object
- Unleash::FeatureToggle
- Defined in:
- lib/unleash/feature_toggle.rb
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#name ⇒ Object
Returns the value of attribute name.
-
#strategies ⇒ Object
Returns the value of attribute strategies.
-
#variant_definitions ⇒ Object
Returns the value of attribute variant_definitions.
Instance Method Summary collapse
- #get_variant(context, fallback_variant = disabled_variant) ⇒ Object
-
#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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/unleash/feature_toggle.rb', line 11 def initialize(params={}) params = {} if params.nil? self.name = params.fetch('name', nil) self.enabled = params.fetch('enabled', false) self.strategies = params.fetch('strategies', []) .select{ |s| ( s.key?('name') && Unleash::STRATEGIES.key?(s['name'].to_sym) ) } .map{ |s| ActivationStrategy.new(s['name'], s['parameters'] || {}) } || [] self.variant_definitions = (params.fetch('variants', []) || []) .select{ |v| v.is_a?(Hash) && v.key?('name') } .map{ |v| VariantDefinition.new( v.fetch('name', ''), v.fetch('weight', 0), v.fetch('payload', nil), v.fetch('overrides', []) ) } || [] end |
Instance Attribute Details
#enabled ⇒ Object
Returns the value of attribute enabled.
9 10 11 |
# File 'lib/unleash/feature_toggle.rb', line 9 def enabled @enabled end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/unleash/feature_toggle.rb', line 9 def name @name end |
#strategies ⇒ Object
Returns the value of attribute strategies.
9 10 11 |
# File 'lib/unleash/feature_toggle.rb', line 9 def strategies @strategies end |
#variant_definitions ⇒ Object
Returns the value of attribute variant_definitions.
9 10 11 |
# File 'lib/unleash/feature_toggle.rb', line 9 def variant_definitions @variant_definitions end |
Instance Method Details
#get_variant(context, fallback_variant = disabled_variant) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/unleash/feature_toggle.rb', line 51 def get_variant(context, fallback_variant = disabled_variant) unless ['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 set to nil." context = nil end unless ['Unleash::Variant'].include? fallback_variant.class.name raise ArgumentError, "Provided fallback_variant is not of the correct type #{fallback_variant.class.name}, please use Unleash::Variant." end return disabled_variant unless self.enabled && am_enabled?(context, true) return disabled_variant if get_sum_variant_defs_weights <= 0 variant = variant_from_override_match(context) variant = variant_from_weights(context) if variant.nil? Unleash.toggle_metrics.increment_variant(self.name, variant.name) unless Unleash.configuration.disable_metrics return variant end |
#is_enabled?(context, default_result) ⇒ Boolean
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/unleash/feature_toggle.rb', line 37 def is_enabled?(context, default_result) unless ['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 set to nil." context = nil end result = am_enabled?(context, default_result) choice = result ? :yes : :no Unleash.toggle_metrics.increment(name, choice) unless Unleash.configuration.disable_metrics return result end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/unleash/feature_toggle.rb', line 33 def to_s "<FeatureToggle: name=#{self.name},enabled=#{self.enabled},strategies=#{self.strategies},variant_definitions=#{self.variant_definitions}>" end |