Enum needs to check that the value is not a ref
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Role.pm
CommitLineData
620db045 1package Moose::Meta::TypeConstraint::Role;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Scalar::Util 'blessed';
8use Moose::Util::TypeConstraints ();
9
620db045 10use base 'Moose::Meta::TypeConstraint';
11
12__PACKAGE__->meta->add_attribute('role' => (
13 reader => 'role',
14));
15
16sub new {
17 my ( $class, %args ) = @_;
18
f37d0936 19 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object');
0779da92 20 my $self = $class->_new(\%args);
620db045 21
22 $self->_create_hand_optimized_type_constraint;
23 $self->compile_type_constraint();
24
25 return $self;
26}
27
28sub _create_hand_optimized_type_constraint {
29 my $self = shift;
30 my $role = $self->role;
31 $self->hand_optimized_type_constraint(
32 sub { Moose::Util::does_role($_[0], $role) }
33 );
34}
35
36sub parents {
37 my $self = shift;
38 return (
39 $self->parent,
40 map {
41 # FIXME find_type_constraint might find a TC named after the role but that isn't really it
42 # I did this anyway since it's a convention that preceded TypeConstraint::Role, and it should DWIM
43 # if anybody thinks this problematic please discuss on IRC.
44 # a possible fix is to add by attr indexing to the type registry to find types of a certain property
45 # regardless of their name
d03bd989 46 Moose::Util::TypeConstraints::find_type_constraint($_)
47 ||
620db045 48 __PACKAGE__->new( role => $_, name => "__ANON__" )
1551760b 49 } @{ Class::MOP::class_of($self->role)->get_roles },
620db045 50 );
51}
52
53sub equals {
54 my ( $self, $type_or_name ) = @_;
55
56 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
57
4c015454 58 return unless defined $other;
620db045 59 return unless $other->isa(__PACKAGE__);
60
61 return $self->role eq $other->role;
62}
63
64sub is_a_type_of {
65 my ($self, $type_or_name) = @_;
66
8d33489c 67 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
620db045 68
69 ($self->equals($type) || $self->is_subtype_of($type_or_name));
70}
71
72sub is_subtype_of {
73 my ($self, $type_or_name_or_role ) = @_;
74
75 if ( not ref $type_or_name_or_role ) {
76 # it might be a role
6f84b038 77 return 1 if Class::MOP::class_of($self->role)->does_role( $type_or_name_or_role );
620db045 78 }
79
80 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_role);
81
4c015454 82 return unless defined $type;
d03bd989 83
620db045 84 if ( $type->isa(__PACKAGE__) ) {
85 # if $type_or_name_or_role isn't a role, it might be the TC name of another ::Role type
86 # or it could also just be a type object in this branch
db6ace79 87 return Class::MOP::class_of($self->role)->does_role( $type->role );
620db045 88 } else {
89 # the only other thing we are a subtype of is Object
90 $self->SUPER::is_subtype_of($type);
91 }
92}
93
2fb4885e 94sub create_child_type {
95 my ($self, @args) = @_;
39170e48 96 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
2fb4885e 97}
98
620db045 991;
100
ad46f524 101# ABSTRACT: Role/TypeConstraint parallel hierarchy
102
620db045 103__END__
104
105=pod
106
3cb26ff4 107=head1 DESCRIPTION
108
109This class represents type constraints for a role.
110
111=head1 INHERITANCE
112
113C<Moose::Meta::TypeConstraint::Role> is a subclass of
114L<Moose::Meta::TypeConstraint>.
115
620db045 116=head1 METHODS
117
118=over 4
119
3cb26ff4 120=item B<< Moose::Meta::TypeConstraint::Role->new(%options) >>
121
122This creates a new role type constraint based on the given
123C<%options>.
124
125It takes the same options as its parent, with two exceptions. First,
126it requires an additional option, C<role>, which is name of the
127constraint's role. Second, it automatically sets the parent to the
128C<Object> type.
129
130The constructor also overrides the hand optimized type constraint with
131one it creates internally.
620db045 132
3cb26ff4 133=item B<< $constraint->role >>
620db045 134
3cb26ff4 135Returns the role name associated with the constraint.
620db045 136
3cb26ff4 137=item B<< $constraint->parents >>
620db045 138
3cb26ff4 139Returns all the type's parent types, corresponding to the roles that
140its role does.
620db045 141
2870fb09 142=item B<< $constraint->is_subtype_of($type_name_or_object) >>
620db045 143
3cb26ff4 144If the given type is also a role type, then this checks that the
145type's role does the other type's role.
620db045 146
3cb26ff4 147Otherwise it falls back to the implementation in
148L<Moose::Meta::TypeConstraint>.
2fb4885e 149
3cb26ff4 150=item B<< $constraint->create_child_type(%options) >>
620db045 151
3cb26ff4 152This returns a new L<Moose::Meta::TypeConstraint> object with the type
153as its parent.
620db045 154
3cb26ff4 155Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Role>
156object!
620db045 157
158=back
159
160=head1 BUGS
161
d4048ef3 162See L<Moose/BUGS> for details on reporting bugs.
620db045 163
620db045 164=cut