initial import of DBIx-Class-InflateColumn-IP 0.02000 from CPAN
[dbsrgits/DBIx-Class-InflateColumn-IP.git] / lib / DBIx / Class / InflateColumn / IP.pm
CommitLineData
3a889a03 1package DBIx::Class::InflateColumn::IP;
2
3use warnings;
4use strict;
5
6our $VERSION = '0.02000';
7
8use base qw/DBIx::Class/;
9__PACKAGE__->mk_classdata(ip_format => 'addr');
10__PACKAGE__->mk_classdata(ip_class => 'NetAddr::IP');
11
12=head1 NAME
13
14DBIx::Class::InflateColumn::IP - Auto-create NetAddr::IP objects from columns.
15
16=head1 SYNOPSIS
17
18Load this component and declare columns as IP addresses with the
19appropriate 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
44Then 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=head1 METHODS
50
51=head2 ip_class
52
53=over
54
55=item Arguments: $class
56
57=back
58
59Gets/sets the address class that the columns should be inflated into.
60The default class is NetAddr::IP.
61
62=head2 ip_format
63
64=over
65
66=item Arguments: $format
67
68=back
69
70Gets/sets the name of the method used to deflate the address for the
71database. This must return a value suitable for C<$ip_class->new(); The
72default format is C<addr>, which returns the address in dotted-quad
73notation. See L<NetAddr::IP/Methods> for suitable values.
74
75=head2 register_column
76
77Chains with L<DBIx::Class::Row/register_column>, and sets up IP address
78columns appropriately. This would not normally be called directly by end
79users.
80
81=cut
82
83sub register_column {
84 my ($self, $column, $info, @rest) = @_;
85 $self->next::method($column, $info, @rest);
86
87 return unless defined $info->{'is_ip'};
88
89 my $ip_format = $info->{ip_format} || $self->ip_format || 'addr';
90 my $ip_class = $info->{ip_class} || $self->ip_class || 'NetAddr::IPf';
91
92 eval "use $ip_class";
93 $self->throw_exception("Error loading $ip_class: $@") if $@;
94 $self->throw_exception("Format '$ip_format' not supported by $ip_class")
95 unless $ip_class->can($ip_format);
96
97 $self->inflate_column(
98 $column => {
99 inflate => sub { return $ip_class->new(shift); },
100 deflate => sub { return scalar shift->$ip_format; },
101 }
102 );
103}
104
105=head1 AUTHOR
106
107Dagfinn Ilmari Mannsåker, C<< <ilmari at ilmari.org> >>
108
109=head1 BUGS
110
111Please report any bugs or feature requests to
112C<bug-dbix-class-inflatecolumn-ip at rt.cpan.org>, or through the web interface at
113L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-IP>.
114I will be notified, and then you'll automatically be notified of progress on
115your bug as I make changes.
116
117=head1 SUPPORT
118
119You can find documentation for this module with the perldoc command.
120
121 perldoc DBIx::Class::InflateColumn::IP
122
123You can also look for information at:
124
125=over 4
126
127=item * AnnoCPAN: Annotated CPAN documentation
128
129L<http://annocpan.org/dist/DBIx-Class-InflateColumn-IP>
130
131=item * CPAN Ratings
132
133L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-IP>
134
135=item * RT: CPAN's request tracker
136
137L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-IP>
138
139=item * Search CPAN
140
141L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-IP>
142
143=back
144
145=head1 SEE ALSO
146
147L<DBIx::Class>, L<NetAddr::IP>
148
149=head1 COPYRIGHT & LICENSE
150
151Copyright 2007 Dagfinn Ilmari Mannsåker, all rights reserved.
152
153This program is free software; you can redistribute it and/or modify it
154under the same terms as Perl itself.
155
156=cut
157
1581; # End of DBIx::Class::InflateColumn::IP