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 our $VERSION = '0.04';
23 Load this module via load_components and utilize is_enum and values property
24 to define Enumuration columns via Object::Enum
30 use base 'DBIx::Class';
32 __PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
33 __PACKAGE__->table('testtable');
34 __PACKAGE__->add_columns(
36 data_type => 'varchar',
39 list => [qw/red green blue/]
42 color_native => { # works inline with native enum type
46 list => [qw/red green blue/]
53 Now you may treat the column as an L<Object::Enum> object.
55 my $table_rs = $db->resultset('TableClass')->create({
59 $table_rs->color->set_red; # sets color to red
60 $table_rs->color->is_red; # would return true
61 $table_rs->color->is_green; # would return false
62 print $table_rs->color->value; # would print 'red'
63 $table_rs->color->unset; # set the value to 'undef' or 'null'
64 $table_rs->color->is_red; # returns false now
69 =head2 register_column
71 Internal chained method with L<DBIx::Class::Row/register_column>.
72 Users do not call this directly!
78 my ($column, $info) = @_;
80 $self->next::method(@_);
82 return unless defined $info->{is_enum} and $info->{is_enum};
84 croak("Object::Enum '$column' missing 'extra => { list => [] }' column configuration")
86 defined $info->{extra}
87 and ref $info->{extra} eq 'HASH'
88 and defined $info->{extra}->{list}
91 croak("Object::Enum '$column' value list (extra => { list => [] }) must be an ARRAY reference")
92 unless ref $info->{extra}->{list} eq 'ARRAY';
94 my $values = $info->{extra}->{list};
95 my %values = map {$_=>1} @{$values};
97 if ( defined($info->{default_value}) && !exists $values{$info->{default_value}}) {
98 push(@{$values},$info->{default_value});
99 $values->{$info->{default_value}} = 1;
102 $self->inflate_column(
106 my $e = Object::Enum->new({values=>$values});
107 $e->value($val) if $val and exists $values{$val};
120 Jason M. Mills, C<< <jmmills at cpan.org> >>
124 Please report any bugs or feature requests to C<bug-dbix-class-inflatecolumn-object-enum at rt.cpan.org>, or through
125 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
126 automatically be notified of progress on your bug as I make changes.
133 You can find documentation for this module with the perldoc command.
135 perldoc DBIx::Class::InflateColumn::Object::Enum
138 You can also look for information at:
142 =item * RT: CPAN's request tracker
144 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-Object-Enum>
146 =item * AnnoCPAN: Annotated CPAN documentation
148 L<http://annocpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
152 L<http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-Object-Enum>
156 L<http://search.cpan.org/dist/DBIx-Class-InflateColumn-Object-Enum>
163 L<Object::Enum>, L<DBIx::Class>, L<DBIx::Class::InflateColumn::URI>
166 =head1 COPYRIGHT & LICENSE
168 Copyright 2008 Jason M. Mills, all rights reserved.
170 This program is free software; you can redistribute it and/or modify it
171 under the same terms as Perl itself.
176 1; # End of DBIx::Class::InflateColumn::Object::Enum