编写长 case 语句的简短方法

Short way to write a 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

case statement 有效,但由于项目敏感性和 material,我无法向您展示其余代码。我的问题是,是否有更简单易读的方式来编写这个 case statement,或者更短的方式来编写它?

我不明白这个问题的一些细节,但这是您可能会采用的一般方法。我假设下面散列中的 :info:email 的值是方法的名称。 (我理解假设是不正确的。)以下可能有错误,考虑到我没有办法测试它。

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