DynamicComponent/ModelToControllerReflector.pm

filter: Statementstmt 35.4% 22 / 62 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 1 totaltotal 33.3% 27 / 81 CC 7 SCAR 23.8
CC
7
Cov
32%
_reflect_model_to_controller
30.0
__ANON__
6.0
_setup_dynamic_controllers
6.0
CRAP 10.8
↑ Summary DynamicComponent/ModelToControllerReflector/Strategy/InterfaceRoles.pm »
1 package CatalystX::DynamicComponent::ModelToControllerReflector;
2 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moose::Util qw/does_role/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use MooseX::Types::Moose qw/Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶my $mangle_attributes_on_generated_methods = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
9 0 my ($meta, $config) = @_;
10 0 foreach my $name (keys %{ $config->{methods}}) {
11 0 my $m = $meta->get_method($name);
12 0 $meta->register_method_attributes($m->body, ['Local']);
13 }
14 6 };
15  
16 6 with 'CatalystX::DynamicComponent' => {
17 name => '_setup_dynamic_controller',
18 pre_immutable_hook => $mangle_attributes_on_generated_methods,
19 };
20  
21 6 requires 'setup_components';
22  
23 0 ▶after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24  
25 0 ▶sub _setup_dynamic_controllers {
Subroutine _setup_dynamic_controllers: not called CC=2 SCAR=17.9
26 0 my ($app) = @_;
27  
28 0 my @model_names = grep { /::Model::/ } keys %{ $app->components };
29 0 foreach my $model_name (@model_names) {
30 0 $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
31 }
32 }
33  
34 6 my $interface = 'CatalystX::DynamicComponent::ModelToControllerReflector::Strategy';
35 6 role_type $interface;
36  
37 0 ▶sub _reflect_model_to_controller {
Subroutine _reflect_model_to_controller: not called CC=5 SCAR=34.0
38 0 my ( $app, $model_name, $model ) = @_;
39  
40 # Model passed in as MyApp::Model::Foo, strip MyApp
41 0 $model_name =~ s/^[^:]+:://;
42  
43 # Get Controller::Foo
44 0 my $controller_name = $model_name;
45 0 $controller_name =~ s/^Model::/Controller::/;
46  
47 # Get Foo
48 0 my $suffix = $model_name;
49 0 $suffix =~ s/Model:://;
50  
51 0 my %controller_methods;
52 # FIXME - Abstract this strategy crap out.
53  
54 my $config = exists $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'}
55 0 ▶ ? $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'} ? : 0 0 0
56 0 ▶ my $strategy = exists $config->{reflection_strategy} ? $config->{reflection_strategy} : 'InterfaceRoles';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'reflection_strategy'} ? : 0 0 0
57 0 $strategy = "CatalystX::DynamicComponent::ModelToControllerReflector::Strategy::$strategy";
58 0 Class::MOP::load_class($strategy);
59 0 $strategy->new;
60  
61 0 my $model_methods = $model->meta->get_method_map;
62 0 foreach my $method_name ( $strategy->get_reflected_method_list($app, $model_name, $model) ) {
63 # Note need to pass model name, as the method actually comes from
64 # the underlying model class, not the Catalyst shim class we autogenerated.
65 $controller_methods{$method_name} =
66 0 $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
67 }
68  
69 # Shallow copy so we don't stuff method refs in config
70 0 ▶ my $controller_config = { %{$app->config->{$controller_name}||{}} };
Condition0%0/2
$app->config->{$controller_name} || {}
l!l
00
MC/DC0%0/1
$app->config->{$controller_name} || {}
$app->config->{$controller_name}
Truth table0%0/2
A
A= $app->config->{$controller_name}
Aresult
11
00
71  
72 0 $controller_config->{methods} = \%controller_methods;
73 0 $app->_setup_dynamic_controller( $controller_name, $controller_config );
74 }
75  
76 0 ▶sub generate_reflected_controller_action_method {
Subroutine generate_reflected_controller_action_method: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ( $app, $model, $method ) = @_;
78 0 my $method_name = $method->name; # Is it worth passing the actual method object here?
79 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 my ($self, $c, @args) = @_;
81 0 $c->res->header('X-From-Model', $model);
82 0 my $response = $c->model($model)->$method_name( { name => $args[0] });
83 0 $c->res->header('X-From-Model-Data', $response->{body});
84 0 $c->res->body('OK');
85 0 $c->stash->{response} = $response;
86 0 };
87 }
88  
89 6 1;
90  
91 __END__
↑ Summary DynamicComponent/ModelToControllerReflector/Strategy/InterfaceRoles.pm »