NAME

Class::Type::Enum - Build Enum-like classes

VERSION

version 0.001

SYNOPSIS

package Toast::Status {
  use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt'];
}

package Toast {
  use Moo;

  has status => (
    is     => 'rw',
    isa    => Toast::Status->get_test,
    coerce => Toast::Status->get_coerce,
  );
}

my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast );

my @trashcan = grep { $_->status->is_burnt } @toast;
my @plate    = grep { $_->status->is_toast } @toast;

my $ready_status   = Toast::Status->new('toast');
my @eventual_toast = grep { $_->status < $ready_status } @toast;

# or:

@eventual_toast = grep { $_->status->none('toast', 'burnt') } @toast;

DESCRIPTION

Class::Type::Enum is a class builder for type-like classes to represent your enumerated values. In particular, it was built to scratch an itch with DBIx::Class value inflation.

I wouldn't consider the interface stable yet; I'd love feedback on this dist.

When useing Class::Type::Enum:

  • Required:

    values => [@symbols]

    The list of symbolic values in your enum, in ascending order if relevant.

  • Optional

    init => $integer // 0

    If provided, the ordinal values of your enum will begin with the init value.

    bits => $bool // !!0

    If true, each ordinal will be an increasing power of two, rather than increments. That is, with this set, your enum's ordinal values will be 1, 2, 4, 8, 16, ... which can be handy for bit fields.

METHODS

$class->import(values => [], ...)

Sets up the consuming class as a subclass of Class::Type::Enum and installs functions that are unique to the class.

$class->new($value)

Your basic constructor, expects only a value corresponding to a symbol in the enum type.

$class->inflate_value($value)

Does the actual work of $class->new($value), also used when inflating values for DBIx::Class::InflateColumn::ClassTypeEnum.

$class->inflate($ord)

Used when inflating ordinal values for DBIx::Class::InflateColumn::ClassTypeEnum or if you need to work with ordinals directly.

$class->values_ord

Returns a hashref with symbols as keys and ordinals as values.

$class->ord_values

Returns a hashref with ordinals as keys and symbols as values.

$class->values

Returns an arrayref of valid symbolic values in order.

$class->get_test

Returns a function which either returns true if it's passed a valid value for the enum, or throws an exception.

$class->test($value)

A helper for directly using $class->get_test.

Toast::Status->test('deleted')   # throws an exception

$class->get_coerce

Returns a function which returns an enum if given an enum, or tries to create an enum from the given value using $class->new($value).

TODO: test and coerce don't work with ordinals

$o->is($value)

Given a test value, returns true or false if the enum instance's value is equal to the test value.

An exception is thrown if an invalid test value is given.

$o->is_$value

Shortcut for $o->is($value)

$o->stringify

Returns the symbolic value.

$o->numify

Returns the ordinal value.

$o->any(@cases)

True if $o->is(..) for any of the given cases.

$o->none(@cases)

True if $o->is(..) for none of the given cases.

SEE ALSO

AUTHOR

Meredith Howard <mhoward@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Meredith Howard.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.