| 1 | package Moose::Meta::TypeConstraint::Enum; | ||||||||||||||
| 2 | 398 | our $VERSION = '2.4001'; | |||||||||||||
| 3 | |||||||||||||||
| 4 | 398 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 398 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 398 | ▶ | use metaclass; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 398 | ▶ | use B; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 398 | ▶ | use Moose::Util::TypeConstraints (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 398 | ▶ | use parent 'Moose::Meta::TypeConstraint'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 398 | ▶ | use Moose::Util 'throw_exception'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 398 | __PACKAGE__->meta->add_attribute('values' => ( | |||||||||||||
| 16 | accessor => 'values', | ||||||||||||||
| 17 | Class::MOP::_definition_context(), | ||||||||||||||
| 18 | )); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 398 | __PACKAGE__->meta->add_attribute('_inline_var_name' => ( | |||||||||||||
| 21 | accessor => '_inline_var_name', | ||||||||||||||
| 22 | Class::MOP::_definition_context(), | ||||||||||||||
| 23 | )); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 30 | ▶ | my $inliner = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 30 | my $self = shift; | |||||||||||||
| 27 | 30 | my $val = shift; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 30 | return 'defined(' . $val . ') ' | |||||||||||||
| 30 | . '&& !ref(' . $val . ') ' | ||||||||||||||
| 31 | . '&& $' . $self->_inline_var_name . '{' . $val . '}'; | ||||||||||||||
| 32 | 398 | }; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 398 | my $var_suffix = 0; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 22 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 37 | 22 | my ( $class, %args ) = @_; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 22 | $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str'); | |||||||||||||
| 40 | 22 | $args{inlined} = $inliner; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 22 | ▶ | if ( scalar @{ $args{values} } < 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 2 | throw_exception( MustHaveAtLeastOneValueToEnumerate => params => \%args, | |||||||||||||
| 44 | class => $class | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 20 | for (@{ $args{values} }) { | |||||||||||||
| 49 | 109 | ▶ | if (!defined($_)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | 2 | throw_exception( EnumValuesMustBeString => params => \%args, | |||||||||||||
| 51 | class => $class, | ||||||||||||||
| 52 | value => $_ | ||||||||||||||
| 53 | ); | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | elsif (ref($_)) { | ||||||||||||||
| 56 | 2 | throw_exception( EnumValuesMustBeString => params => \%args, | |||||||||||||
| 57 | class => $class, | ||||||||||||||
| 58 | value => $_ | ||||||||||||||
| 59 | ); | ||||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 16 | my %values = map { $_ => 1 } @{ $args{values} }; | |||||||||||||
| 64 | 0 | ▶ | $args{constraint} = sub { $values{ $_[0] } }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | 16 | my $var_name = 'enums' . $var_suffix++;; | |||||||||||||
| 67 | 16 | $args{_inline_var_name} = $var_name; | |||||||||||||
| 68 | 16 | $args{inline_environment} = { '%' . $var_name => \%values }; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 16 | my $self = $class->SUPER::new(\%args); | |||||||||||||
| 71 | |||||||||||||||
| 72 | 16 | ▶ | $self->compile_type_constraint() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | unless $self->_has_compiled_type_constraint; | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 51 | ▶ | $self->message( sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | 51 | my $value = shift; | |||||||||||||
| 77 | sprintf( | ||||||||||||||
| 78 | '%s. Value must be equal to %s.', | ||||||||||||||
| 79 | $self->_default_message->( $value ), | ||||||||||||||
| 80 | 51 | Moose::Util::_english_list_or( map B::perlstring($_), @{ $self->values } ), | |||||||||||||
| 81 | ) | ||||||||||||||
| 82 | 16 | } ); | |||||||||||||
| 83 | |||||||||||||||
| 84 | 16 | return $self; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 5 | ▶ | sub equals { | ||||||||||||
|
Subroutine
equals: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 88 | 5 | my ( $self, $type_or_name ) = @_; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 5 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | |||||||||||||
| 91 | |||||||||||||||
| 92 | 5 | ▶ | return unless $other->isa(__PACKAGE__); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 3 | my @self_values = sort @{ $self->values }; | |||||||||||||
| 95 | 3 | my @other_values = sort @{ $other->values }; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 3 | ▶ | return unless @self_values == @other_values; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | 2 | ▶ | while ( @self_values ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 8 | my $value = shift @self_values; | |||||||||||||
| 101 | 8 | my $other_value = shift @other_values; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 8 | ▶ | return unless $value eq $other_value; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 2 | return 1; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 151 | ▶ | sub constraint { | ||||||||||||
|
Subroutine
constraint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 110 | 151 | my $self = shift; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 151 | my %values = map { $_ => undef } @{ $self->values }; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 151 | ▶ | return sub { exists $values{$_[0]} }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | sub create_child_type { | ||||||||||||
|
Subroutine
create_child_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 118 | 0 | my ($self, @args) = @_; | |||||||||||||
| 119 | 0 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 398 | 1; | |||||||||||||
| 123 | |||||||||||||||
| 124 | # ABSTRACT: Type constraint for enumerated values. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | __END__ | ||||||||||||||