DBIx::Class::Helper::Row::Enumeration - Add methods for emum values
version v0.1.8
In your result class:
use base qw/DBIx::Class::Core/; __PACKAGE__->load_components(qw/ Helper::Row::Enumeration /); __PACKAGE__->add_column( foo => { data_type => 'enum', extra => { list => [qw/ good bad ugly /], }, },
with a row:
if ($row->is_good) { ... }
This plugin is inspired by MooseX::Enumeration.
Suppose your database has a column with an enum value. Checks against string values are prone to typos:
if ($row->result eq 'faol') { ... }
when instead you wanted
if ($row->result eq 'fail') { ... }
Using this plugin, you can avoid common bugs by checking against a method instead:
if ($row->is_fail) { ... }
You can override method names by adding an extra handles attribute to the column definition:
handles
bar => { data_type => 'enum', extra => { list => [qw/ good bad ugly /], handles => { good_bar => 'good', coyote => 'ugly', }, }, },
Note that only methods you specify will be added. In the above case, there is no "is_bad" method added.
The handles attribute can also be set to a code reference so that method names can be generated dynamically:
baz => { data_type => 'enum', extra => { list => [qw/ good bad ugly /], handles => sub { my ($value, $col, $class) = @_; return undef if $value eq 'deprecated'; return "is_${col}_${value}"; }, }, }, );
If the function returns undef, then no method will be generated for that value.
undef
If handles is set to "0", then no methods will be generated for the column at all.
See also "BUGS" below.
Multiple columns with overlapping enum values will cause an error. You'll need to specify a handler to rename methods or skip them altogether.
You can use column modifiers to update autogenerated classes created by the likes of DBIx::Class::Schema::Loader. However, the extra attributes are not deep-merged, so you will have to repeat them when if you want to specify custom handlers, e.g.
extra
# Created by DBIx::Class::Schema::Loader etc. # DO NOT MODIFY THIS OR ANYTHING ABOVE! etc. __PACKAGE__->load_components(qw/ Helper::Row::Enumeration /); __PACKAGE__->add_columns( '+foo', # using default handlers '+baz' => { extra => { list => [qw/ good bad ugly /], handles => { ... }, }, }, );
Note that this is by design, since the intention of column modifiers is to override existing values.
DBIx::Class
MooseX::Enumeration
The module DBIx::Class::Helper::ResultSet::EnumMethods adds similar methods to resultsets.
The development version is on github at https://github.com/robrwo/DBIx-Class-Helper-Row-Enumeration and may be cloned from git://github.com/robrwo/DBIx-Class-Helper-Row-Enumeration.git
Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/DBIx-Class-Helper-Row-Enumeration/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Robert Rothenberg <rrwo@cpan.org>
The initial development of this module was sponsored by Science Photo Library https://www.sciencephoto.com.
Aaron Crane <arc@cpan.org>
This software is Copyright (c) 2018-2019 by Robert Rothenberg.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install DBIx::Class::Helper::Row::Enumeration, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::Helper::Row::Enumeration
CPAN shell
perl -MCPAN -e shell install DBIx::Class::Helper::Row::Enumeration
For more information on module installation, please visit the detailed CPAN module installation guide.