Search code examples
rubycase-statement

Short way to write a long case statement


I have an extremely long case statement:

def gather_intel
  case OPTIONS[:type]
    when /osha/
      FORMAT.info('Creating OSHA Regional email..')
      EMAILS.osha_reg
    when /pend/
      FORMAT.info('Creating 6 day hold pending email..')
      EMAILS.pend
    when /60/
      FORMAT.info('Creating 60 day hold account deletion email..')
      EMAILS.sixty_day
    when /generic/
      FORMAT.info('Creating generic email..')
      EMAILS.generic
    when /resolve/
      FORMAT.info('Creating resolution ticket..')
      EMAILS.resolve
    when /esc/
      FORMAT.info('Creating escalation ticket..')
      EMAILS.assign
    when /pii/
      FORMAT.info('Creating request to remove personal info..')
      EMAILS.remove_pii
    when /vip/
      FORMAT.info('Creating VIP user email..')
      EMAILS.vip_user
    when /inop/
      FORMAT.info('Creating INOP user email..')
      EMAILS.in_op_user
    when /dev/
      if OPTIONS[:type].to_s.include?('dev=unlock')
        message = 'unlock'
      else
        message = 'password reset'
      end
      FORMAT.info("Creating dev account #{message} email")
      EMAILS.dev_account(OPTIONS[:type])
    else
      raise ERROR
  end
end

This case statement works however I will be unable to show you the rest of the code due to the project sensitivity and material. My question is, is there an easier more readable way to write this case statement, or a shorter way to write it?


Solution

  • I don't understand some of the details of the question, but here's a general approach you might take. I've assumed that :info and the values of :email in the hash below are names of methods. (I understand that assumption is incorrect.) The following may have errors, considering that I have no means of testing it.

    DATA = [[/osha/,    'Creating OSHA Regional email..',                :osha_reg],
            [/pend/,    'Creating 6 day hold pending email..',           :pend],
            [/60/,      'Creating 60 day hold account deletion email..', :sixty_day],
            [/generic/, 'Creating generic email..',                      :generic],
            [/resolve/, 'Creating resolution ticket..',                  :resolve],
            [/esc/,     'Creating escalation ticket..',                  :assign],
            [/pii/,     'Creating request to remove personal info..',    :remove_pii],
            [/vip/,     'Creating VIP user email..',                     :vip_user],
            [/inop/,    'Creating INOP user email..',                    :in_op_user]]
    
    def gather_intel
      type = OPTIONS[:type]
      regex, msg, email = DATA.find { |r,*_| type =~ r }
      if regex
        FORMAT.send :info, msg
        EMAILS.send email
      elsif type =~ /dev/ 
        message = type.to_s.include?('dev=unlock') ? 'unlock' : 'password reset'
        FORMAT.info("Creating dev account #{message} email")
        EMAILS.dev_account(type)
      else
        raise ERROR
      end
    end