module ConfigManager module Toggle def self.active?(test, challenge=nil) ToggleIdentityMap.value(test) { active_in_db?(test, challenge) } end def self.active_in_db?(test, challenge=nil) return true if stub_in_host_app? toggle_def = Toggles::Definition.new(test) return false unless toggle_def.type type_klass = toggle_def.type_class if challenge type_klass.active? toggle_def, challenge else type_klass.active? toggle_def end end def self.toggles toggle_defs = [] $redis.keys("#{Toggles::Definition.prefix}*").each do |key| toggle_defs << Toggles::Definition.new(key.gsub(Toggles::Definition.prefix, '')) end toggle_defs.sort{|x,y| x.name <=> y.name} end def self.create(*args) toggle_hash = args.first toggle_def = Toggles::Definition.new(toggle_hash[:name]) toggle_def.type = toggle_hash[:type] values = (toggle_hash[:type] == 'boolean') ? toggle_hash[:formatted_acceptable_values] : toggle_hash[:formatted_acceptable_values].gsub(/\s*,\s*/,',').split(',') toggle_def.acceptable_values = values toggle_def end def self.delete(name) toggle = Toggles::Definition.new(name) toggle.delete end def self.from_yaml(path) toggles = YAML::load(File.open(path)) counts = [0, 0] toggles.each do |name, values| begin create({:name => name, :type => values['type'], :formatted_acceptable_values => values['acceptable_values']}) counts[0] += 1 rescue RuntimeError Rails.logger.error "Bad toggle definition. #{$!}" counts[1] += 1 end end counts end def self.to_yaml toggles_hash = Hash[toggles.map(&:to_hash).map{|t| [t.keys.first, t.values.first]}] YAML.dump(toggles_hash) end def self.quick(name, acceptable_values='test') create :name => name, :type => 'set', :formatted_acceptable_values => acceptable_values end private # true when running tests in a host app def self.stub_in_host_app? Rails.env.test? && !Rails.root.to_s.match(ConfigManager::Engine.root.to_s) end end end