APIRole/Internals.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 12 / 12 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 88.4% 84 / 95 CC 12 SCAR 14.1
CC
12
Cov
95%
create_role_for
8.2
_analyze_metaclass
4.0
role_for
2.0
CRAP 4.1
↑ Summary APIRole/Meta.pm »
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
BranchTrueFalseTotal
if exists $ROLE_FOR{$meta} 4 11 15
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
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Role") 5 5 10
32  
33 10 ▶ my @roles = @{ $meta->isa('Moose::Meta::Class') ? $meta->roles : $meta->get_roles };
Branch: 1 branch
BranchTrueFalseTotal
$meta->isa('Moose::Meta::Class') ? : 5 5 10
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
BranchTrueFalseTotal
$meta->isa('Moose::Meta::Class') ? : 5 5 10
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
BranchTrueFalseTotal
if $cached_role 2 10 12
62  
63 # create and cache
64 10 ▶ my $role = Moose::Meta::Role->create(
Condition66%2/3
$name || _name_role_for($meta)
l!l&&r!l&&!r
640
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)
ABresult
1X1
011
000
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'} || []
l!l
100
MC/DC0%0/1
$metainfo->{'methods'} || []
$metainfo->{'methods'}
Truth table50%1/2
A
A= $metainfo->{'methods'}
Aresult
11
00
74  
75 # role role role your boat, gently down the stream
76 10 ▶ for(@{$metainfo->{roles} || []}, @{$metainfo->{superclasses} || []}){
Condition50%2/4
$metainfo->{'roles'} || []
l!l
100
$metainfo->{'superclasses'} || []
l!l
100
MC/DC0%0/1
$metainfo->{'roles'} || []
$metainfo->{'roles'}
MC/DC0%0/1
$metainfo->{'superclasses'} || []
$metainfo->{'superclasses'}
Truth table50%1/2
A
A= $metainfo->{'roles'}
Aresult
11
00
Truth table50%1/2
A
A= $metainfo->{'superclasses'}
Aresult
11
00
77 7 ▶ if(does_role($_, 'MooseX::APIRole::Meta')){
Branch: 1 branch
BranchTrueFalseTotal
if (does_role($_, 'MooseX::APIRole::Meta')) { } 3 4 7
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__
↑ Summary APIRole/Meta.pm »