Giter Club home page Giter Club logo

dbix-class-helper-row-enumeration's Introduction

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)

dbix-class-helper-row-enumeration's People

Contributors

arc avatar robrwo avatar

Watchers

 avatar  avatar

Forkers

arc

dbix-class-helper-row-enumeration's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.