Class: ConfigManager::Toggles::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/config_manager/toggles/definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test) ⇒ Definition

Returns a new instance of Definition.



5
6
7
# File 'lib/config_manager/toggles/definition.rb', line 5

def initialize(test)
 	@key = key(test)
end

Class Method Details

.prefixObject



58
59
60
# File 'lib/config_manager/toggles/definition.rb', line 58

def self.prefix
 	"#{Rails.application.class.parent_name.underscore}_toggles-"
end

.typesObject



54
55
56
# File 'lib/config_manager/toggles/definition.rb', line 54

def self.types
 	%w(boolean set)
end

Instance Method Details

#acceptable_valuesObject



36
37
38
39
40
# File 'lib/config_manager/toggles/definition.rb', line 36

def acceptable_values
 	@values ||= $redis.hget(@key, 'acceptable_values')
 	#self.send("to_#{type}", @values)
 	type_class.from_json(@values)
end

#acceptable_values=(values) ⇒ Object Also known as: formatted_acceptable_values=



42
43
44
45
46
47
48
# File 'lib/config_manager/toggles/definition.rb', line 42

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

#deleteObject



50
51
52
# File 'lib/config_manager/toggles/definition.rb', line 50

def delete
 	$redis.del(@key)
end

#exists?Boolean

Returns:



9
10
11
# File 'lib/config_manager/toggles/definition.rb', line 9

def exists?
	$redis.exists(@key)
end

#formatted_acceptable_valuesObject



73
74
75
76
# File 'lib/config_manager/toggles/definition.rb', line 73

def formatted_acceptable_values
	values = acceptable_values
	values.respond_to?(:join) ? values.join(',') : values
end

#messageObject



66
67
68
69
70
# File 'lib/config_manager/toggles/definition.rb', line 66

def message
	return "missing name" unless name.present?
	return "missing type" unless type.present?
	nil
end

#nameObject



13
14
15
# File 'lib/config_manager/toggles/definition.rb', line 13

def name
 	@name ||=	@key.gsub(Definition.prefix, '')
end

#to_hashObject



82
83
84
# File 'lib/config_manager/toggles/definition.rb', line 82

def to_hash
  {name => {'type' => type, 'acceptable_values' => formatted_acceptable_values}}
end

#to_yamlObject



78
79
80
# File 'lib/config_manager/toggles/definition.rb', line 78

def to_yaml
  to_hash.to_yaml
end

#typeObject



17
18
19
# File 'lib/config_manager/toggles/definition.rb', line 17

def type
 	@type ||= $redis.hget(@key, 'type')
end

#type=(type) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/config_manager/toggles/definition.rb', line 21

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

#type_classObject



31
32
33
34
# File 'lib/config_manager/toggles/definition.rb', line 31

def type_class
 		return Null unless valid?
 		eval("ConfigManager::Toggles::#{type.camelize}")
end

#valid?Boolean

Returns:



62
63
64
# File 'lib/config_manager/toggles/definition.rb', line 62

def valid?
	!!(name.present? && type)
end