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   340868 use v5.10.1;
  2         8  
6              
7 2     2   12 use strict;
  2         5  
  2         42  
8 2     2   10 use warnings;
  2         4  
  2         59  
9              
10 2     2   963 use Ref::Util ();
  2         3323  
  2         50  
11 2     2   14 use Sub::Quote ();
  2         4  
  2         991  
12              
13             # RECOMMEND PREREQ: Ref::Util::XS
14              
15             our $VERSION = 'v0.1.6';
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 533884 my ( $self, @cols ) = @_;
24              
25 8         44 $self->next::method(@cols);
26              
27 8   33     4472 my $class = Ref::Util::is_ref($self) || $self;
28              
29 8         24 foreach my $col (@cols) {
30              
31 42 100       2729 next if ref $col;
32              
33 23         66 $col =~ s/^\+//;
34 23         534 my $info = $self->column_info($col);
35              
36 23 100       2117 next unless $info->{data_type} eq 'enum';
37              
38 20 50       54 next unless exists $info->{extra}{list};
39              
40 20   100 25   102 my $handlers = $info->{extra}{handles} //= sub { "is_" . $_[0] };
  25         72  
41              
42 20 50       50 next unless $handlers;
43              
44 20 100       58 if ( Ref::Util::is_plain_coderef($handlers) ) {
45             $info->{extra}{handles} = {
46             map {
47              
48 40 100       114 if ( my $method = $handlers->( $_, $col, $class ) ) {
49 35         218 ( $method => $_ )
50             }
51             else {
52             ()
53 5         63 }
54              
55 15         25 } @{ $info->{extra}{list} }
  15         40  
56             };
57 15         79 $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         97 foreach my $handler ( keys %$handlers ) {
64 45 50       3577 next unless $handler;
65 45 50       109 my $value = $handlers->{$handler} or next;
66              
67 45         111 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     632 if $self->can($method) && !$MINE{$method};
76              
77             my $code =
78             $info->{is_nullable}
79 45 100       201 ? 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         118 $MINE{$method} = 1;
84 45         116 Sub::Quote::quote_sub $method, $code;
85              
86             }
87              
88             }
89              
90 8         52 return $self;
91             }
92              
93              
94             1;
95              
96             __END__