DBIx::Class::InflateColumn::Object::Enum - Allows a DBIx::Class user to define a Object::Enum column


DBIx-Class-InflateColumn-Object-Enum documentation Contained in the DBIx-Class-InflateColumn-Object-Enum distribution.

Index


Code Index:

NAME

Top

DBIx::Class::InflateColumn::Object::Enum - Allows a DBIx::Class user to define a Object::Enum column

VERSION

Top

Version 0.03

SYNOPSIS

Top

Load this module via load_components and utilize is_enum and values property to define Enumuration columns via Object::Enum

    package TableClass;

    use strict;
    use warnings;
    use base 'DBIx::Class';

    __PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
    __PACKAGE__->table('testtable');
    __PACKAGE__->add_columns(
        color => {
            data_type => 'varchar',
            is_enum => 1,
            extra => {
                list => [qw/red green blue/]
            }
        }
        color_native => { # works inline with native enum type
            data_type => 'enum',
            is_enum => 1,
            extra => {
                list => [qw/red green blue/]
            }
        }
    );

    1;

Now you may treat the column as an Object::Enum object.

    my $table_rs = $db->resultset('TableClass')->create({
        color => undef
    });

    $table_rs->color->set_red; # sets color to red
    $table_rs->color->is_red; # would return true
    $table_rs->color->is_green; # would return false
    print $table_rs->color->value; # would print 'red'
    $table_rs->color->unset; # set the value to 'undef' or 'null'
    $table_rs->color->is_red; # returns false now




METHODS

Top

register_column

Internal chained method with register_column in DBIx::Class::Row. Users do not call this directly!

AUTHOR

Top

Jason M. Mills, <jmmills at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-dbix-class-inflatecolumn-object-enum at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-InflateColumn-Object-Enum. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc DBIx::Class::InflateColumn::Object::Enum




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-InflateColumn-Object-Enum

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/DBIx-Class-InflateColumn-Object-Enum

* CPAN Ratings

http://cpanratings.perl.org/d/DBIx-Class-InflateColumn-Object-Enum

* Search CPAN

http://search.cpan.org/dist/DBIx-Class-InflateColumn-Object-Enum

SEE ALSO

Top

Object::Enum, DBIx::Class, DBIx::Class::InflateColumn::URI

COPYRIGHT & LICENSE

Top


DBIx-Class-InflateColumn-Object-Enum documentation Contained in the DBIx-Class-InflateColumn-Object-Enum distribution.
package DBIx::Class::InflateColumn::Object::Enum;

use warnings;
use strict;
use Carp qw/croak confess/;
use Object::Enum;

our $VERSION = '0.04';


sub register_column {
    my $self = shift;
    my ($column, $info) = @_;
    
    $self->next::method(@_);
    
    return unless defined $info->{is_enum} and $info->{is_enum};
    
    croak("Object::Enum '$column' missing 'extra => { list => [] }' column configuration")
        unless (
            defined $info->{extra}
            and ref $info->{extra}  eq 'HASH'
            and defined $info->{extra}->{list}
        );
        
    croak("Object::Enum '$column' value list (extra => { list => [] }) must be an ARRAY reference")
        unless ref $info->{extra}->{list} eq 'ARRAY';
    
    my $values = $info->{extra}->{list};
    my %values = map {$_=>1} @{$values};
    
    if ( defined($info->{default_value}) && !exists $values{$info->{default_value}}) {
        push(@{$values},$info->{default_value});
        $values->{$info->{default_value}} = 1;
    }
    
    $self->inflate_column(
        $column => {
            inflate => sub {
                my $val = shift;
                my $e = Object::Enum->new({values=>$values});
                $e->value($val) if $val and exists $values{$val};
                return $e;
            },
            deflate => sub {
                return shift->value
            }
        }
    );
    
}

1; # End of DBIx::Class::InflateColumn::Object::Enum