X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FInflateColumn%2FIP.pm;h=a37dfa1d351fec0c444dfc124c73850d122ff2e3;hb=5da3e14747f34ab3fccb870e513f0b0ae762851f;hp=ea7ed8136f2b6b5cf2f37b0293d6f5ddc402a2c2;hpb=3a889a03955999c38eb4a152974522994eef72e4;p=dbsrgits%2FDBIx-Class-InflateColumn-IP.git diff --git a/lib/DBIx/Class/InflateColumn/IP.pm b/lib/DBIx/Class/InflateColumn/IP.pm index ea7ed81..a37dfa1 100644 --- a/lib/DBIx/Class/InflateColumn/IP.pm +++ b/lib/DBIx/Class/InflateColumn/IP.pm @@ -3,7 +3,7 @@ package DBIx::Class::InflateColumn::IP; use warnings; use strict; -our $VERSION = '0.02000'; +our $VERSION = '0.02001'; use base qw/DBIx::Class/; __PACKAGE__->mk_classdata(ip_format => 'addr'); @@ -22,7 +22,7 @@ appropriate format. __PACKAGE__->load_components(qw/InflateColumn::IP Core/); __PACKAGE__->add_columns( ip_address => { - data_type => 'integer', + data_type => 'bigint', is_nullable => 0, is_ip => 1, ip_format => 'numeric', @@ -46,6 +46,12 @@ Then you can treat the specified column as a NetAddr::IP object. print 'IP address: ', $host->ip_address->addr; print 'Address type: ', $host->ip_address->iptype; +DBIx::Class::InflateColumn::IP supports a limited amount of +auto-detection of the format based on the column type. If the type +begins with C or C, it's assumed to be numeric, while +C and C (as used by e.g. PostgreSQL) are assumed to be +C format. + =head1 METHODS =head2 ip_class @@ -86,8 +92,9 @@ sub register_column { return unless defined $info->{'is_ip'}; - my $ip_format = $info->{ip_format} || $self->ip_format || 'addr'; - my $ip_class = $info->{ip_class} || $self->ip_class || 'NetAddr::IPf'; + my $ip_format = $info->{ip_format} || _default_format($info->{data_type}) + || $self->ip_format || 'addr'; + my $ip_class = $info->{ip_class} || $self->ip_class || 'NetAddr::IP'; eval "use $ip_class"; $self->throw_exception("Error loading $ip_class: $@") if $@; @@ -102,9 +109,22 @@ sub register_column { ); } +my @format_map = ( + { type => qr/^(?:big)?int/i, format => 'numeric' }, + { type => qr{^(?:inet|cidr)$}i, format => 'cidr' }, +); + +sub _default_format { + my ($type) = @_; + + for my $match (@format_map) { + return $match->{format} if $type =~ $match->{type}; + } +} + =head1 AUTHOR -Dagfinn Ilmari MannsÃ¥ker, C<< >> +Dagfinn Ilmari Mannsåker, C<< >> =head1 BUGS @@ -148,7 +168,7 @@ L, L =head1 COPYRIGHT & LICENSE -Copyright 2007 Dagfinn Ilmari MannsÃ¥ker, all rights reserved. +Copyright 2007 Dagfinn Ilmari Mannsåker, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.