blib/lib/DBIx/Class/InflateColumn/Object/Enum.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 78.5% 11 / 14 Conditioncond 61.9% 13 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 76.9% 70 / 91 CC 15 SCAR 21.6
CC
15
Cov
83%
register_column
13.3
__ANON__
4.0
__ANON__
2.0
CRAP 8.7
↑ Summary
1 package DBIx::Class::InflateColumn::Object::Enum;
2 4 $DBIx::Class::InflateColumn::Object::Enum::VERSION = '0.06';
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Object::Enum;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 DBIx::Class::InflateColumn::Object::Enum - Allows a DBIx::Class user to define a Object::Enum column
11  
12 =head1 VERSION
13  
14 Version 0.03
15  
16 =cut
17  
18 # Dist::Zill should handle this now
19 # ABSTRACT: Allows a DBIx::Class user to define a Object::Enum column
20 #our $VERSION = '0.04';
21  
22  
23 =head1 SYNOPSIS
24  
25 Load this module via load_components and utilize is_enum and values property
26 to define Enumuration columns via Object::Enum
27  
28 package TableClass;
29
30 use strict;
31 use warnings;
32 use base 'DBIx::Class';
33
34 __PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
35 __PACKAGE__->table('testtable');
36 __PACKAGE__->add_columns(
37 color => {
38 data_type => 'varchar',
39 is_enum => 1,
40 extra => {
41 list => [qw/red green blue/]
42 }
43 }
44 color_native => { # works inline with native enum type
45 data_type => 'enum',
46 is_enum => 1,
47 extra => {
48 list => [qw/red green blue/]
49 }
50 }
51 );
52
53 1;
54
55 Now you may treat the column as an L<Object::Enum> object.
56
57 my $table_rs = $db->resultset('TableClass')->create({
58 color => undef
59 });
60
61 $table_rs->color->set_red; # sets color to red
62 $table_rs->color->is_red; # would return true
63 $table_rs->color->is_green; # would return false
64 print $table_rs->color->value; # would print 'red'
65 $table_rs->color->unset; # set the value to 'undef' or 'null'
66 $table_rs->color->is_red; # returns false now
67  
68 =head1 METHODS
69  
70 =head2 register_column
71  
72 Internal chained method with L<DBIx::Class::Row/register_column>.
73 Users do not call this directly!
74  
75 =cut
76  
77 30 ▶sub register_column {
Subroutine register_column: called CC=12 SCAR=25.9
Pod documented
78 30 my $self = shift;
79 30 my ($column, $info) = @_;
80
81 30 $self->next::method(@_);
82
83 30 ▶ return unless defined $info->{is_enum} and $info->{is_enum};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"is_enum"} and $info->{"is_enum"} 15 15 30
Condition66%2/3
defined $info->{"is_enum"} and $info->{"is_enum"}
!ll&&!rl&&r
15015
MC/DC50%1/2
defined $info->{"is_enum"} and $info->{"is_enum"}
defined $info->{"is_enum"} $info->{"is_enum"}
Truth table66%2/3
A and B
A= defined $info->{"is_enum"}B= $info->{"is_enum"}
ABresult
0X0
100
111
84
85 croak("Object::Enum '$column' missing 'extra => { list => [] }' column configuration")
86 unless (
87 defined $info->{extra}
88 and ref $info->{extra} eq 'HASH'
89 and defined $info->{extra}->{list}
90 15 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"extra"} and ref $info->{"extra"} eq "HASH" and defined $info->{"extra"}{"list"} 0 15 15
Condition33%2/6
defined $info->{"extra"} and ref $info->{"extra"} eq "HASH" and defined $info->{"extra"}{"list"}
!ll&&!rl&&r
0015
defined $info->{"extra"} and ref $info->{"extra"} eq "HASH"
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $info->{"extra"} and ref $info->{"extra"} eq "HASH" and defined $info->{"extra"}{"list"}
defined $info->{"extra"} ref $info->{"extra"} eq "HASH" defined $info->{"extra"}{"list"}
Truth table25%1/4
A and B and C
A= defined $info->{"extra"}B= ref $info->{"extra"} eq "HASH"C= defined $info->{"extra"}{"list"}
ABCresult
0XX0
10X0
1100
1111
91
92 croak("Object::Enum '$column' value list (extra => { list => [] }) must be an array reference")
93 15 ▶ unless ref $info->{extra}->{list} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info->{"extra"}{"list"} eq "ARRAY" 0 15 15
94  
95 croak("Object::Enum requires a default value when a column is nullable")
96 if exists $info->{is_nullable}
97 and $info->{is_nullable}
98 15 ▶ and !$info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"is_nullable"} and $info->{"is_nullable"} and not $info->{"default_value"} 0 15 15
Condition66%4/6
exists $info->{"is_nullable"} and $info->{"is_nullable"} and not $info->{"default_value"}
!ll&&!rl&&r
960
exists $info->{"is_nullable"} and $info->{"is_nullable"}
!ll&&!rl&&r
096
MC/DC0%0/3
exists $info->{"is_nullable"} and $info->{"is_nullable"} and not $info->{"default_value"}
exists $info->{"is_nullable"} $info->{"is_nullable"} not $info->{"default_value"}
Truth table50%2/4
A and B and not(C)
A= exists $info->{"is_nullable"}B= $info->{"is_nullable"}C= not $info->{"default_value"}
ABCresult
0XX0
10X0
1100
1111
99  
100 15 my $values = $info->{extra}->{list};
101 15 my %values = map { $_ => 1 } @{$values};
102  
103 3 push(@{$values},$info->{default_value})
104 if defined($info->{default_value})
105 15 ▶ && !exists $values{$info->{default_value}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"default_value"} and not exists $values{$info->{"default_value"}} 3 12 15
Condition100%3/3
defined $info->{"default_value"} and not exists $values{$info->{"default_value"}}
!ll&&!rl&&r
663
MC/DC100%2/2
defined $info->{"default_value"} and not exists $values{$info->{"default_value"}}
defined $info->{"default_value"} not exists $values{$info->{"default_value"}}
Truth table100%3/3
A and not(B)
A= defined $info->{"default_value"}B= not exists $values{$info->{"default_value"}}
ABresult
0X0
100
111
106  
107 $self->inflate_column(
108 $column => {
109 4 ▶ inflate => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
110 4 my $val = shift;
111  
112 4 my $c = {values => $values};
113 $c->{unset} = $info->{is_nullable}
114 if exists $info->{is_nullable}
115 4 ▶ and $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"is_nullable"} and $info->{"is_nullable"} 2 2 4
Condition66%2/3
exists $info->{"is_nullable"} and $info->{"is_nullable"}
!ll&&!rl&&r
022
MC/DC50%1/2
exists $info->{"is_nullable"} and $info->{"is_nullable"}
exists $info->{"is_nullable"} $info->{"is_nullable"}
Truth table66%2/3
A and B
A= exists $info->{"is_nullable"}B= $info->{"is_nullable"}
ABresult
0X0
100
111
116 $c->{default} = $info->{default_value}
117 4 ▶ if exists $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"default_value"} 2 2 4
118  
119 4 my $e = Object::Enum->new($c);
120 4 $e->value($val);
121  
122 3 return $e;
123 },
124 0 ▶ deflate => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 return shift->value
126 0 }
127 }
128 15 );
129
130 }
131  
132 =head1 AUTHOR
133  
134 Jason M. Mills, C<< <jmmills at cpan.org> >>
135  
136 =head1 BUGS
137  
138 Please report any bugs or feature requests to C<bug-dbix-class-inflatecolumn-object-enum at rt.cpan.org>, or through
139 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-Object-Enum>. I will be notified, and then you'll
140 automatically be notified of progress on your bug as I make changes.
141  
142 =head1 CAVEATS
143  
144 =over 2
145  
146 =item * Please note that when a column definition C<is_nullable> then L<Object::Enum> will insist that there be a C<default_value> set.
147  
148 =back
149  
150 =head1 SUPPORT
151  
152 You can find documentation for this module with the perldoc command.
153  
154 perldoc DBIx::Class::InflateColumn::Object::Enum
155  
156  
157 You can also look for information at:
158  
159 =over 4
160  
161 =item * RT: CPAN's request tracker
162  
163 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-Object-Enum>
164  
165 =item * AnnoCPAN: Annotated CPAN documentation
166  
167 L<http://annocpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
168  
169 =item * CPAN Ratings
170  
171 L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-Object-Enum>
172  
173 =item * Search CPAN
174  
175 L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
176  
177 =back
178  
179  
180 =head1 SEE ALSO
181  
182 L<Object::Enum>, L<DBIx::Class>, L<DBIx::Class::InflateColumn::URI>
183  
184  
185 =head1 COPYRIGHT & LICENSE
186  
187 Copyright 2008 Jason M. Mills, all rights reserved.
188  
189 This program is free software; you can redistribute it and/or modify it
190 under the same terms as Perl itself.
191  
192  
193 =cut
194  
195 4 1; # End of DBIx::Class::InflateColumn::Object::Enum
↑ Summary