1 package DBIx::Class::InflateColumn::Object::Enum;
5 use Carp qw/croak confess/;
10 DBIx::Class::InflateColumn::Object::Enum - Allows a DBIx::Class user to define a Object::Enum column
18 # Dist::Zill should handle this now
19 #our $VERSION = '0.04';
24 Load this module via load_components and utilize is_enum and values property
25 to define Enumuration columns via Object::Enum
31 use base 'DBIx::Class';
33 __PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
34 __PACKAGE__->table('testtable');
35 __PACKAGE__->add_columns(
37 data_type => 'varchar',
40 list => [qw/red green blue/]
43 color_native => { # works inline with native enum type
47 list => [qw/red green blue/]
54 Now you may treat the column as an L<Object::Enum> object.
56 my $table_rs = $db->resultset('TableClass')->create({
60 $table_rs->color->set_red; # sets color to red
61 $table_rs->color->is_red; # would return true
62 $table_rs->color->is_green; # would return false
63 print $table_rs->color->value; # would print 'red'
64 $table_rs->color->unset; # set the value to 'undef' or 'null'
65 $table_rs->color->is_red; # returns false now
70 =head2 register_column
72 Internal chained method with L<DBIx::Class::Row/register_column>.
73 Users do not call this directly!
79 my ($column, $info) = @_;
81 $self->next::method(@_);
83 return unless defined $info->{is_enum} and $info->{is_enum};
85 croak("Object::Enum '$column' missing 'extra => { list => [] }' column configuration")
87 defined $info->{extra}
88 and ref $info->{extra} eq 'HASH'
89 and defined $info->{extra}->{list}
92 croak("Object::Enum '$column' value list (extra => { list => [] }) must be an ARRAY reference")
93 unless ref $info->{extra}->{list} eq 'ARRAY';
95 my $values = $info->{extra}->{list};
96 my %values = map {$_=>1} @{$values};
98 if ( defined($info->{default_value}) && !exists $values{$info->{default_value}}) {
99 push(@{$values},$info->{default_value});
100 $values->{$info->{default_value}} = 1;
103 $self->inflate_column(
107 my $e = Object::Enum->new({values=>$values});
108 $e->value($val) if $val and exists $values{$val};
121 Jason M. Mills, C<< <jmmills at cpan.org> >>
125 Please report any bugs or feature requests to C<bug-dbix-class-inflatecolumn-object-enum at rt.cpan.org>, or through
126 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
127 automatically be notified of progress on your bug as I make changes.
134 You can find documentation for this module with the perldoc command.
136 perldoc DBIx::Class::InflateColumn::Object::Enum
139 You can also look for information at:
143 =item * RT: CPAN's request tracker
145 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-Object-Enum>
147 =item * AnnoCPAN: Annotated CPAN documentation
149 L<http://annocpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
153 L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-Object-Enum>
157 L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
164 L<Object::Enum>, L<DBIx::Class>, L<DBIx::Class::InflateColumn::URI>
167 =head1 COPYRIGHT & LICENSE
169 Copyright 2008 Jason M. Mills, all rights reserved.
171 This program is free software; you can redistribute it and/or modify it
172 under the same terms as Perl itself.
177 1; # End of DBIx::Class::InflateColumn::Object::Enum