File Coverage

blib/lib/DBIx/Class/Helper/Row/Enumeration.pm
Criterion Covered Total %
statement 43 43 100.0
branch 16 22 72.7
condition 5 8 62.5
subroutine 7 7 100.0
pod 0 1 0.0
total 71 81 87.6


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Row::Enumeration;
2              
3             # ABSTRACT: Add methods for emum values
4              
5 2     2   334360 use v5.10.1;
  2         8  
6              
7 2     2   12 use strict;
  2         4  
  2         40  
8 2     2   11 use warnings;
  2         5  
  2         58  
9              
10 2     2   1138 use Ref::Util ();
  2         3402  
  2         63  
11 2     2   15 use Sub::Quote ();
  2         5  
  2         922  
12              
13             # RECOMMEND PREREQ: Ref::Util::XS
14              
15             our $VERSION = 'v0.1.8';
16              
17             # The names of all methods installed by this module.
18             my %MINE;
19              
20              
21              
22             sub add_columns {
23 8     8 0 524769 my ( $self, @cols ) = @_;
24              
25 8         41 $self->next::method(@cols);
26              
27 8   33     6564 my $class = Ref::Util::is_ref($self) || $self;
28              
29 8         22 foreach my $col (@cols) {
30              
31 42 100       2580 next if ref $col;
32              
33 23         63 $col =~ s/^\+//;
34 23         525 my $info = $self->column_info($col);
35              
36 23 100       2214 next unless $info->{data_type} eq 'enum';
37              
38 20 50       56 next unless exists $info->{extra}{list};
39              
40 20   100 25   99 my $handlers = $info->{extra}{handles} //= sub { "is_" . $_[0] };
  25         72  
41              
42 20 50       50 next unless $handlers;
43              
44 20 100       57 if ( Ref::Util::is_plain_coderef($handlers) ) {
45             $info->{extra}{handles} = {
46             map {
47              
48 40 100       118 if ( my $method = $handlers->( $_, $col, $class ) ) {
49 35         226 ( $method => $_ )
50             }
51             else {
52             ()
53 5         67 }
54              
55 15         27 } @{ $info->{extra}{list} }
  15         37  
56             };
57 15         54 $handlers = $info->{extra}{handles};
58             }
59              
60 20 50       53 DBIx::Class::Exception->throw("handles is not a hashref")
61             unless Ref::Util::is_plain_hashref($handlers);
62              
63 20         76 foreach my $handler ( keys %$handlers ) {
64 45 50       3408 next unless $handler;
65 45 50       111 my $value = $handlers->{$handler} or next;
66              
67 45         127 my $method = "${class}::${handler}";
68              
69             # Keep track of what we've installed, and don't complain about
70             # being asked to reinstall it. This is needed when using
71             # DBIx::Class::Schema::Loader. In theory we should check whether
72             # the current method is the one we installed, and throw anyway if
73             # it isn't, but this seems adequate.
74             DBIx::Class::Exception->throw("${method} is already defined")
75 45 50 66     610 if $self->can($method) && !$MINE{$method};
76              
77             my $code =
78             $info->{is_nullable}
79 45 100       200 ? qq{ my \$val = \$_[0]->get_column("${col}"); }
80             . qq{ defined(\$val) && \$val eq "${value}" }
81             : qq{ \$_[0]->get_column("${col}") eq "${value}" };
82              
83 45         153 $MINE{$method} = 1;
84 45         116 Sub::Quote::quote_sub $method, $code;
85              
86             }
87              
88             }
89              
90 8         58 return $self;
91             }
92              
93              
94             1;
95              
96             __END__