| 1 | package MooseX::APIRole::Internals; | ||||||||||||||||||||||
| 2 | 3 | ▶ | BEGIN { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 3 | $MooseX::APIRole::Internals::VERSION = '0.01'; | |||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||
| 5 | # ABSTRACT: utility functions for MooseX::APIRole | ||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 3 | ▶ | use true; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 3 | ▶ | use Moose::Meta::Role; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 3 | ▶ | use Moose::Util qw(does_role); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 3 | ▶ | use Hash::Util::FieldHash qw(fieldhash); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 3 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | exports => [qw/role_for create_role_for/], | ||||||||||||||||||||||
| 16 | 3 | }; | |||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | # fieldhash so that when a class goes away, so does the role | ||||||||||||||||||||||
| 19 | 3 | fieldhash my %ROLE_FOR; | |||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 15 | ▶ | sub role_for { | ||||||||||||||||||||
|
Subroutine
role_for: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 22 | 15 | my $meta = shift; | |||||||||||||||||||||
| 23 | 15 | ▶ | return $ROLE_FOR{$meta} if exists $ROLE_FOR{$meta}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 24 | 11 | return; | |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 10 | ▶ | sub _analyze_metaclass { | ||||||||||||||||||||
|
Subroutine
_analyze_metaclass: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 28 | 10 | my $meta = shift; | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 10 | my @methods = $meta->get_method_list; | |||||||||||||||||||||
| 31 | 10 | ▶ | push @methods, $meta->get_required_method_list if $meta->isa('Moose::Meta::Role'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 10 | ▶ | my @roles = @{ $meta->isa('Moose::Meta::Class') ? $meta->roles : $meta->get_roles }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | # we do this for both classes and roles, but roles do not have superclasses | ||||||||||||||||||||||
| 36 | 10 | ▶ | my @superclasses = $meta->isa('Moose::Meta::Class') ? $meta->superclasses : (); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | return { | ||||||||||||||||||||||
| 39 | 31 | methods => [grep { $_ ne 'meta' } @methods], | |||||||||||||||||||||
| 40 | roles => \@roles, | ||||||||||||||||||||||
| 41 | 10 | superclasses => [map { $_->meta } grep { $_ ne 'Moose::Object' } @superclasses], | |||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 4 | ▶ | sub _name_role_for { | ||||||||||||||||||||
|
Subroutine
_name_role_for: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 46 | 4 | my $meta = shift; | |||||||||||||||||||||
| 47 | 4 | my $name = $meta->name; | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 4 | $name =~ s/\|/::__AND__::/g; | |||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | # this is so is_anon_role returns true. hopefully it doesn't fuck | ||||||||||||||||||||||
| 52 | # up destruction too much. | ||||||||||||||||||||||
| 53 | 4 | return "Moose::Meta::Role::__ANON__::SERIAL::__AUTOGEN_FOR__::$name"; | |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | 12 | ▶ | sub create_role_for { | ||||||||||||||||||||
|
Subroutine
create_role_for: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||
| 57 | 12 | my ($meta, $name) = @_; | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | # already cached? | ||||||||||||||||||||||
| 60 | 12 | my $cached_role = role_for($meta); | |||||||||||||||||||||
| 61 | 12 | ▶ | return $cached_role if $cached_role; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | # create and cache | ||||||||||||||||||||||
| 64 | 10 | ▶ | my $role = Moose::Meta::Role->create( | ||||||||||||||||||||
|
Condition66%2/3
$name || _name_role_for($meta)
MC/DC0%0/2
$name || _name_role_for($meta)
$name
_name_role_for($meta)
Truth table66%2/3
A || B
A= $nameB= _name_role_for($meta)
| |||||||||||||||||||||||
| 65 | $name || _name_role_for($meta), | ||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||
| 67 | 10 | $ROLE_FOR{$meta} = $role; | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | # analyze the metaclass | ||||||||||||||||||||||
| 70 | 10 | my $metainfo = _analyze_metaclass($meta); | |||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | # any methods that this class/role requires, the new role requires | ||||||||||||||||||||||
| 73 | 10 | ▶ | $role->add_required_methods(@{$metainfo->{methods} || []}); | ||||||||||||||||||||
|
Condition50%1/2
$metainfo->{'methods'} || []
MC/DC0%0/1
$metainfo->{'methods'} || []
$metainfo->{'methods'}
Truth table50%1/2
A
A= $metainfo->{'methods'}
| |||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | # role role role your boat, gently down the stream | ||||||||||||||||||||||
| 76 | 10 | ▶ | for(@{$metainfo->{roles} || []}, @{$metainfo->{superclasses} || []}){ | ||||||||||||||||||||
|
Condition50%2/4
$metainfo->{'roles'} || []
$metainfo->{'superclasses'} || []
MC/DC0%0/1
$metainfo->{'roles'} || []
$metainfo->{'roles'}
MC/DC0%0/1
$metainfo->{'superclasses'} || []
$metainfo->{'superclasses'}
Truth table50%1/2
A
A= $metainfo->{'roles'}
Truth table50%1/2
A
A= $metainfo->{'superclasses'}
| |||||||||||||||||||||||
| 77 | 7 | ▶ | if(does_role($_, 'MooseX::APIRole::Meta')){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | # this will vivify the parent api_role; we do it here because | ||||||||||||||||||||||
| 79 | # we don't want to vivify the current class's api_role while we are | ||||||||||||||||||||||
| 80 | # vivifying the current class's api_role :) | ||||||||||||||||||||||
| 81 | 3 | $_->api_role->apply($role); | |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||
| 84 | 4 | create_role_for($_)->apply($role); | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | 10 | return $role; | |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||