Commit | Line | Data |
620db045 |
1 | package Moose::Meta::TypeConstraint::Role; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
7 | use Scalar::Util 'blessed'; |
8 | use Moose::Util::TypeConstraints (); |
9 | |
620db045 |
10 | use base 'Moose::Meta::TypeConstraint'; |
11 | |
12 | __PACKAGE__->meta->add_attribute('role' => ( |
13 | reader => 'role', |
14 | )); |
15 | |
16 | sub new { |
17 | my ( $class, %args ) = @_; |
18 | |
f37d0936 |
19 | $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object'); |
7ae2d933 |
20 | |
21 | my $role_name = $args{role}; |
22 | $args{constraint} = sub { Moose::Util::does_role( $_[0], $role_name ) }; |
23 | |
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 | |
32 | sub _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 | |
40 | sub 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 |
d03bd989 |
50 | Moose::Util::TypeConstraints::find_type_constraint($_) |
51 | || |
620db045 |
52 | __PACKAGE__->new( role => $_, name => "__ANON__" ) |
1551760b |
53 | } @{ Class::MOP::class_of($self->role)->get_roles }, |
620db045 |
54 | ); |
55 | } |
56 | |
57 | sub 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 | |
68 | sub 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 | |
76 | sub 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 |
6f84b038 |
81 | return 1 if Class::MOP::class_of($self->role)->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; |
d03bd989 |
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 |
db6ace79 |
91 | return Class::MOP::class_of($self->role)->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 |
98 | sub create_child_type { |
99 | my ($self, @args) = @_; |
39170e48 |
100 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
101 | } |
102 | |
620db045 |
103 | 1; |
104 | |
ad46f524 |
105 | # ABSTRACT: Role/TypeConstraint parallel hierarchy |
106 | |
620db045 |
107 | __END__ |
108 | |
109 | =pod |
110 | |
3cb26ff4 |
111 | =head1 DESCRIPTION |
112 | |
113 | This class represents type constraints for a role. |
114 | |
115 | =head1 INHERITANCE |
116 | |
117 | C<Moose::Meta::TypeConstraint::Role> is a subclass of |
118 | L<Moose::Meta::TypeConstraint>. |
119 | |
620db045 |
120 | =head1 METHODS |
121 | |
122 | =over 4 |
123 | |
3cb26ff4 |
124 | =item B<< Moose::Meta::TypeConstraint::Role->new(%options) >> |
125 | |
126 | This creates a new role type constraint based on the given |
127 | C<%options>. |
128 | |
129 | It takes the same options as its parent, with two exceptions. First, |
130 | it requires an additional option, C<role>, which is name of the |
131 | constraint's role. Second, it automatically sets the parent to the |
132 | C<Object> type. |
133 | |
134 | The constructor also overrides the hand optimized type constraint with |
135 | one it creates internally. |
620db045 |
136 | |
3cb26ff4 |
137 | =item B<< $constraint->role >> |
620db045 |
138 | |
3cb26ff4 |
139 | Returns the role name associated with the constraint. |
620db045 |
140 | |
3cb26ff4 |
141 | =item B<< $constraint->parents >> |
620db045 |
142 | |
3cb26ff4 |
143 | Returns all the type's parent types, corresponding to the roles that |
144 | its role does. |
620db045 |
145 | |
2870fb09 |
146 | =item B<< $constraint->is_subtype_of($type_name_or_object) >> |
620db045 |
147 | |
3cb26ff4 |
148 | If the given type is also a role type, then this checks that the |
149 | type's role does the other type's role. |
620db045 |
150 | |
3cb26ff4 |
151 | Otherwise it falls back to the implementation in |
152 | L<Moose::Meta::TypeConstraint>. |
2fb4885e |
153 | |
3cb26ff4 |
154 | =item B<< $constraint->create_child_type(%options) >> |
620db045 |
155 | |
3cb26ff4 |
156 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
157 | as its parent. |
620db045 |
158 | |
3cb26ff4 |
159 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Role> |
160 | object! |
620db045 |
161 | |
162 | =back |
163 | |
164 | =head1 BUGS |
165 | |
d4048ef3 |
166 | See L<Moose/BUGS> for details on reporting bugs. |
620db045 |
167 | |
620db045 |
168 | =cut |