Skip to content

robrwo/DBIx-Class-Helper-Row-Enumeration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

DBIx::Class::Helper::Row::Enumeration - Add methods for emum values

VERSION

version v0.1.8

SYNOPSIS

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) { ... }

DESCRIPTION

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) { ... }

Overriding method names

You can override method names by adding an extra handles attribute to the column definition:

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.

If handles is set to "0", then no methods will be generated for the column at all.

KNOWN ISSUES

See also "BUGS" below.

Overlapping enum values

Multiple columns with overlapping enum values will cause an error. You'll need to specify a handler to rename methods or skip them altogether.

Autogenerated Classes

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.

# 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.

SEE ALSO

DBIx::Class

MooseX::Enumeration

The module DBIx::Class::Helper::ResultSet::EnumMethods adds similar methods to resultsets.

SOURCE

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

BUGS

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.

AUTHOR

Robert Rothenberg [email protected]

The initial development of this module was sponsored by Science Photo Library https://www.sciencephoto.com.

CONTRIBUTOR

Aaron Crane [email protected]

COPYRIGHT AND LICENSE

This software is Copyright (c) 2018-2019 by Robert Rothenberg.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

About

Add methods for emum values

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages