File Coverage

blib/lib/DBIx/Class/InflateColumn/Object/Enum.pm
Criterion Covered Total %
statement 33 34 97.0
branch 11 14 78.5
condition 11 21 52.3
subroutine 6 7 85.7
pod 1 1 100.0
total 62 77 80.5


line stmt bran cond sub pod time code
1             package DBIx::Class::InflateColumn::Object::Enum;
2             $DBIx::Class::InflateColumn::Object::Enum::VERSION = '0.06';
3 4     4   382711 use warnings;
  4         4  
  4         102  
4 4     4   15 use strict;
  4         6  
  4         69  
5 4     4   11 use Carp qw/croak confess/;
  4         11  
  4         163  
6 4     4   1464 use Object::Enum;
  4         48254  
  4         13  
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             sub register_column {
78 30     30 1 211781 my $self = shift;
79 30         32 my ($column, $info) = @_;
80            
81 30         70 $self->next::method(@_);
82            
83 30 100 66     3989 return unless defined $info->{is_enum} and $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 50 33     106 );
      33        
91            
92             croak("Object::Enum '$column' value list (extra => { list => [] }) must be an array reference")
93 15 50       31 unless ref $info->{extra}->{list} eq 'ARRAY';
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 50 33     59 and !$info->{default_value};
      66        
99              
100 15         14 my $values = $info->{extra}->{list};
101 15         13 my %values = map { $_ => 1 } @{$values};
  45         89  
  15         20  
102              
103 3         24 push(@{$values},$info->{default_value})
104             if defined($info->{default_value})
105 15 100 100     65 && !exists $values{$info->{default_value}};
106              
107             $self->inflate_column(
108             $column => {
109             inflate => sub {
110 4     4   648565 my $val = shift;
111              
112 4         9 my $c = {values => $values};
113             $c->{unset} = $info->{is_nullable}
114             if exists $info->{is_nullable}
115 4 100 33     25 and $info->{is_nullable};
116             $c->{default} = $info->{default_value}
117 4 100       12 if exists $info->{default_value};
118              
119 4         21 my $e = Object::Enum->new($c);
120 4         1355 $e->value($val);
121              
122 3         215 return $e;
123             },
124             deflate => sub {
125             return shift->value
126 0     0     }
127             }
128 15         186 );
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             1; # End of DBIx::Class::InflateColumn::Object::Enum