Sha256: a2b102afedd9ee07fdedf2ade4500f3e300e9ac09f4785ba98f8f89a37aaffc7

Contents?: true

Size: 945 Bytes

Versions: 3

Compression:

Stored size: 945 Bytes

Contents

class MacAddressValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless self.class.valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.mac_address'))
    end
  end

  private

  def self.validate_format(mac_address)
    !!(mac_address =~ /^([\h]{2}:){5}[\h]{2}?$/i) ||          # 08:00:2b:01:02:03
    !!(mac_address =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) ||  # 08-00-2b-01-02-03 or 08.00.2b.01.02.03
    !!(mac_address =~ /^([\h]{6})[-|\.][\h]{6}?$/i) ||        # 08002b-010203 or 08002b.010203
    !!(mac_address =~ /^([\h]{6}):[\h]{6}?$/i) ||             # 08002b:010203
    !!(mac_address =~ /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i) ||  # 0800.2b01.0203 or 0800-2b01-0203 0800 2b01 0203
    !!(mac_address =~ /^[\h]{12}?$/i)                         # 08002b010203
  end

  def self.valid?(mac_address, options)
    validate_format(mac_address)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flash_validators-1.1.0 lib/flash_validators/validators/mac_address_validator.rb
flash_validators-1.0.0 lib/flash_validators/validators/mac_address_validator.rb
flash_validators-0.0.1 lib/flash_validators/validators/mac_address_validator.rb