Document how to pass new attribute values at instance-role application time
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Enum.pm
index bfe6aa9..3ef7234 100644 (file)
@@ -4,26 +4,41 @@ use strict;
 use warnings;
 use metaclass;
 
+use B;
 use Moose::Util::TypeConstraints ();
 
-our $VERSION   = '1.9900';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
-
 use base 'Moose::Meta::TypeConstraint';
 
 __PACKAGE__->meta->add_attribute('values' => (
     accessor => 'values',
+    Class::MOP::_definition_context(),
+));
+
+__PACKAGE__->meta->add_attribute('_inline_var_name' => (
+    accessor => '_inline_var_name',
+    Class::MOP::_definition_context(),
 ));
 
+my $inliner = sub {
+    my $self = shift;
+    my $val  = shift;
+
+    return 'defined(' . $val . ') '
+             . '&& !ref(' . $val . ') '
+             . '&& $' . $self->_inline_var_name . '{' . $val . '}';
+};
+
+my $var_suffix = 0;
+
 sub new {
     my ( $class, %args ) = @_;
 
     $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
+    $args{inlined} = $inliner;
 
-    if ( scalar @{ $args{values} } < 2 ) {
+    if ( scalar @{ $args{values} } < 1 ) {
         require Moose;
-        Moose->throw_error("You must have at least two values to enumerate through");
+        Moose->throw_error("You must have at least one value to enumerate through");
     }
 
     for (@{ $args{values} }) {
@@ -37,7 +52,14 @@ sub new {
         }
     }
 
-    my $self = $class->_new(\%args);
+    my %values = map { $_ => 1 } @{ $args{values} };
+    $args{constraint} = sub { $values{ $_[0] } };
+
+    my $var_name = 'enums' . $var_suffix++;;
+    $args{_inline_var_name} = $var_name;
+    $args{inline_environment} = { '%' . $var_name => \%values };
+
+    my $self = $class->SUPER::new(\%args);
 
     $self->compile_type_constraint()
         unless $self->_has_compiled_type_constraint;
@@ -75,14 +97,6 @@ sub constraint {
     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);
@@ -90,14 +104,12 @@ sub create_child_type {
 
 1;
 
+# ABSTRACT: Type constraint for enumerated values.
+
 __END__
 
 =pod
 
-=head1 NAME
-
-Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
-
 =head1 DESCRIPTION
 
 This class represents type constraints based on an enumerated list of
@@ -144,19 +156,6 @@ object!
 
 See L<Moose/BUGS> for details on reporting bugs.
 
-=head1 AUTHOR
-
-Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2006-2010 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
 =cut