| 1 | package DBIx::Class::InflateColumn::ISBN; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $VERSION = '0.04000'; | ||
| 7 | |||
| 8 | use base qw/DBIx::Class/; | ||
| 9 | __PACKAGE__->mk_classdata('isbn_class'); | ||
| 10 | __PACKAGE__->isbn_class('Business::ISBN'); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | DBIx::Class::InflateColumn::ISBN - Auto-create Business::ISBN objects from columns. | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 0.04000 | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | Load this component and declare columns as ISBNs with the appropriate format. | ||
| 23 | |||
| 24 | package Library; | ||
| 25 | __PACKAGE__->load_components(qw/InflateColumn::ISBN Core/); | ||
| 26 | __PACKAGE__->add_columns( | ||
| 27 | isbn => { | ||
| 28 | data_type => 'varchar', | ||
| 29 | size => 13, | ||
| 30 | is_nullable => 0, | ||
| 31 | is_isbn => 1, | ||
| 32 | as_string => 0, | ||
| 33 | } | ||
| 34 | ); | ||
| 35 | |||
| 36 | It has to be a varchar rather than a simple integer given that it is possible | ||
| 37 | for ISBNs to contain the character X. Old style ISBNs are 10 characters, not | ||
| 38 | including hyphens, but new style ones are 13 characters. | ||
| 39 | |||
| 40 | The C<as_string> attribute is optional, and if set to 1 then values will be | ||
| 41 | stored in the database with hyphens in the appopriate places. In this case, an | ||
| 42 | extra 3 characters will be required. | ||
| 43 | |||
| 44 | Then you can treat the specified column as a Business::ISBN object. | ||
| 45 | |||
| 46 | print 'ISBN: ', $book->isbn->as_string; | ||
| 47 | print 'Publisher code: ', $book->isbn->publisher_code; | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =head2 isbn_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 Business::ISBN and only that is currently supported. | ||
| 61 | |||
| 62 | =head2 register_column | ||
| 63 | |||
| 64 | Chains with L<DBIx::Class::Row/register_column>, and sets up ISBN columns | ||
| 65 | appropriately. This would not normally be called directly by end users. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub register_column { | ||
| 70 | my ($self, $column, $info, @rest) = @_; | ||
| 71 | $self->next::method($column, $info, @rest); | ||
| 72 | |||
| 73 | return unless defined $info->{'is_isbn'}; | ||
| 74 | |||
| 75 | if ( $info->{'size'} && ($info->{'size'} < 10 || | ||
| 76 | ($info->{'as_string'} && $info->{'size'} < 13)) ) { | ||
| 77 | $self->throw_exception("ISBN field datatype is too small"); | ||
| 78 | } | ||
| 79 | $self->throw_exception("ISBN field datatype must not be integer") | ||
| 80 | if ($info->{'data_type'} eq 'integer'); | ||
| 81 | |||
| 82 | my $isbn_class = $info->{'isbn_class'} || $self->isbn_class || 'Business::ISBN'; | ||
| 83 | |||
| 84 | eval "use $isbn_class"; | ||
| 85 | $self->throw_exception("Error loading $isbn_class: $@") if $@; | ||
| 86 | |||
| 87 | $self->inflate_column( | ||
| 88 | $column => { | ||
| 89 | inflate => sub { | ||
| 90 | return $isbn_class->new(sprintf("%010s", shift)); | ||
| 91 | }, | ||
| 92 | deflate => sub { | ||
| 93 | $info->{'as_string'} ? shift->as_string : shift->isbn; | ||
| 94 | }, | ||
| 95 | } | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | |||
| 99 | =head1 AUTHOR | ||
| 100 | |||
| 101 | K. J. Cheetham C<< <jamie @ shadowcatsystems.co.uk> >> | ||
| 102 | |||
| 103 | =head1 BUGS | ||
| 104 | |||
| 105 | Please report any bugs or feature requests to | ||
| 106 | C<bug-dbix-class-inflatecolumn-ISBN at rt.cpan.org>, or through the web interface at | ||
| 107 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-ISBN>. | ||
| 108 | I will be notified, and then you'll automatically be notified of progress on | ||
| 109 | your bug as I make changes. | ||
| 110 | |||
| 111 | =head1 SUPPORT | ||
| 112 | |||
| 113 | You can find documentation for this module with the perldoc command. | ||
| 114 | |||
| 115 | perldoc DBIx::Class::InflateColumn::ISBN | ||
| 116 | |||
| 117 | You can also look for information at: | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 122 | |||
| 123 | L<http://annocpan.org/dist/DBIx-Class-InflateColumn-ISBN> | ||
| 124 | |||
| 125 | =item * CPAN Ratings | ||
| 126 | |||
| 127 | L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-ISBN> | ||
| 128 | |||
| 129 | =item * RT: CPAN's request tracker | ||
| 130 | |||
| 131 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-ISBN> | ||
| 132 | |||
| 133 | =item * Search CPAN | ||
| 134 | |||
| 135 | L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-ISBN> | ||
| 136 | |||
| 137 | =back | ||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | L<Business::ISBN> | ||
| 142 | |||
| 143 | L<DBIx::Class::InflateColumn> | ||
| 144 | |||
| 145 | L<WWW::Scraper::ISBN> | ||
| 146 | |||
| 147 | =head1 COPYRIGHT & LICENSE | ||
| 148 | |||
| 149 | Copyright 2007 K. J. Cheetham, all rights reserved. | ||
| 150 | |||
| 151 | This program is free software; you can redistribute it and/or modify it | ||
| 152 | under the same terms as Perl itself. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | 1; # End of DBIx::Class::InflateColumn::ISBN |