class_of in Class type constraint
[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
56d7c745 10our $VERSION = '0.73';
75b95414 11$VERSION = eval $VERSION;
620db045 12our $AUTHORITY = 'cpan:STEVAN';
13
14use base 'Moose::Meta::TypeConstraint';
15
16__PACKAGE__->meta->add_attribute('role' => (
17 reader => 'role',
18));
19
20sub new {
21 my ( $class, %args ) = @_;
22
e324865b 23 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Role');
0779da92 24 my $self = $class->_new(\%args);
620db045 25
26 $self->_create_hand_optimized_type_constraint;
27 $self->compile_type_constraint();
28
29 return $self;
30}
31
32sub _create_hand_optimized_type_constraint {
33 my $self = shift;
34 my $role = $self->role;
35 $self->hand_optimized_type_constraint(
36 sub { Moose::Util::does_role($_[0], $role) }
37 );
38}
39
40sub parents {
41 my $self = shift;
42 return (
43 $self->parent,
44 map {
45 # FIXME find_type_constraint might find a TC named after the role but that isn't really it
46 # I did this anyway since it's a convention that preceded TypeConstraint::Role, and it should DWIM
47 # if anybody thinks this problematic please discuss on IRC.
48 # a possible fix is to add by attr indexing to the type registry to find types of a certain property
49 # regardless of their name
50 Moose::Util::TypeConstraints::find_type_constraint($_)
51 ||
52 __PACKAGE__->new( role => $_, name => "__ANON__" )
53 } @{ $self->role->meta->get_roles },
54 );
55}
56
57sub equals {
58 my ( $self, $type_or_name ) = @_;
59
60 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
61
4c015454 62 return unless defined $other;
620db045 63 return unless $other->isa(__PACKAGE__);
64
65 return $self->role eq $other->role;
66}
67
68sub is_a_type_of {
69 my ($self, $type_or_name) = @_;
70
8d33489c 71 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
620db045 72
73 ($self->equals($type) || $self->is_subtype_of($type_or_name));
74}
75
76sub is_subtype_of {
77 my ($self, $type_or_name_or_role ) = @_;
78
79 if ( not ref $type_or_name_or_role ) {
80 # it might be a role
e324865b 81 return 1 if $self->role->meta->does_role( $type_or_name_or_role );
620db045 82 }
83
84 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_role);
85
4c015454 86 return unless defined $type;
87
620db045 88 if ( $type->isa(__PACKAGE__) ) {
89 # if $type_or_name_or_role isn't a role, it might be the TC name of another ::Role type
90 # or it could also just be a type object in this branch
e324865b 91 return $self->role->meta->does_role( $type->role );
620db045 92 } else {
93 # the only other thing we are a subtype of is Object
94 $self->SUPER::is_subtype_of($type);
95 }
96}
97
2fb4885e 98sub create_child_type {
99 my ($self, @args) = @_;
39170e48 100 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
2fb4885e 101}
102
620db045 1031;
104
105__END__
106
107=pod
108
109=head1 NAME
110
111Moose::Meta::TypeConstraint::Role - Role/TypeConstraint parallel hierarchy
112
3cb26ff4 113=head1 DESCRIPTION
114
115This class represents type constraints for a role.
116
117=head1 INHERITANCE
118
119C<Moose::Meta::TypeConstraint::Role> is a subclass of
120L<Moose::Meta::TypeConstraint>.
121
620db045 122=head1 METHODS
123
124=over 4
125
3cb26ff4 126=item B<< Moose::Meta::TypeConstraint::Role->new(%options) >>
127
128This creates a new role type constraint based on the given
129C<%options>.
130
131It takes the same options as its parent, with two exceptions. First,
132it requires an additional option, C<role>, which is name of the
133constraint's role. Second, it automatically sets the parent to the
134C<Object> type.
135
136The constructor also overrides the hand optimized type constraint with
137one it creates internally.
620db045 138
3cb26ff4 139=item B<< $constraint->role >>
620db045 140
3cb26ff4 141Returns the role name associated with the constraint.
620db045 142
3cb26ff4 143=item B<< $constraint->parents >>
620db045 144
3cb26ff4 145Returns all the type's parent types, corresponding to the roles that
146its role does.
620db045 147
2870fb09 148=item B<< $constraint->is_subtype_of($type_name_or_object) >>
620db045 149
3cb26ff4 150If the given type is also a role type, then this checks that the
151type's role does the other type's role.
620db045 152
3cb26ff4 153Otherwise it falls back to the implementation in
154L<Moose::Meta::TypeConstraint>.
2fb4885e 155
3cb26ff4 156=item B<< $constraint->create_child_type(%options) >>
620db045 157
3cb26ff4 158This returns a new L<Moose::Meta::TypeConstraint> object with the type
159as its parent.
620db045 160
3cb26ff4 161Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Role>
162object!
620db045 163
164=back
165
166=head1 BUGS
167
168All complex software has bugs lurking in it, and this module is no
169exception. If you find a bug please either email me, or add the bug
170to cpan-RT.
171
172=head1 AUTHOR
173
174Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
175
176=head1 COPYRIGHT AND LICENSE
177
2840a3b2 178Copyright 2006-2009 by Infinity Interactive, Inc.
620db045 179
180L<http://www.iinteractive.com>
181
182This library is free software; you can redistribute it and/or modify
183it under the same terms as Perl itself.
184
185=cut