DynamicComponent.pm

filter: Statementstmt 93.2% 152 / 163 Branchbran 63.6% 28 / 44 Conditioncond 53.3% 16 / 30 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 79.8% 218 / 273 CC 37 SCAR 20.0
CC
37
Cov
83%
_setup_dynamic_controller
10.7
_setup_dynamic_model
10.7
dynamic_component_method
10.7
CRAP 7.4
« DynamicComponent/ModelToControllerReflector/Strategy/InterfaceRoles.pm ↑ Summary DynamicComponent/ModelsFromConfig/InterfaceRoles.pm »
1 package CatalystX::DynamicComponent;
2 7 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use MooseX::Types::Moose qw/Str CodeRef HashRef ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 enum __PACKAGE__ . '::ResolveStrategy' => qw/
10 merge
11 replace
12 /;
13  
14 1 our $VERSION = "0.000000_01";
15  
16 1 parameter 'name' => (
17 isa => Str,
18 required => 1,
19 );
20  
21 1 parameter 'pre_immutable_hook' => (
22 isa => CodeRef|Str,
23 predicate => 'has_pre_immutable_hook',
24 );
25  
26 1 my $coerceablearray = subtype ArrayRef;
27 2 ▶coerce $coerceablearray, from Str, via { [ $_ ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 my %parameters = (
30 methods => {
31 isa =>HashRef,
32 8 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 resolve_strategy => 'merge',
34 },
35 roles => {
36 isa => $coerceablearray, coerce => 1,
37 15 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 resolve_strategy => 'merge',
39 },
40 superclasses => {
41 isa => $coerceablearray, coerce => 1,
42 15 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 resolve_strategy => 'replace',
44 },
45 );
46  
47 # Shameless metaprogramming.
48 1 foreach my $name (keys %parameters) {
49 3 my $resolve_strategy = delete $parameters{$name}->{resolve_strategy};
50  
51 3 parameter $name, %{ $parameters{$name} };
52  
53 3 parameter $name . '_resolve_strategy' => (
54 isa => __PACKAGE__ . '::ResolveStrategy',
55 default => $resolve_strategy,
56 );
57 }
58  
59 # Code refs to implement the strategy types
60 my %strategies = ( # Right hand precedence where appropriate
61 19 ▶ replace => sub {
Subroutine __ANON__: called CC=6 SCAR=18.4
62 19 ▶ $_[0] = [ $_[0] ] if $_[0] && !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and not ref $_[0] 0 19 19
Condition33%1/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
0190
MC/DC0%0/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
63 19 ▶ $_[1] = [ $_[1] ] if $_[1] && !ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] and not ref $_[1] 1 18 19
Condition100%3/3
$_[1] and not ref $_[1]
!ll&&!rl&&r
1261
MC/DC100%2/2
$_[1] and not ref $_[1]
$_[1] not ref $_[1]
Truth table100%3/3
A and not(B)
A= $_[1]B= not ref $_[1]
ABresult
0X0
100
111
64 19 ▶ $_[1] ? $_[1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 7 12 19
65 },
66 23 ▶ merge => sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
67 23 ▶ $_[0] = [ $_[0] ] if $_[0] && !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and not ref $_[0] 0 23 23
Condition33%1/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
0230
MC/DC0%0/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
68 23 ▶ $_[1] = [ $_[1] ] if $_[1] && !ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] and not ref $_[1] 2 21 23
Condition100%3/3
$_[1] and not ref $_[1]
!ll&&!rl&&r
1562
MC/DC100%2/2
$_[1] and not ref $_[1]
$_[1] not ref $_[1]
Truth table100%3/3
A and not(B)
A= $_[1]B= not ref $_[1]
ABresult
0X0
100
111
69 23 ▶ if (ref($_[0]) eq 'ARRAY' || ref($_[1]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY' or ref $_[1] eq 'ARRAY') { } 12 11 23
Condition66%2/3
ref $_[0] eq "ARRAY" or ref $_[1] eq "ARRAY"
l!l&&r!l&&!r
12011
MC/DC50%1/2
ref $_[0] eq "ARRAY" or ref $_[1] eq "ARRAY"
ref $_[0] eq "ARRAY" ref $_[1] eq "ARRAY"
Truth table66%2/3
A or B
A= ref $_[0] eq "ARRAY"B= ref $_[1] eq "ARRAY"
ABresult
1X1
011
000
70 12 [ uniq( @{ $_[0] }, @{ $_[1] } ) ];
71 }
72 else {
73 11 Catalyst::Utils::merge_hashes(shift, shift);
74 }
75 },
76 1 );
77  
78 # Wrap all the crazy up in a method to generically merge configs.
79 42 ▶my $get_resolved_config = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
80 42 my ($name, $p, $config) = @_;
81 42 my $get_strategy_method_name = $name . '_resolve_strategy';
82 42 my $strategy = $strategies{$p->$get_strategy_method_name()};
83 $strategy->($p->$name, $config->{$name})
84 42 ▶ || $parameters{$name}->{default}->();
Branch: 1 branch
BranchTrueFalseTotal
&$strategy($p->$name, $config->{$name}) or $parameters{$name}{"default"}->() 42 0 42
85 1 };
86  
87 20 ▶role {
Subroutine __ANON__: called CC=1 SCAR=0.2
88 20 my $p = shift;
89 20 my $name = $p->name;
90 20 my $pre_immutable_hook = $p->pre_immutable_hook;
91  
92 8 ▶ method $name => sub {
Subroutine dynamic_component_method: called CC=9 SCAR=23.7
Subroutine _setup_dynamic_controller: called CC=9 SCAR=23.7
Subroutine _setup_dynamic_model: called CC=9 SCAR=23.7
93 8 my ($app, $name, $config) = @_;
94 8 ▶ my $appclass = blessed($app) || $app;
Condition33%3/9
builtin::blessed($app) || $app
l!l&&r!l&&!r
080
builtin::blessed($app) || $app
l!l&&r!l&&!r
040
builtin::blessed($app) || $app
l!l&&r!l&&!r
020
MC/DC0%0/2
builtin::blessed($app) || $app
builtin::blessed($app) $app
MC/DC0%0/2
builtin::blessed($app) || $app
builtin::blessed($app) $app
MC/DC0%0/2
builtin::blessed($app) || $app
builtin::blessed($app) $app
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
ABresult
1X1
011
000
95  
96 8 ▶ $config ||= {};
Condition50%3/6
$config ||= {}
l!l
80
$config ||= {}
l!l
40
$config ||= {}
l!l
20
MC/DC0%0/1
$config ||= {}
$config
MC/DC0%0/1
$config ||= {}
$config
MC/DC0%0/1
$config ||= {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
Truth table50%1/2
A
A= $config
Aresult
11
00
Truth table50%1/2
A
A= $config
Aresult
11
00
97  
98 8 my $type = $name;
99 8 $type =~ s/::.*$//;
100  
101 8 my $component_name = $appclass . '::' . $name;
102 8 my $meta = Moose->init_meta( for_class => $component_name );
103  
104 8 my @superclasses = @{ $get_resolved_config->('superclasses', $p, $config) };
105 8 ▶ push(@superclasses, 'Catalyst::' . $type) unless @superclasses;
Branch: 3 branches
BranchTrueFalseTotal
unless @superclasses 3 5 8
unless @superclasses 4 0 4
unless @superclasses 2 0 2
106 8 $meta->superclasses(@superclasses);
107  
108 8 my $methods = $get_resolved_config->('methods', $p, $config);
109 8 foreach my $method_name (keys %$methods) {
110 7 ▶ next unless $methods->{$method_name}; # Skip explicitly undef methods
Branch: 3 branches
BranchTrueFalseTotal
unless $methods->{$method_name} 0 7 7
unless $methods->{$method_name} 1 3 4
unless $methods->{$method_name} 0 2 2
111 7 $meta->add_method($method_name => $methods->{$method_name});
112 }
113  
114 8 ▶ if (my @roles = @{ $get_resolved_config->('roles', $p, $config) }) {
Branch: 3 branches
BranchTrueFalseTotal
if (my(@roles) = @{&$get_resolved_config("roles", $p, $config);}) 5 3 8
if (my(@roles) = @{&$get_resolved_config("roles", $p, $config);}) 2 2 4
if (my(@roles) = @{&$get_resolved_config("roles", $p, $config);}) 1 1 2
115 5 Moose::Util::apply_all_roles( $component_name, @roles);
116 }
117  
118 8 ▶ if ($p->has_pre_immutable_hook) {
Branch: 3 branches
BranchTrueFalseTotal
if ($p->has_pre_immutable_hook) 0 8 8
if ($p->has_pre_immutable_hook) 0 3 3
if ($p->has_pre_immutable_hook) 0 2 2
119 0 ▶ if (!ref($pre_immutable_hook)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $pre_immutable_hook) { } 0 0 0
if (not ref $pre_immutable_hook) { } 0 0 0
if (not ref $pre_immutable_hook) { } 0 0 0
120 0 $app->$pre_immutable_hook($meta, $config);
121 }
122 else {
123 0 $pre_immutable_hook->($meta, $config);
124 }
125 }
126  
127 8 $meta->make_immutable;
128  
129 8 my $instance = $app->setup_component($component_name);
130 0 $app->components->{ $component_name } = $instance;
131 20 };
132 1 };
133  
134 1 1;
135  
136 __END__
« DynamicComponent/ModelToControllerReflector/Strategy/InterfaceRoles.pm ↑ Summary DynamicComponent/ModelsFromConfig/InterfaceRoles.pm »