Class:...e::Enum(3pm)

πŸ’– Class::Type::Enum β€” the manual, but make it cute πŸ’–

okay so bestie, welcome to the Class:...e::Enum(3pm) page from the User Contributed Perl Documentation. sit down, grab a strawberry πŸ“, and let's build some enum classes together.


✨ NAME ✨

Class::Type::Enum β€” literally builds you Enum-like classes. that's the whole vibe. πŸ‘‘


🌸 VERSION 🌸

version 0.014 β€” that's the one we're talking about, no cap.


πŸ¦‹ SYNOPSIS πŸ¦‹

okay here's the whole thing in action, take it in:

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;

it's giving toast. it's giving enumerated toast. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

So Class::Type::Enum is a class builder for type-like classes that represent your enumerated values. the way it was built? to scratch an itch with DBIx::Class value inflation. relatable honestly. 😭

and ngl, the author says the interface isn't stable yet β€” they'd love feedback on this dist. so if you have thoughts, speak up, bestie. 🫢

here's what happens when you use Class::Type::Enum:

⭐ Required ⭐

  • values => [@symbols] β€” the list of symbolic values in your enum, in ascending order if that's relevant to you. πŸ’–
  • values => {symbol => ordinal, ...} β€” the list of symbols and their ordinal values in your enum. heads up: there is no check that a given ordinal isn't reused. πŸ™ˆ

🌈 Custom Ordinal Values 🌈

if you wanna build an enum that works like a bitfield, or some other custom setup, you literally just pass a more explicit hashref to Class::Type::Enum. that's it. iconic behaviour.

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, bestie. expects only a value corresponding to a symbol in the enum type. and plot twist β€” it also works as an instance method for enums of the same class. 😌

🌟 $class->inflate_symbol($symbol)

Does the actual work of "$class->new($value)", and it's 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. so many little helpers, so cute. 🎁

🌟 $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)". lazy girl coding, and honestly? valid. πŸ’…

🌟 $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 β€” it's 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

go read them, iykyk. πŸ“šβœ¨


πŸ‘‘ AUTHOR πŸ‘‘

Meredith Howard <mhoward@cpan.org> πŸ’–


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.


and that's the whole page, bestie β€” perl v5.32.1, dated 2021-08-02, the Class:...e::Enum(3pm) manual. slay, periodt. πŸ’…βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from