Debian Codegraph @

lib/Class/DBI/Sweet/Topping.pm

raw
1 package Class::DBI::Sweet::Topping;
2
3 use strict;
4 use Class::DBI::Sweet;
5
6 # Alias
7 *Class::DBI::Sweet::find = \&Class::DBI::Sweet::search;
8
9 sub Class::DBI::Sweet::AUTOLOAD {
10 my $self = shift;
11 our $AUTOLOAD = $Class::DBI::Sweet::AUTOLOAD;
12 my $super_meth = $AUTOLOAD;
13 $super_meth =~ s/(?:.*::)?/SUPER::/;
14 $AUTOLOAD =~ m/(search|find|page|count|(?:previous|next)_by)_(.*)/
15 || return $self->$super_meth(@_);
16 my $method = $1 || '';
17 my $query = $2 || '';
18 return 0 unless $query;
19 my @keys = split /_and_/, $query;
20 my %con;
21
22 for my $key (@keys) {
23 if ($method =~ /_by/) {
24 $con{$key} = $self->$key; # previous/next_by conds are from self
25 } else {
26 $con{$key} = shift || '';
27 }
28 }
29
30 $method =~ s/(.*)_by/retrieve_$1/; # Convert next_by to retrieve_next
31
32 my $attrs = shift || {};
33
34 return wantarray()
35 ? @{[$self->$method( \%con, $attrs )]}
36 : $self->$method( \%con, $attrs );
37 }
38
39 1;
40
41 __END__
42
43 =head1 NAME
44
45 Class::DBI::Sweet::Topping - Topping for Class::DBI::Sweet
46
47 =head1 SYNOPSIS
48
49 MyApp::Article->find_title_and_created_on( $title, $created_on );
50
51 MyApp::Article->search_title_and_created_on( $title, $created_on );
52
53 MyApp::Article->count_title_and_created_on( $title, $created_on );
54
55 MyApp::Article->page_title_and_created_on( $title, $created_on );
56
57 MyApp::Article->next_by_created_by( { order_by => 'created_on' } );
58
59 MyApp::Article->previous_by_created_by( { order_by => 'created_on' } );
60
61 =head1 DESCRIPTION
62
63 Class::DBI::Sweet::Topping provides a convenient AUTOLOAD for search, page,
64 retrieve_next and retrieve_previous.
65
66 =head1 AUTHORS
67
68 Christian Hansen <[email protected]>
69
70 Matt S Trout <[email protected]>
71
72 Sebastian Riedel <[email protected]>
73
74 =head1 THANKS TO
75
76 Danijel Milicevic, Jesse Sheidlower, Marcus Ramberg, Viljo Marrandi
77
78 =head1 SUPPORT
79
80 #catalyst on L<irc://irc.perl.org>
81
82 L<http://lists.rawmode.org/mailman/listinfo/catalyst>
83
84 L<http://lists.rawmode.org/mailman/listinfo/catalyst-dev>
85
86 =head1 LICENSE
87
88 This library is free software; you can redistribute it and/or modify
89 it under the same terms as Perl itself.
90
91 =head1 SEE ALSO
92
93 L<Class::DBI::Sweet>
94
95 L<Catalyst>
96
97 =cut