brainstorm
2006-Nov-02 16:02 UTC
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
brainstorm
2006-Nov-07 16:28 UTC
Re: Validating and saving a MAC address using only one format
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> On 11/2/06, brainstorm <braincode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
brainstorm
2006-Dec-05 17:02 UTC
Re: Validating and saving a MAC address using only one format
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 On 11/7/06, brainstorm <braincode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> > > On 11/2/06, brainstorm <braincode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---