File Coverage

blib/lib/DBIx/Class/Helper/ResultSet/EnumMethods.pm
Criterion Covered Total %
statement 11 29 37.9
branch 0 2 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 0 1 0.0
total 15 42 35.7


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::ResultSet::EnumMethods;
2              
3 1     1   137229 use 5.008;
  1         3  
4 1     1   14 use utf8;
  1         3  
  1         7  
5 1     1   459 use strictures 2;
  1         1418  
  1         32  
6              
7             =head1 NAME
8              
9             DBIx::Class::Helper::ResultSet::EnumMethods - Automatically create search methods for enum columns.
10              
11             =head1 VERSION
12              
13             Version 0.02
14              
15             =cut
16              
17             our $VERSION = '0.02';
18              
19             # Le 'install' => 1 est pour Subroutines::ProhibitCallsToUndeclaredSubs.
20 1     1   656 use Package::Variant 'install' => 1, 'importing' => ['Moo::Role'];
  1         4940  
  1         7  
21              
22             sub make_variant {
23 0     0 0   my ($class, $target_package, $resultset) = @_;
24              
25 0           my $result = $resultset;
26 0           $result =~ s/::ResultSet::/::Result::/oms;
27              
28 0           my $columns_info = $result->result_source_instance->columns_info;
29              
30 0           for my $method (keys %{$columns_info}) {
  0            
31 0           my $detail = $columns_info->{$method};
32              
33 0 0 0       if ('enum' ne $detail->{'data_type'} && $detail->{'extra'}{'list'}) {
34 0           next;
35             }
36              
37 0           for my $v (@{ $detail->{'extra'}{'list'} }) {
  0            
38              
39             install "${method}_${v}" => sub {
40 0     0     my $self = shift;
41              
42 0           return $self->search(
43             {
44             $self->me($method) => $v
45             }
46             );
47 0           };
48              
49             install "${method}_not_${v}" => sub {
50 0     0     my $self = shift;
51              
52 0           return $self->search(
53             {
54             $self->me($method) => {
55             q{!=} => $v,
56             }
57             }
58             );
59 0           };
60             }
61             }
62              
63 0           return;
64             }
65              
66             =head1 SYNOPSIS
67              
68             This module automatically creates search method helpers for enum columns.
69              
70             In your ResultSet class, add:
71              
72             use Role::Tiny::With qw(with);
73             use DBIx::Class::Helper::ResultSet::EnumMethods;
74              
75             with(EnumMethods(__PACKAGE__));
76              
77             =head1 METHODS
78              
79             Say your table has an enum column "payment_type", and the possible values for
80             the enum are "cash", "check", and "card", using this role will act as if you
81             added these methods:
82              
83             sub payment_type_cash {
84             return shift->search( {
85             $self->me('payment_type') => 'cash'
86             });
87             };
88            
89             sub payment_type_not_cash {
90             return shift->search( {
91             $self->me('payment_type') => {
92             q{!=} => 'cash',
93             }
94             });
95             };
96            
97             sub payment_type_check {
98             return shift->search( {
99             $self->me('payment_type') => 'check'
100             });
101             };
102            
103             sub payment_type_not_check {
104             return shift->search( {
105             $self->me('payment_type') => {
106             q{!=} => 'check',
107             }
108             });
109             };
110            
111             sub payment_type_card {
112             return shift->search( {
113             $self->me('payment_type') => 'card'
114             });
115             };
116            
117             sub payment_type_not_card {
118             return shift->search( {
119             $self->me('payment_type') => {
120             q{!=} => 'card',
121             }
122             });
123             };
124              
125             =head1 AUTHOR
126              
127             Mathieu Arnold, C<< <mat at cpan.org> >>
128              
129             =head1 BUGS
130              
131             Please report any bugs or feature requests to C<bug-dbix-class-helper-resultset-enummethods at rt.cpan.org>, or through
132             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Helper-ResultSet-EnumMethods>. I will be notified, and then you'll
133             automatically be notified of progress on your bug as I make changes.
134              
135              
136              
137              
138             =head1 SUPPORT
139              
140             You can find documentation for this module with the perldoc command.
141              
142             perldoc DBIx::Class::Helper::ResultSet::EnumMethods
143              
144              
145             You can also look for information at:
146              
147             =over 4
148              
149             =item * RT: CPAN's request tracker (report bugs here)
150              
151             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Helper-ResultSet-EnumMethods>
152              
153             =item * CPAN Ratings
154              
155             L<https://cpanratings.perl.org/d/DBIx-Class-Helper-ResultSet-EnumMethods>
156              
157             =item * Search CPAN
158              
159             L<https://metacpan.org/release/DBIx-Class-Helper-ResultSet-EnumMethods>
160              
161             =back
162              
163              
164             =head1 ACKNOWLEDGEMENTS
165              
166             People on #dbix-class for the original idea.
167              
168             =head1 LICENSE AND COPYRIGHT
169              
170             This software is Copyright (c) 2019 by Mathieu Arnold.
171              
172             This is free software, licensed under:
173              
174             The Artistic License 2.0 (GPL Compatible)
175              
176              
177             =cut
178              
179             1;