1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
NAME
Class::Type::Enum - Build Enum-like classes
VERSION
version 0.014
SYNOPSIS
package Toast::Status {
use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt'];
}
package Toast {
use Moo;
has status => (
is => 'rw',
isa => Toast::Status->type_constraint,
coerce => 1,
handles => [ Toast::Status->list_is_methods ],
);
}
my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast );
my @trashcan = grep { $_->is_burnt } @toast;
my @plate = grep { $_->is_toast } @toast;
my $ready_status = Toast::Status->new('toast');
my @eventual_toast = grep { $_->status < $ready_status } @toast;
# or:
@eventual_toast = grep { $_->status lt 'toast' } @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.
values => {symbol => ordinal, ...}
The list of symbols and ordinal values in your enum. There is no
check that a given ordinal isn't reused.
Custom Ordinal Values
If you'd like to build an enum that works like a bitfield or some other
custom setup, you need only pass a more explicit hashref to
Class::Type::Enum.
package BitField {
use Class::Type::Enum values => {
READ => 1,
WRITE => 2,
EXECUTE => 4,
};
}
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. Also works as an instance method for enums of the
same class.
$class->inflate_symbol($symbol)
Does the actual work of $class->new($value), also used when inflating
values for DBIx::Class::InflateColumn::ClassTypeEnum.
$class->inflate_ordinal($ord)
Used when inflating ordinal values for
DBIx::Class::InflateColumn::ClassTypeEnum or if you need to work with
ordinals directly.
$class->sym_to_ord
Returns a hashref keyed by symbol, with ordinals as values.
$class->ord_to_sym
Returns a hashref keyed by ordinal, with symbols as values.
$class->values
Returns an arrayref of valid symbolic values, in order.
$class->list_is_methods
Returns a list of is_ methods defined for each symbolic value for the
class.
$class->type_constraint
This method requires the optional dependency Type::Tiny.
Returns a type constraint suitable for use with Moo and friends.
$class->test_symbol($value)
Test whether or not the given value is a valid symbol in this enum
class.
$class->test_ordinal($value)
Test whether or not the given value is a valid ordinal in this enum
class.
$class->coerce_symbol($value)
If the given value is already a $class, return it, otherwise try to
inflate it as a symbol. Dies on invalid value.
$class->coerce_ordinal($value)
If the given value is already a $class, return it, otherwise try to
inflate it as an ordinal. Dies on invalid value.
$class->coerce_any($value)
If the given value is already a $class, return it, otherwise try to
inflate it first as an ordinal, then as a symbol. Dies on invalid
value.
$o->is($value)
Given a test symbol, test that the enum instance's value is equivalent.
An exception is thrown if an invalid symbol is provided
$o->is_$value
Shortcut for $o->is($value)
$o->stringify
Returns the symbolic value.
$o->numify
Returns the ordinal value.
$o->cmp($other, $reversed = undef)
The string-compare implementation used by overloading. Returns the same
values as cmp. The optional third argument is an artifact of overload,
set to true if the order of $o and $other have been reversed in order
to make the overloaded method call work.
$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
* Object::Enum
* Class::Enum
* Enumeration
AUTHOR
Meredith Howard <mhoward@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2018 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.
|