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 | |
fb4fcfee |
10 | our $VERSION = '0.57'; |
75b95414 |
11 | $VERSION = eval $VERSION; |
620db045 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
13 | |
14 | use base 'Moose::Meta::TypeConstraint'; |
15 | |
16 | __PACKAGE__->meta->add_attribute('role' => ( |
17 | reader => 'role', |
18 | )); |
19 | |
20 | sub 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 | |
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 |
50 | Moose::Util::TypeConstraints::find_type_constraint($_) |
51 | || |
52 | __PACKAGE__->new( role => $_, name => "__ANON__" ) |
53 | } @{ $self->role->meta->get_roles }, |
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 | |
62 | return unless $other->isa(__PACKAGE__); |
63 | |
64 | return $self->role eq $other->role; |
65 | } |
66 | |
67 | sub is_a_type_of { |
68 | my ($self, $type_or_name) = @_; |
69 | |
70 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
71 | |
72 | ($self->equals($type) || $self->is_subtype_of($type_or_name)); |
73 | } |
74 | |
75 | sub is_subtype_of { |
76 | my ($self, $type_or_name_or_role ) = @_; |
77 | |
78 | if ( not ref $type_or_name_or_role ) { |
79 | # it might be a role |
e324865b |
80 | return 1 if $self->role->meta->does_role( $type_or_name_or_role ); |
620db045 |
81 | } |
82 | |
83 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_role); |
84 | |
85 | if ( $type->isa(__PACKAGE__) ) { |
86 | # if $type_or_name_or_role isn't a role, it might be the TC name of another ::Role type |
87 | # or it could also just be a type object in this branch |
e324865b |
88 | return $self->role->meta->does_role( $type->role ); |
620db045 |
89 | } else { |
90 | # the only other thing we are a subtype of is Object |
91 | $self->SUPER::is_subtype_of($type); |
92 | } |
93 | } |
94 | |
95 | 1; |
96 | |
97 | __END__ |
98 | |
99 | =pod |
100 | |
101 | =head1 NAME |
102 | |
103 | Moose::Meta::TypeConstraint::Role - Role/TypeConstraint parallel hierarchy |
104 | |
105 | =head1 METHODS |
106 | |
107 | =over 4 |
108 | |
109 | =item B<new> |
110 | |
6c36eeaa |
111 | =item B<role> |
620db045 |
112 | |
113 | =item B<hand_optimized_type_constraint> |
114 | |
115 | =item B<has_hand_optimized_type_constraint> |
116 | |
117 | =item B<equals> |
118 | |
119 | =item B<is_a_type_of> |
120 | |
121 | =item B<is_subtype_of> |
122 | |
123 | =item B<parents> |
124 | |
125 | Return all the parent types, corresponding to the parent classes. |
126 | |
127 | =item B<meta> |
128 | |
129 | =back |
130 | |
131 | =head1 BUGS |
132 | |
133 | All complex software has bugs lurking in it, and this module is no |
134 | exception. If you find a bug please either email me, or add the bug |
135 | to cpan-RT. |
136 | |
137 | =head1 AUTHOR |
138 | |
139 | Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt> |
140 | |
141 | =head1 COPYRIGHT AND LICENSE |
142 | |
143 | Copyright 2006-2008 by Infinity Interactive, Inc. |
144 | |
145 | L<http://www.iinteractive.com> |
146 | |
147 | This library is free software; you can redistribute it and/or modify |
148 | it under the same terms as Perl itself. |
149 | |
150 | =cut |