| File | /usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Moose/Meta/TypeConstraint/Role.pm |
| Statements Executed | 23 |
| Statement Execution Time | 506µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 17µs | 20µs | Moose::Meta::TypeConstraint::Role::BEGIN@3 |
| 1 | 1 | 1 | 10µs | 20µs | Moose::Meta::TypeConstraint::Role::BEGIN@4 |
| 1 | 1 | 1 | 10µs | 38µs | Moose::Meta::TypeConstraint::Role::BEGIN@7 |
| 1 | 1 | 1 | 6µs | 52µs | Moose::Meta::TypeConstraint::Role::BEGIN@14 |
| 1 | 1 | 1 | 6µs | 105µs | Moose::Meta::TypeConstraint::Role::BEGIN@5 |
| 1 | 1 | 1 | 4µs | 4µs | Moose::Meta::TypeConstraint::Role::BEGIN@8 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::__ANON__[:36] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::_create_hand_optimized_type_constraint |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::create_child_type |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::equals |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::is_a_type_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::is_subtype_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::new |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Role::parents |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Meta::TypeConstraint::Role; | ||||
| 2 | |||||
| 3 | 3 | 22µs | 2 | 23µs | # spent 20µs (17+3) within Moose::Meta::TypeConstraint::Role::BEGIN@3 which was called
# once (17µs+3µs) by Moose::Util::TypeConstraints::BEGIN@33 at line 3 # spent 20µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@3
# spent 3µs making 1 call to strict::import |
| 4 | 3 | 22µs | 2 | 30µs | # spent 20µs (10+10) within Moose::Meta::TypeConstraint::Role::BEGIN@4 which was called
# once (10µs+10µs) by Moose::Util::TypeConstraints::BEGIN@33 at line 4 # spent 20µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@4
# spent 10µs making 1 call to warnings::import |
| 5 | 3 | 30µs | 2 | 204µs | # spent 105µs (6+99) within Moose::Meta::TypeConstraint::Role::BEGIN@5 which was called
# once (6µs+99µs) by Moose::Util::TypeConstraints::BEGIN@33 at line 5 # spent 105µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@5
# spent 99µs making 1 call to metaclass::import |
| 6 | |||||
| 7 | 3 | 26µs | 2 | 65µs | # spent 38µs (10+27) within Moose::Meta::TypeConstraint::Role::BEGIN@7 which was called
# once (10µs+27µs) by Moose::Util::TypeConstraints::BEGIN@33 at line 7 # spent 38µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@7
# spent 27µs making 1 call to Exporter::import |
| 8 | 3 | 41µs | 1 | 4µs | # spent 4µs within Moose::Meta::TypeConstraint::Role::BEGIN@8 which was called
# once (4µs+0s) by Moose::Util::TypeConstraints::BEGIN@33 at line 8 # spent 4µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@8 |
| 9 | |||||
| 10 | 1 | 700ns | our $VERSION = '0.98'; | ||
| 11 | 1 | 14µs | $VERSION = eval $VERSION; | ||
| 12 | 1 | 300ns | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 13 | |||||
| 14 | 3 | 341µs | 2 | 97µs | # spent 52µs (6+45) within Moose::Meta::TypeConstraint::Role::BEGIN@14 which was called
# once (6µs+45µs) by Moose::Util::TypeConstraints::BEGIN@33 at line 14 # spent 52µs making 1 call to Moose::Meta::TypeConstraint::Role::BEGIN@14
# spent 46µs making 1 call to base::import |
| 15 | |||||
| 16 | 1 | 3µs | 2 | 295µs | __PACKAGE__->meta->add_attribute('role' => ( # spent 279µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 16µs making 1 call to Moose::Meta::TypeConstraint::Role::meta |
| 17 | reader => 'role', | ||||
| 18 | )); | ||||
| 19 | |||||
| 20 | sub new { | ||||
| 21 | my ( $class, %args ) = @_; | ||||
| 22 | |||||
| 23 | $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object'); | ||||
| 24 | my $self = $class->_new(\%args); | ||||
| 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 | } @{ Class::MOP::class_of($self->role)->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 defined $other; | ||||
| 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 | |||||
| 71 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | ||||
| 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 | ||||
| 81 | return 1 if Class::MOP::class_of($self->role)->does_role( $type_or_name_or_role ); | ||||
| 82 | } | ||||
| 83 | |||||
| 84 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_role); | ||||
| 85 | |||||
| 86 | return unless defined $type; | ||||
| 87 | |||||
| 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 | ||||
| 91 | return Class::MOP::class_of($self->role)->does_role( $type->role ); | ||||
| 92 | } else { | ||||
| 93 | # the only other thing we are a subtype of is Object | ||||
| 94 | $self->SUPER::is_subtype_of($type); | ||||
| 95 | } | ||||
| 96 | } | ||||
| 97 | |||||
| 98 | sub create_child_type { | ||||
| 99 | my ($self, @args) = @_; | ||||
| 100 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); | ||||
| 101 | } | ||||
| 102 | |||||
| 103 | 1 | 7µs | 1; | ||
| 104 | |||||
| 105 | __END__ | ||||
| 106 | |||||
| 107 | =pod | ||||
| 108 | |||||
| 109 | =head1 NAME | ||||
| 110 | |||||
| 111 | Moose::Meta::TypeConstraint::Role - Role/TypeConstraint parallel hierarchy | ||||
| 112 | |||||
| 113 | =head1 DESCRIPTION | ||||
| 114 | |||||
| 115 | This class represents type constraints for a role. | ||||
| 116 | |||||
| 117 | =head1 INHERITANCE | ||||
| 118 | |||||
| 119 | C<Moose::Meta::TypeConstraint::Role> is a subclass of | ||||
| 120 | L<Moose::Meta::TypeConstraint>. | ||||
| 121 | |||||
| 122 | =head1 METHODS | ||||
| 123 | |||||
| 124 | =over 4 | ||||
| 125 | |||||
| 126 | =item B<< Moose::Meta::TypeConstraint::Role->new(%options) >> | ||||
| 127 | |||||
| 128 | This creates a new role type constraint based on the given | ||||
| 129 | C<%options>. | ||||
| 130 | |||||
| 131 | It takes the same options as its parent, with two exceptions. First, | ||||
| 132 | it requires an additional option, C<role>, which is name of the | ||||
| 133 | constraint's role. Second, it automatically sets the parent to the | ||||
| 134 | C<Object> type. | ||||
| 135 | |||||
| 136 | The constructor also overrides the hand optimized type constraint with | ||||
| 137 | one it creates internally. | ||||
| 138 | |||||
| 139 | =item B<< $constraint->role >> | ||||
| 140 | |||||
| 141 | Returns the role name associated with the constraint. | ||||
| 142 | |||||
| 143 | =item B<< $constraint->parents >> | ||||
| 144 | |||||
| 145 | Returns all the type's parent types, corresponding to the roles that | ||||
| 146 | its role does. | ||||
| 147 | |||||
| 148 | =item B<< $constraint->is_subtype_of($type_name_or_object) >> | ||||
| 149 | |||||
| 150 | If the given type is also a role type, then this checks that the | ||||
| 151 | type's role does the other type's role. | ||||
| 152 | |||||
| 153 | Otherwise it falls back to the implementation in | ||||
| 154 | L<Moose::Meta::TypeConstraint>. | ||||
| 155 | |||||
| 156 | =item B<< $constraint->create_child_type(%options) >> | ||||
| 157 | |||||
| 158 | This returns a new L<Moose::Meta::TypeConstraint> object with the type | ||||
| 159 | as its parent. | ||||
| 160 | |||||
| 161 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Role> | ||||
| 162 | object! | ||||
| 163 | |||||
| 164 | =back | ||||
| 165 | |||||
| 166 | =head1 BUGS | ||||
| 167 | |||||
| 168 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 169 | |||||
| 170 | =head1 AUTHOR | ||||
| 171 | |||||
| 172 | Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt> | ||||
| 173 | |||||
| 174 | =head1 COPYRIGHT AND LICENSE | ||||
| 175 | |||||
| 176 | Copyright 2006-2010 by Infinity Interactive, Inc. | ||||
| 177 | |||||
| 178 | L<http://www.iinteractive.com> | ||||
| 179 | |||||
| 180 | This library is free software; you can redistribute it and/or modify | ||||
| 181 | it under the same terms as Perl itself. | ||||
| 182 | |||||
| 183 | =cut |