is there a way to write this clean?

91 Views Asked by At

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

2

There are 2 best solutions below

3
Abdul Rehman On BEST ANSWER

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
3
Vishal Zambre On

Below is the one of modified version of above code

def input
  value = prompt_input("Enter value:").upcase
  update_flags(value)
end

def update_flags(value)
  case value
  when "BOTH"
    toggle_flags(:enable)
  when "FLAG_A"
    toggle_flags(:enable, :flag_a)
  when "FLAG_B"
    toggle_flags(:enable, :flag_b)
  when "NONE"
    toggle_flags(:disable)
  else
    puts "Invalid value: #{value}"
  end
end

def toggle_flags(action, *flags)
  flags = [:flag_a, :flag_b] if flags.empty? # Default flags to both if none specified
  flags.each { |flag| toggle_flag(action, flag) }
end

def toggle_flag(action, flag)
  Featureflag.user(@id).send(action, flag)
end

def prompt_input(message)
  print message + ' '
  gets.chomp
end

In case of larger case statements

FLAGS_MAPPING = {
  "BOTH" => [:enable, [:flag_a, :flag_b]],
  "FLAG_A" => [:enable, [:flag_a]],
  "FLAG_B" => [:enable, [:flag_b]],
  "NONE" => [:disable, [:flag_a, :flag_b]]
}

def input
  value = prompt_input("Enter value:").upcase
  update_flags(value)
end

def update_flags(value)
  action, flags = FLAGS_MAPPING[value] || [:invalid, nil]
  if action == :invalid
    puts "Invalid value: #{value}"
  else
    toggle_flags(action, *flags)
  end
end

def toggle_flags(action, *flags)
  flags.each { |flag| toggle_flag(action, flag) }
end

def toggle_flag(action, flag)
  Featureflag.user(@id).send(action, flag)
end

def prompt_input(message)
  print message + ' '
  gets.chomp
end