使用数学运算符减少代码

时间:2017-05-22 17:38:36

标签: ruby-on-rails

我在Ruby on Rails下运行了以下代码,它正在运行,但我知道我们可以使用数学运算符来减少它,对吗?

以下是实际运行的代码:

def pms_guest_purchase
if @effective_portal && @effective_portal.name == "Rose Splash Portal" &&
    #Plan MP 1 dia 2 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 1))
    @usage_plan = UsagePlan.find_by_id(62)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 4 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 2))
    @usage_plan = UsagePlan.find_by_id(64)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 6 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 3))
    @usage_plan = UsagePlan.find_by_id(104)
    @usage_plans = [ @usage_plan ]      
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 8 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 4))
    @usage_plan = UsagePlan.find_by_id(105)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 10 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 5))
    @usage_plan = UsagePlan.find_by_id(106)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 12 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 6))
    @usage_plan = UsagePlan.find_by_id(107)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 14 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 7))
    @usage_plan = UsagePlan.find_by_id(108)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 14 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 8))
    @usage_plan = UsagePlan.find_by_id(109)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 14 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 9))
    @usage_plan = UsagePlan.find_by_id(110)
    @usage_plans = [ @usage_plan ]
elsif @effective_portal && @effective_portal.name == "Rose Splash Portal" &&    
    #Plan MP 1 dia 14 usuarios
    (@guest.custom0 == 'MP' && (@guest.custom6.to_i + @guest.custom7.to_i == 10))
    @usage_plan = UsagePlan.find_by_id(111)
    @usage_plans = [ @usage_plan ]

我正在尝试这个,但看起来我做错了

if @effective_portal && @effective_portal.name == "Rose Splash Portal" &&
    #Plan MP 1 dia 20 usuarios
    (@guest.custom0 == 'MP' &&  ((@guest.custom6) + (@guest.custom7)) == '2')
    @usage_plan = UsagePlan.find_by_id(64)
    @usage_plans = [ @usage_plan ]

elsif @effective_portal && @effective_portal.name == "Guest Splash Policy" && #Plan MP 1 dia 20 usuarios (@guest.custom0 == 'MP' && ((@guest.custom6) + (@guest.custom7)) == '3') @usage_plan = UsagePlan.find_by_id(104) @usage_plans = [ @usage_plan ]

如果有帮助,这是完整的领域。 Full Code.

提前致谢!

2 个答案:

答案 0 :(得分:1)

首先要说的是,命名你的字段'custom6 / 7/8'是非常糟糕的命名惯例,最终会导致大规模的混乱,也可能是大灾难。

如果你想让连接数量成为房间里人数的函数(即2个人= 4个连接,3个人= 6个连接等),你的代码可以大量压缩到像此

if @effective_portal && @effective_portal.name == "Guest Splash Policy" && 
 @guest.custom8 == 'MP PLATINO'
  @usage_plan = (@guest.custom6.to_i + @guest.custom7.to_i * 2)
  @usage_plans = [ @usage_plan ]
end

答案 1 :(得分:0)

从假类开始,仅用于说明目的:

  class Guest

    attr_accessor :custom0, :custom6, :custom7, :custom8

    def initialize(args)
      args.each do |k,v|
        self.send("#{k}=",v)
      end
    end

  end

创建映射到plan_idnumber_of_guests的{​​{1}}数组:

type_of_guest

那只是其中的一部分。你明白了。

创建一些实用方法:

  PLAN_POLICIES = [
    {number_of_guests: 10, type_of_guest: "MP PLATINO", plan_id: 64},
    {number_of_guests: 10, type_of_guest: "MP", plan_id: 53},
    {number_of_guests: 9, type_of_guest: "MP PLATINO", plan_id: 63},
    {number_of_guests: 9, type_of_guest: "MP", plan_id: 52},
    {number_of_guests: 8, type_of_guest: "MP PLATINO", plan_id: 62},
    {number_of_guests: 8, type_of_guest: "MP", plan_id: 51},
    {number_of_guests: 7, type_of_guest: "MP PLATINO", plan_id: 61},
    {number_of_guests: 7, type_of_guest: "MP", plan_id: 50},
  ]

在控制台中运行:

  def plan_id
    return PLAN_POLICIES.find do |pp|
      pp[:number_of_guests] == number_of_guests && pp[:type_of_guest] == type_of_guest
    end[:plan_id]    
  end

  def number_of_guests
    @guest.custom6.to_i + @guest.custom7.to_i
  end

  def type_of_guest
    return "MP" if @guest.custom0 == "MP"
    return "MP PLATINO" if @guest.custom8 == "MP PLATINO"
  end

使用类似:

  2.3.1 :039 >  @guest = Guest.new(custom6: '1', custom7: '9', custom8: 'MP PLATINO')
  2.3.1 :040 >  plan_id
   => 64 
  2.3.1 :041 >  @guest = Guest.new(custom6: '2', custom7: '6', custom8: 'MP PLATINO')
  2.3.1 :042 >  plan_id
   => 62 
  2.3.1 :043 >  @guest = Guest.new(custom0: "MP", custom6: '10', custom7: '')
  2.3.1 :044 >  plan_id
   => 53 
  2.3.1 :045 >  @guest = Guest.new(custom0: "MP", custom6: '2', custom7: '5')
  2.3.1 :046 >  plan_id
   => 50

将约200行代码转换为~40行代码。快乐!

顺便说一下,你真的不应该手动编码 @usage_plan = UsagePlan.find_by_id(plan_id) 。那最终会咬你的。