Sha256: 8a1e045e2d39ae3af318036cbabb3d685b9a9864590891b1396a645173449023

Contents?: true

Size: 702 Bytes

Versions: 2

Compression:

Stored size: 702 Bytes

Contents

class SubnetValidator < 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::Subnet.new(value).valid?
  end
end

module ValidatesHost
  class Subnet
    def initialize(subnet)
      @subnet = subnet
    end

    def valid?
      return true if @subnet.blank?
      @subnet =~ /^([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}\/(2[4-9]|30)$/
    end

    def subnet
      @subnet
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validates_host-0.3.1 lib/validates_host/subnet_validator.rb
validates_host-0.3.0 lib/validates_host/subnet_validator.rb