Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.


Moose documentation Contained in the Moose distribution.

Index


Code Index:

NAME

Top

Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.

VERSION

Top

version 2.0010

DESCRIPTION

Top

This class represents type constraints based on an enumerated list of acceptable values.

INHERITANCE

Top

Moose::Meta::TypeConstraint::Enum is a subclass of Moose::Meta::TypeConstraint.

METHODS

Top

Moose::Meta::TypeConstraint::Enum->new(%options)

This creates a new enum type constraint based on the given %options.

It takes the same options as its parent, with several exceptions. First, it requires an additional option, values. This should be an array reference containing a list of valid string values. Second, it automatically sets the parent to the Str type.

Finally, it ignores any provided constraint option. The constraint is generated automatically based on the provided values.

$constraint->values

Returns the array reference of acceptable values provided to the constructor.

$constraint->create_child_type

This returns a new Moose::Meta::TypeConstraint object with the type as its parent.

Note that it does not return a Moose::Meta::TypeConstraint::Enum object!

BUGS

Top

See BUGS in Moose for details on reporting bugs.

AUTHOR

Top

Stevan Little <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


Moose documentation Contained in the Moose distribution.

package Moose::Meta::TypeConstraint::Enum;
BEGIN {
  $Moose::Meta::TypeConstraint::Enum::AUTHORITY = 'cpan:STEVAN';
}
BEGIN {
  $Moose::Meta::TypeConstraint::Enum::VERSION = '2.0010';
}

use strict;
use warnings;
use metaclass;

use Moose::Util::TypeConstraints ();

use base 'Moose::Meta::TypeConstraint';

__PACKAGE__->meta->add_attribute('values' => (
    accessor => 'values',
));

sub new {
    my ( $class, %args ) = @_;

    $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');

    if ( scalar @{ $args{values} } < 2 ) {
        require Moose;
        Moose->throw_error("You must have at least two values to enumerate through");
    }

    for (@{ $args{values} }) {
        if (!defined($_)) {
            require Moose;
            Moose->throw_error("Enum values must be strings, not undef");
        }
        elsif (ref($_)) {
            require Moose;
            Moose->throw_error("Enum values must be strings, not '$_'");
        }
    }

    my $self = $class->_new(\%args);

    $self->compile_type_constraint()
        unless $self->_has_compiled_type_constraint;

    return $self;
}

sub equals {
    my ( $self, $type_or_name ) = @_;

    my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);

    return unless $other->isa(__PACKAGE__);

    my @self_values  = sort @{ $self->values };
    my @other_values = sort @{ $other->values };

    return unless @self_values == @other_values;

    while ( @self_values ) {
        my $value = shift @self_values;
        my $other_value = shift @other_values;

        return unless $value eq $other_value;
    }

    return 1;
}

sub constraint {
    my $self = shift;

    my %values = map { $_ => undef } @{ $self->values };

    return sub { exists $values{$_[0]} };
}

sub _compile_hand_optimized_type_constraint {
    my $self  = shift;

    my %values = map { $_ => undef } @{ $self->values };

    sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
}

sub create_child_type {
    my ($self, @args) = @_;
    return Moose::Meta::TypeConstraint->new(@args, parent => $self);
}

1;

# ABSTRACT: Type constraint for enumerated values.




__END__