module ConfigManager module Toggles class Definition def initialize(test) @key = key(test) end def name @key.gsub(Definition.prefix, '') end def type @type ||= $redis.hget(@key, 'type') end def type=(type) return unless name.present? unless Definition.types.include?(type) raise("#{type} is an unsupported Toggle type. Acceptable values are #{Definition.types}") end $redis.hset(@key, 'type', type) @type = $redis.hget(@key, 'type') end def type_class return Null unless valid? eval("ConfigManager::Toggles::#{type.camelize}") end def acceptable_values @values ||= $redis.hget(@key, 'acceptable_values') #self.send("to_#{type}", @values) type_class.from_json(@values) end def acceptable_values=(values) return unless valid? $redis.hset(@key, 'acceptable_values', type_class.to_json(values)) #$redis.hset(@key, 'acceptable_values', self.send("to_#{type}", values)) @values = $redis.hget(@key, 'acceptable_values') end def delete $redis.del(@key) end def self.types %w(boolean set) end def self.prefix "#{Rails.application.class.parent_name.underscore}_toggles-" end def valid? !!(name.present? && type) end def message return "missing name" unless name.present? return "missing type" unless type.present? nil end alias_method :formatted_acceptable_values=, :acceptable_values= def formatted_acceptable_values values = acceptable_values values.respond_to?(:join) ? values.join(',') : values end def to_yaml to_hash.to_yaml end def to_hash {name => {'type' => type, 'acceptable_values' => formatted_acceptable_values}} end private def key(test) "#{Definition.prefix}#{test}" end def to_boolean(value) if value.is_a?(String) !!(value =~ /^(true|t|yes|y|1)$/i) else value end end def to_set(values) values || [] end def to_(values) nil end end end end