lib/unleash/feature_toggle.rb in unleash-0.1.4 vs lib/unleash/feature_toggle.rb in unleash-0.1.5

- old
+ new

@@ -3,14 +3,16 @@ module Unleash class FeatureToggle attr_accessor :name, :enabled, :strategies, :choices, :choices_lock def initialize(params={}) - self.name = params['name'] || nil - self.enabled = params['enabled'] || false + params = {} if params.nil? - self.strategies = params['strategies'] + 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'])} || [] # Unleash.logger.debug "FeatureToggle params: #{params}" # Unleash.logger.debug "strategies: #{self.strategies}" @@ -24,15 +26,15 @@ 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 set to nil." context = nil end - result = self.enabled && self.strategies.select{ |s| + 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? + }.any? || self.strategies.empty? ) result ||= default_result Unleash.logger.debug "FeatureToggle (enabled:#{self.enabled} default_result:#{default_result} and Strategies combined returned #{result})" choice = result ? :yes : :no