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