class MacAddressValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value) record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.mac_address')) end end private def valid_format?(value) (value =~ /^([\h]{2}:){5}[\h]{2}?$/i) || (value =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) || (value =~ /^([\h]{6})[-|\.][\h]{6}?$/i) || (value =~ /^([\h]{6}):[\h]{6}?$/i) || (value =~ /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i) || (value =~ /^[\h]{12}?$/i) end def valid?(value) valid_format?(value) end end