blib/lib/DBIx/Class/Tokenize.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 54 CC 6 SCAR 23.0
CC
6
Cov
0%
register_column
20.0
insert
6.0
tokenize
6.0
CRAP 10.0
1 package DBIx::Class::Tokenize;
2  
3 use strict;
4 use warnings;
5  
6 use base qw/DBIx::Class/;
7  
8 __PACKAGE__->mk_classdata( '__columns_to_tokenize' => {} );
9  
10 our $VERSION = '0.01';
11  
12 =head1 NAME
13  
14 DBIx::Class::Tokenize - Automatically tokenize a column on creation
15  
16 =head1 DESCRIPTION
17  
18 This component simply creates a clean token based on a field on insertion. The
19 simple use case is having a long name that is displayable, like "Catalyst Book"
20 that you want to change to "catalyst_book". Rather than do that by hand
21 every time you create a record, this component does it for you.
22  
23 =head1 SYNOPSIS
24  
25 package MyApp::Schema::Book;
26  
27 __PACKAGE__->load_components( qw(Tokenize ... Core) );
28 __PACKAGE__->add_columns(
29 id => { data_type => 'integer', is_auto_increment => 1 },
30 name => { data_type => 'varchar', size => 128,
31 # Update the 'token' field on create
32 token_field => 'token' },
33 token => { data_type => 'varchar', size => 128, is_nullable => 0 }
34 );
35  
36 ...
37  
38 my $row = $schema->resultset('Book')->create({ name => "Catalyst Book" });
39
40 print $row->token; # Prints "catalyst_book
41  
42 =cut
43  
44 sub register_column {
45 my ( $self, $column, $info, @rest ) = @_;
46
47 $self->next::method($column, $info, @rest);
48 return unless $info->{token_field};
49 return unless defined($info->{data_type});
50 return unless $info->{data_type} =~ /^(var)?char$/i;
51  
52 my $token = $info->{token_field} || 'token';
53  
54 $self->__columns_to_tokenize->{$column} = $token;
55 }
56  
57 sub insert {
58 my ( $self, $attrs ) = ( shift, shift );
59  
60 foreach my $key ( keys %{ $self->__columns_to_tokenize } ) {
61 my $dest = $self->__columns_to_tokenize->{$key};
62 # Don't overwrite if there is something already there
63 next if defined $self->get_column($dest);
64 $self->$dest( $self->tokenize( $key ) );
65 }
66 $self->next::method(@_);
67 }
68  
69 =head1 METHODS
70  
71 =head2 tokenize
72  
73 This method is what performs the actual conversion to the tokenized form. It is
74 easy to override so that you can change things around to suit your particular
75 table. Whatever is returned is inserted into the configured C<token_field>.
76  
77 An example of extending this method would be to traverse a tree in a row
78 that uses L<DBIx::Class::Tree::AdjacencyList> and tokenize the parents as well.
79  
80 =cut
81  
82 sub tokenize {
83 my ( $self, $key ) = @_;
84
85 my $field = $self->get_column($key);
86  
87 # Should we throw an exception, or just return undef?
88 return undef unless $field;
89  
90 $field = lc($field);
91 $field =~ s/\s+/_/g;
92 $field =~ s/[^\w]/_/g;
93 return $field;
94 }
95  
96 =head1 AUTHOR
97  
98 J. Shirley, C<< <jshirley at coldhardcode.com> >>
99  
100 =head1 BUGS
101  
102 Please report any bugs or feature requests to C<bug-catalyst-controller-rest-dbi
103 c-item at rt.cpan.org>, or through
104 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Tokenize>. I will be notified, and then you'll automatically be notified of
105 progress on your bug as I make changes.
106  
107 =head1 SUPPORT
108  
109 You can find documentation for this module with the perldoc command.
110  
111 perldoc DBIx::Class::Tokenize
112  
113 You can also look for information at:
114  
115 =over 4
116  
117 =item * RT: CPAN's request tracker
118  
119 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Tokenize>
120  
121 =item * AnnoCPAN: Annotated CPAN documentation
122  
123 L<http://annocpan.org/dist/DBIx-Class-Tokenize>
124  
125 =item * CPAN Ratings
126  
127 L<http://cpanratings.perl.org/d/DBIx-Class-Tokenize>
128  
129 =item * Search CPAN
130  
131 L<http://search.cpan.org/dist/DBIx-Class-Tokenize>
132  
133 =back
134  
135  
136 =head1 ACKNOWLEDGEMENTS
137  
138 This is a Cold Hard Code, LLC module - http://www.coldhardcode.com
139  
140 =head1 COPYRIGHT & LICENSE
141  
142 Copyright 2008 Cold Hard Code, LLC, all rights reserved.
143  
144 This program is free software; you can redistribute it and/or modify it
145 under the same terms as Perl itself.
146  
147 =cut
148  
149 1;