6153eb2f9ecaad83b09a8fd57fee6161dd14f0bc
[dbsrgits/DBIx-Class-InflateColumn-IP.git] / lib / DBIx / Class / InflateColumn / IP.pm
1 package DBIx::Class::InflateColumn::IP;
2
3 use warnings;
4 use strict;
5 use 5.008001;
6
7 our $VERSION = '0.02002';
8
9 use base qw/DBIx::Class/;
10 __PACKAGE__->mk_classdata(ip_format => 'addr');
11 __PACKAGE__->mk_classdata(ip_class  => 'NetAddr::IP');
12
13 =head1 NAME
14
15 DBIx::Class::InflateColumn::IP - Auto-create NetAddr::IP objects from columns.
16
17 =head1 SYNOPSIS
18
19 Load this component and declare columns as IP addresses with the
20 appropriate format.
21
22     package Host;
23     __PACKAGE__->load_components(qw/InflateColumn::IP Core/);
24     __PACKAGE__->add_columns(
25         ip_address => {
26             data_type => 'bigint',
27             is_nullable => 0,
28             is_ip => 1,
29             ip_format => 'numeric',
30         }
31     );
32
33     package Network;
34     __PACKAGE__->load_components(qw/InflateColumn::IP Core/);
35     __PACKAGE__->add_columns(
36         address => {
37             data_type => 'varchar',
38             size        => 18
39             is_nullable => 0,
40             is_ip => 1,
41             ip_format => 'cidr',
42         }
43     );
44
45 Then you can treat the specified column as a NetAddr::IP object.
46
47     print 'IP address: ', $host->ip_address->addr;
48     print 'Address type: ', $host->ip_address->iptype;
49
50 DBIx::Class::InflateColumn::IP supports a limited amount of
51 auto-detection of the format based on the column type. If the type
52 begins with C<int> or C<bigint>, it's assumed to be numeric, while
53 C<inet> and C<cidr> (as used by e.g. PostgreSQL) are assumed to be
54 C<cidr> format.
55
56 =head1 METHODS
57
58 =head2 ip_class
59
60 =over
61
62 =item Arguments: $class
63
64 =back
65
66 Gets/sets the address class that the columns should be inflated into.
67 The default class is NetAddr::IP.
68
69 =head2 ip_format
70
71 =over
72
73 =item Arguments: $format
74
75 =back
76
77 Gets/sets the name of the method used to deflate the address for the
78 database. This must return a value suitable for C<$ip_class->new(); The
79 default format is C<addr>, which returns the address in dotted-quad
80 notation. See L<NetAddr::IP/Methods> for suitable values.
81
82 =head2 register_column
83
84 Chains with L<DBIx::Class::Row/register_column>, and sets up IP address
85 columns appropriately. This would not normally be called directly by end
86 users.
87
88 =cut
89
90 sub register_column {
91     my ($self, $column, $info, @rest) = @_;
92     $self->next::method($column, $info, @rest);
93
94     return unless defined $info->{'is_ip'};
95
96     my $ip_format = $info->{ip_format} || _default_format($info->{data_type})
97         || $self->ip_format || 'addr';
98     my $ip_class = $info->{ip_class} || $self->ip_class || 'NetAddr::IP';
99
100     eval "use $ip_class";
101     $self->throw_exception("Error loading $ip_class: $@") if $@;
102     $self->throw_exception("Format '$ip_format' not supported by $ip_class")
103         unless $ip_class->can($ip_format);
104
105     $self->inflate_column(
106         $column => {
107             inflate => sub { return $ip_class->new(shift); },
108             deflate => sub { return scalar shift->$ip_format; },
109         }
110     );
111 }
112
113 my @format_map = (
114   { type => qr/^(?:big)?int/i, format => 'numeric' },
115   { type => qr{^(?:inet|cidr)$}i, format => 'cidr' },
116 );
117
118 sub _default_format {
119     my ($type) = @_;
120
121     for my $match (@format_map) {
122         return $match->{format} if $type =~ $match->{type};
123     }
124 }
125
126 =head1 AUTHOR
127
128 Dagfinn Ilmari MannsÃ¥ker, C<< <ilmari at ilmari.org> >>
129
130 =head1 BUGS
131
132 Please report any bugs or feature requests to
133 C<bug-dbix-class-inflatecolumn-ip at rt.cpan.org>, or through the web interface at
134 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-IP>.
135 I will be notified, and then you'll automatically be notified of progress on
136 your bug as I make changes.
137
138 =head1 SUPPORT
139
140 You can find documentation for this module with the perldoc command.
141
142     perldoc DBIx::Class::InflateColumn::IP
143
144 You can also look for information at:
145
146 =over 4
147
148 =item * AnnoCPAN: Annotated CPAN documentation
149
150 L<http://annocpan.org/dist/DBIx-Class-InflateColumn-IP>
151
152 =item * CPAN Ratings
153
154 L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-IP>
155
156 =item * RT: CPAN's request tracker
157
158 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-IP>
159
160 =item * Search CPAN
161
162 L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-IP>
163
164 =back
165
166 =head1 SEE ALSO
167
168 L<DBIx::Class>, L<NetAddr::IP>
169
170 =head1 COPYRIGHT & LICENSE
171
172 Copyright 2007 Dagfinn Ilmari MannsÃ¥ker, all rights reserved.
173
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 =cut
178
179 1; # End of DBIx::Class::InflateColumn::IP