Search code examples
rubyif-statementcasecalabash

Better way to pass parameters to ruby calabash method


I have the following ruby calabash step:

When(/^I enter (phone number|email)$/) do |method|
login_type = case method
        when 'phone number'
          true
        when 'email'
          false
        else
          raise("#{method} is not supported")
      end
verify_login_page(type: login_type)
aggregator = case method
             when 'phone number'
               Aggregator::PHONE
             when 'email'
               Aggregator::EMAIL
             else
               Aggregator::ALL
           end
get_and_enter_code(aggregator)
end

Even though it seems clear, I'm pretty sure I can make it better from design perspective. Especially what I don't like is login_type case method with true/false assignment. How it's better to do it? Thanks


Solution

  • You can use ternary operator assignment:

    When(/^I enter (phone number|email)$/) do |method|
      unless  ['phone number', 'email'].include?(method)
        raise "#{method} is not supported"
      else
        login_type = method == 'phone number' ? true : false
      end
    
      verify_login_page(type: login_type)
    
      aggregator = login_type ? Aggregator::PHONE : Aggregator::EMAIL
    
      get_and_enter_code(aggregator)
    end
    

    Since you raise exception Aggregator::ALL case should never occur.