1 package Moose::Meta::TypeConstraint::Role;
7 use Scalar::Util 'blessed';
8 use Moose::Util::TypeConstraints ();
10 use base 'Moose::Meta::TypeConstraint';
12 __PACKAGE__->meta->add_attribute('role' => (
17 my ( $class, %args ) = @_;
19 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object');
20 my $self = $class->_new(\%args);
22 $self->_create_hand_optimized_type_constraint;
23 $self->compile_type_constraint();
28 sub _create_hand_optimized_type_constraint {
30 my $role = $self->role;
31 $self->hand_optimized_type_constraint(
32 sub { Moose::Util::does_role($_[0], $role) }
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
46 Moose::Util::TypeConstraints::find_type_constraint($_)
48 __PACKAGE__->new( role => $_, name => "__ANON__" )
49 } @{ Class::MOP::class_of($self->role)->get_roles },
54 my ( $self, $type_or_name ) = @_;
56 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
58 return unless defined $other;
59 return unless $other->isa(__PACKAGE__);
61 return $self->role eq $other->role;
65 my ($self, $type_or_name) = @_;
67 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
69 ($self->equals($type) || $self->is_subtype_of($type_or_name));
73 my ($self, $type_or_name_or_role ) = @_;
75 if ( not ref $type_or_name_or_role ) {
77 return 1 if Class::MOP::class_of($self->role)->does_role( $type_or_name_or_role );
80 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_role);
82 return unless defined $type;
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
87 return Class::MOP::class_of($self->role)->does_role( $type->role );
89 # the only other thing we are a subtype of is Object
90 $self->SUPER::is_subtype_of($type);
94 sub create_child_type {
95 my ($self, @args) = @_;
96 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
101 # ABSTRACT: Role/TypeConstraint parallel hierarchy
109 This class represents type constraints for a role.
113 C<Moose::Meta::TypeConstraint::Role> is a subclass of
114 L<Moose::Meta::TypeConstraint>.
120 =item B<< Moose::Meta::TypeConstraint::Role->new(%options) >>
122 This creates a new role type constraint based on the given
125 It takes the same options as its parent, with two exceptions. First,
126 it requires an additional option, C<role>, which is name of the
127 constraint's role. Second, it automatically sets the parent to the
130 The constructor also overrides the hand optimized type constraint with
131 one it creates internally.
133 =item B<< $constraint->role >>
135 Returns the role name associated with the constraint.
137 =item B<< $constraint->parents >>
139 Returns all the type's parent types, corresponding to the roles that
142 =item B<< $constraint->is_subtype_of($type_name_or_object) >>
144 If the given type is also a role type, then this checks that the
145 type's role does the other type's role.
147 Otherwise it falls back to the implementation in
148 L<Moose::Meta::TypeConstraint>.
150 =item B<< $constraint->create_child_type(%options) >>
152 This returns a new L<Moose::Meta::TypeConstraint> object with the type
155 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Role>
162 See L<Moose/BUGS> for details on reporting bugs.