| 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
Condition66%2/3
defined $info->{"is_enum"} and $info->{"is_enum"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
defined $info->{"extra"} and ref $info->{"extra"} eq "HASH" and defined $info->{"extra"}{"list"}
defined $info->{"extra"} and ref $info->{"extra"} eq "HASH"
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
exists $info->{"is_nullable"} and $info->{"is_nullable"} and not $info->{"default_value"}
exists $info->{"is_nullable"} and $info->{"is_nullable"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $info->{"default_value"} and not exists $values{$info->{"default_value"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $info->{"is_nullable"} and $info->{"is_nullable"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | $c->{default} = $info->{default_value} | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | if exists $info->{default_value}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||