欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多判断转发的方案

程序员文章站 2022-07-14 18:52:40
...
很多场景用到条件分发跳转

方案一:
def aa(condition,option)
  if condition == 1
     todo 1
  elsif condition == 2
     todo 2
  end
end

示例:
def get_charge(serve)
    if serve.is_a? QuantityServe
      charge = serve.compute_price(good.unified_quantity * number, extra)
    elsif serve.is_a? NumberServe
      charge = serve.compute_price(number, extra)
    else
      charge = serve.compute_price(number, extra)
    end
    charge
  end

方案二:
def bb(condition_x,option)
   send(condition_x,option)
end

def condition_1(option)
end

def condition_2(option)
end

示例:
def perform(order_detail_id,action_tag,send_email,current_employee_name)
       Api::OverseasRealOrder.new(order_detail_id,send_email,current_employee_name).send(action_tag)	
    end

方案三:
def cc(obj,option)
   obj = obj || obj.class
   obj.cc(option)
end

class a
  def cc(option)
  end
end

class b
  def self.cc(option)
  end
end


方案四:
class base
   def initialize(objs,option)
     @objs
     @option
   end
   def a
     objs...option
   end
end

示例:
 @checked_items = CartItem.where(session_id: session_id).pending.checked
 SummaryService.new(@checked_items, buyer_id: buyer_id, extra: extra)

class SummaryService
 def initialize(_checked_items, buyer_id: nil, extra: {})
    @checked_items = _checked_items
    @buyer = Buyer.find(buyer_id) if buyer_id
    @extra = extra
    a
    b
  end
  
  def a
  end 
 
  def b
  end 
end