如何在ruby中的特定条件之后结束每个循环

时间:2015-09-08 06:19:38

标签: ruby-on-rails ruby ruby-on-rails-4

我已经包含了给定的代码

  def check_ip
    start_ip = IPAddr.new(myip).to_i
    end_ip = IPAddr.new(endip).to_i
    ip_pool = IpTab.all
    p '!!!!!!!!!!!!!!!!!!!!!!!'
    p ip_pool
    ip_pool.each do |ip|
     low = IPAddr.new(ip.start_ip).to_i
     high = IPAddr.new(ip.end_ip).to_i
     p '-------------------------------'
     p ((low..high)===start_ip)
     p ((low..high)===start_ip)
     p '******************************'
     break  if (low..high)===start_ip
     break  if (low..high)===end_ip
     p '*******************************'
     self.errors.add(:start_ip, I18n.t('errors.start_ip'))

    end
  end  

我得到了输出:

"!!!!!!!!!!!!!!!!!!!!!!!"
  IpPool Load (0.1ms)  SELECT `ip_pools`.* FROM `ip_pools`
#<ActiveRecord::Relation [#<IpPool id: 1, start_ip: "10.10.10.10", end_ip: "10.10.10.20", user_id: 1, created_at: "2015-09-08 05:12:34", updated_at: "2015-09-08 05:12:34">, #<IpPool id: 4, start_ip: "11.12.12.13", end_ip: "11.12.12.16", user_id: 1, created_at: "2015-09-08 06:08:38", updated_at: "2015-09-08 06:08:38">]>
"-------------------------------"
true
true
"******************************"

但如果我的start_ip或end_ip位于数据库中给定的ips之间,则它不能正常工作,那么它不允许保存ip。即如果(low..high)===start_ip(low..high)===end_ip为真,则不允许保存 指导我如何解决这个问题,因为我的代码不能正常工作,指导我如何编写这段代码。

4 个答案:

答案 0 :(得分:3)

  

如果(low..high)===start_ipif (low..high)===end_iptrue则不允许保存

你的循环不起作用。我们假设(low..high)===start_iptrue。你的循环变为:

ip_pool.each do |ip|
  low = IPAddr.new(ip.start_ip).to_i
  high = IPAddr.new(ip.end_ip).to_i
  break if true                                         # loop exits here
  break if (low..high)===end_ip                         # not called
  self.errors.add(:start_ip, I18n.t('errors.start_ip')) # not called either
end

如果if (low..high)===end_iptrue,则会变为:

ip_pool.each do |ip|
  low = IPAddr.new(ip.start_ip).to_i
  high = IPAddr.new(ip.end_ip).to_i
  break if (low..high)===start_ip                       # nothing happens                
  break if true                                         # loop exits here
  self.errors.add(:start_ip, I18n.t('errors.start_ip')) # not called
end

无论哪种方式,self.errors.add 都不被调用。如果两个条件都是false,那么 就会被调用,可能不是你想要的。

要解决您的问题,您可以写:

ip_pool.each do |ip|
  low = IPAddr.new(ip.start_ip).to_i
  high = IPAddr.new(ip.end_ip).to_i

  if (low..high).include?(start_ip) || (low..high).include?(end_ip)
    errors.add(:start_ip, I18n.t('errors.start_ip'))
    break
  end
end

或者有不同的错误:

ip_pool.each do |ip|
  low = IPAddr.new(ip.start_ip).to_i
  high = IPAddr.new(ip.end_ip).to_i

  if (low..high).include?(start_ip)
    errors.add(:start_ip, I18n.t('errors.start_ip'))
    break
  elsif (low..high).include?(end_ip)
    errors.add(:end_ip, I18n.t('errors.end_ip'))
    break
  end
end

请注意,我已将rng===obj替换为rng.include?(obj)

此外,我认为您的before_save :check_ip应为validate :check_ip,如Performing Custom Validations所示。该文档还说明了如何实现可应用于多个属性的EachValidator

答案 1 :(得分:1)

您真正需要做的是,在验证失败时返回false。我猜你应该试试这个

    def check_ip
        start_ip = IPAddr.new(self.start_ip).to_i
        end_ip = IPAddr.new(self.end_ip).to_i
        ip_pool = IpPool.all
        p '!!!!!!!!!!!!!!!!!!!!!!!'
        p ip_pool
        begin
            ip_pool.each do |ip|
                low = IPAddr.new(ip.start_ip).to_i
                high = IPAddr.new(ip.end_ip).to_i
                p '-------------------------------'
                p ((low..high)===start_ip)
                p ((low..high)===start_ip)
                p '******************************'
                raise ArgumentError, I18n.t('errors.start_ip')  if ((low..high)===start_ip or (low..high)===end_ip
                p '*******************************'
            end
        rescue ArgumentError => msg
            p msg
            self.errors.add(:start_ip, msg)
            return false
        end
        return true
    end

答案 2 :(得分:1)

有一个validates宝石,其IpValidator

你可以

validates :start_ip, ip: true

答案 3 :(得分:1)

如果我是你,我会向Ip模型添加自定义验证检查:

validate :validate_pool_existence!

def validate_pool_existence
  # DRY by taste
  errors.add(:start_ip, I18n.t('errors.start_ip')) if IpPool.contains_ip?(start_ip)
  errors.add(:end_ip, I18n.t('errors.end_ip')) if IpPool.contains_ip?(end_ip)
end

现在您需要向IpPool添加一些有用的方法来支持ip范围查询:

# Check if any IpPool in database contains passed ip
# `all` usage maybe slow if you have lots of records in IpPool table
def self.contains_ip?(ip)
  all.any? { |pool| pool.contains?(ip) }
end

# Check if current IpPool contains passed ip
def contains_ip?(ip)
  to_addr.include? ip
end

# This returns an IPAddr range like this:
# #<IPAddr: IPv4:10.10.10.10/255.255.255.255>..#<IPAddr: IPv4:10.10.10.20/255.255.255.255>
def to_addr
  AddrIP.new(start_ip)..AddrIP.new(end_ip)
end

那应该是它。这可行,因为IPAddr支持范围,您可以查询ip是否在简单include?检查的范围内:

>> range = IPAddr.new('10.10.10.10')..IPAddr.new('10.10.10.20')
=> #<IPAddr: IPv4:10.10.10.10/255.255.255.255>..#<IPAddr: IPv4:10.10.10.20/255.255.255.255>
>> range.include? '10.10.10.15'
=> true
>> range.include? '10.10.10.25'
=> false

享受! :)