So i have a flag to disable and enable a feature. and will based on the variable it has to toggle the flags
flag_a
, flag_b
, are my two feature flags and the variable input string can be none
, all
,flag_a
, flag_b
as a input string we could further add more flags like flag_c or flag_d too so on.
def input
variable = stdin "value"
update_flag(value.upcase)
end
def update_flag(value)
case value
when "ALL"
toggle_flag(flag_a, true)
toogle_flag(flag_b, true)
when "FLAG_A"
toggle_flag(flag_a, true)
toogle_flag(flag_b, false)
when "FLAG_B"
toggle_flag(flag_a, false)
toogle_flag(flag_b, true)
when "NONE"
toggle_flag(flag_a, flase)
toogle_flag(flag_b, false)
end
def toggle_flag(name, flag)
if flag
Featureflag.user(@id).enable(name.to_sym)
else
Featureflag.user(@id).disable(name.to_sym)
end
end
i want a way to send the values to the variables this input is only send through console as input.
trying to make it dry and also make it optimistic along with it has to be easy for flexible methods note "input is only send through console as input" also if we add flag_C then we need to have a way to take multiple inputs which have to be enabled and rest has to be disabled
You don't need the when condition for this, you can just use the flag name to check if the feature needs to be enabled or disabled, like
def input
variable = stdin "value"
update_flag(value.upcase)
end
def update_flag(value)
toggle_flag("flag_a", value)
toggle_flag("flag_b", value)
end
def enable_feature?(name, flag)
return true if flag == "BOTH"
return false if flag == "NONE"
flag.downcase == name
end
def toggle_flag(name, flag)
if enable_feature?(name, flag)
Featureflag.user(@id).enable(name.to_sym)
else
Featureflag.user(@id).disable(name.to_sym)
end
end
UPDATE:
According to the suggestions:
In case of larger case statements
FLAGS = [
"flag_a",
"flag_b",
"flag_c",
]
def input
value = prompt_input("Enter value:").upcase
update_flags(value)
end
def update_flag(value)
FLAGS.each do { |flag_name| toggle_flag(flag_name, value) }
end
def enable_feature?(name, flag)
return true if flag == "BOTH" || flag == "ALL"
return false if flag == "NONE"
flag.downcase == name
end
def toggle_flag(name, flag)
if enable_feature?(name, flag)
Featureflag.user(@id).enable(name.to_sym)
else
Featureflag.user(@id).disable(name.to_sym)
end
end