Sha256: 616e654de18d1eca1c374c8f0ca92a4ebc799e17ebc7104ee670f26366e51258

Contents?: true

Size: 649 Bytes

Versions: 3

Compression:

Stored size: 649 Bytes

Contents

class IpValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
    record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::Ip.new(value).valid?
  end
end

module ValidatesHost
  class Ip
    def initialize(ip)
      @ip = ip
    end

    def valid?
      return true if @ip.blank?
      @ip =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
    end

    def ip
      @ip
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validates_host-0.3.1 lib/validates_host/ip_validator.rb
validates_host-0.3.0 lib/validates_host/ip_validator.rb
validates_host-0.2.0 lib/validates_host/ip_validator.rb