lib/unleash/feature_toggle.rb in unleash-3.2.1 vs lib/unleash/feature_toggle.rb in unleash-3.2.2

- old
+ new

@@ -1,6 +1,7 @@ require 'unleash/activation_strategy' +require 'unleash/constraint' require 'unleash/variant_definition' require 'unleash/variant' require 'unleash/strategy/util' require 'securerandom' @@ -11,24 +12,13 @@ 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.has_key?('name') && Unleash::STRATEGIES.has_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.has_key?('name') } - .map do |v| - VariantDefinition.new( - v.fetch('name', ''), - v.fetch('weight', 0), - v.fetch('payload', nil), - v.fetch('overrides', []) - ) - end || [] + self.strategies = initialize_strategies(params) + self.variant_definitions = initialize_variant_definitions(params) end def to_s "<FeatureToggle: name=#{name},enabled=#{enabled},strategies=#{strategies},variant_definitions=#{variant_definitions}>" end @@ -62,27 +52,33 @@ # only check if it is enabled, do not do metrics def am_enabled?(context, default_result) result = if self.enabled self.strategies.empty? || - self.strategies.any?{ |s| strategy_enabled?(s, context) } + self.strategies.any? do |s| + strategy_enabled?(s, context) && strategy_constraint_matches?(s, context) + end else default_result end - Unleash.logger.debug "FeatureToggle (enabled:#{self.enabled} default_result:#{default_result} " \ - "and Strategies combined returned #{result})" + Unleash.logger.debug "Unleash::FeatureToggle (enabled:#{self.enabled} default_result:#{default_result} " \ + "and Strategies combined with contraints returned #{result})" result end def strategy_enabled?(strategy, context) r = Unleash::STRATEGIES.fetch(strategy.name.to_sym, :unknown).is_enabled?(strategy.params, context) - Unleash.logger.debug "Strategy #{strategy.name} returned #{r} with context: #{context}" # "for params #{strategy.params} " + Unleash.logger.debug "Unleash::FeatureToggle.strategy_enabled? Strategy #{strategy.name} returned #{r} with context: #{context}" r end + def strategy_constraint_matches?(strategy, context) + strategy.constraints.empty? || strategy.constraints.all?{ |c| c.matches_context?(context) } + end + def disabled_variant Unleash::Variant.new(name: 'disabled', enabled: false) end def sum_variant_defs_weights @@ -124,8 +120,39 @@ 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 context + end + + def initialize_strategies(params) + params.fetch('strategies', []) + .select{ |s| s.has_key?('name') && Unleash::STRATEGIES.has_key?(s['name'].to_sym) } + .map do |s| + ActivationStrategy.new( + s['name'], + s['parameters'], + (s['constraints'] || []).map do |c| + Constraint.new( + c.fetch('contextName'), + c.fetch('operator'), + c.fetch('values') + ) + end + ) + end || [] + end + + def initialize_variant_definitions(params) + (params.fetch('variants', []) || []) + .select{ |v| v.is_a?(Hash) && v.has_key?('name') } + .map do |v| + VariantDefinition.new( + v.fetch('name', ''), + v.fetch('weight', 0), + v.fetch('payload', nil), + v.fetch('overrides', []) + ) + end || [] end end end