Validating and saving a MAC address using only one format

Hi people,

I’m developing my first rails app, and I’m having a little issue with
model validations:

==
require_gem ‘netaddr’

(…) (AR model definition)

protected
def validate
@eui = NetAddr::EUI.create(mac).address(:Delimiter=>’:’)
#@eui.address(:Delimiter=> ‘.’)
rescue NetAddr::ValidationError
errors.add(mac, “Incorrect MAC address format”)
end

My idea is to ensure at the model level, that a MAC address is
properly filtered and saved using the following format:

0000.1111.2222

… and automatically convert the following netaddr-supported formats
to the above one:

00:00:11:11:22:22
00-00-11-11-22-22
(…)

Now when I try to add a MAC using any format, it saves ‘000011112222’
on the DB and I cannot figure why it’s happening by looking at netaddr
code.

NetAddr uses gsub!(/[.:-]/, ‘’) all around to remove formating
chars, but ".address(:Delimiter =>’.’) " should put ‘.’-format back to
the MAC string, shouldn’t it ?

Thanks in advance.

When tried standalone (outside rails), it works like a charm :_/ What
i’m doing wrong with exception management ?

irb(main):001:0> require_gem ‘netaddr’
=> true
irb(main):002:0>
NetAddr::EUI.create(‘00:00:11:11:22:22’).address(:Delimiter=>’.’)
=> “0000.1111.2222”
irb(main):003:0>
NetAddr::EUI.create(‘00:00:11:11:22:22’).address(:Delimiter=>’-’)
=> “00-00-11-11-22-22”
irb(main):004:0>
NetAddr::EUI.create(‘00:00:11:11:22:22’).address(:Delimiter=>’:’)
=> “00:00:11:11:22:22”
irb(main):005:0>

Also tried with ActiveRecord callbacks used in the model:

def after_validation
self.mac = NetAddr::EUI.create(mac).address(:Delimiter=>’.’)
rescue NetAddr::ValidationError
errors.add(mac,“Incorrect MAC address format”)
end

But the ValidationError never raises :-/ Any bet ?

Thanks