initial import of DBIx-Class-InflateColumn-IP 0.02000 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.02000';
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 =head1 METHODS
50
51 =head2 ip_class
52
53 =over
54
55 =item Arguments: $class
56
57 =back
58
59 Gets/sets the address class that the columns should be inflated into.
60 The default class is NetAddr::IP.
61
62 =head2 ip_format
63
64 =over
65
66 =item Arguments: $format
67
68 =back
69
70 Gets/sets the name of the method used to deflate the address for the
71 database. This must return a value suitable for C<$ip_class->new(); The
72 default format is C<addr>, which returns the address in dotted-quad
73 notation. See L<NetAddr::IP/Methods> for suitable values.
74
75 =head2 register_column
76
77 Chains with L<DBIx::Class::Row/register_column>, and sets up IP address
78 columns appropriately. This would not normally be called directly by end
79 users.
80
81 =cut
82
83 sub 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
107 Dagfinn Ilmari MannsÃ¥ker, C<< <ilmari at ilmari.org> >>
108
109 =head1 BUGS
110
111 Please report any bugs or feature requests to
112 C<bug-dbix-class-inflatecolumn-ip at rt.cpan.org>, or through the web interface at
113 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-IP>.
114 I will be notified, and then you'll automatically be notified of progress on
115 your bug as I make changes.
116
117 =head1 SUPPORT
118
119 You can find documentation for this module with the perldoc command.
120
121     perldoc DBIx::Class::InflateColumn::IP
122
123 You can also look for information at:
124
125 =over 4
126
127 =item * AnnoCPAN: Annotated CPAN documentation
128
129 L<http://annocpan.org/dist/DBIx-Class-InflateColumn-IP>
130
131 =item * CPAN Ratings
132
133 L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-IP>
134
135 =item * RT: CPAN's request tracker
136
137 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-IP>
138
139 =item * Search CPAN
140
141 L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-IP>
142
143 =back
144
145 =head1 SEE ALSO
146
147 L<DBIx::Class>, L<NetAddr::IP>
148
149 =head1 COPYRIGHT & LICENSE
150
151 Copyright 2007 Dagfinn Ilmari MannsÃ¥ker, all rights reserved.
152
153 This program is free software; you can redistribute it and/or modify it
154 under the same terms as Perl itself.
155
156 =cut
157
158 1; # End of DBIx::Class::InflateColumn::IP