« 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
Condition33%1/3
$_[0] and not ref $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 19 | ▶ | $_[1] = [ $_[1] ] if $_[1] && !ref $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[1] and not ref $_[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 19 | ▶ | $_[1] ? $_[1] : $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 23 | ▶ | merge => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 23 | ▶ | $_[0] = [ $_[0] ] if $_[0] && !ref $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and not ref $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 23 | ▶ | $_[1] = [ $_[1] ] if $_[1] && !ref $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[1] and not ref $_[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 23 | ▶ | if (ref($_[0]) eq 'ARRAY' || ref($_[1]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] eq "ARRAY" or ref $_[1] eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
MC/DC0%0/2
builtin::blessed($app) || $app
builtin::blessed($app)
$app
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
Truth table33%1/3
A || B
A= builtin::blessed($app)B= $app
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 8 | ▶ | $config ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$config ||= {}
$config ||= {}
$config ||= {}
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
Truth table50%1/2
A
A= $config
Truth table50%1/2
A
A= $config
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | Moose::Util::apply_all_roles( $component_name, @roles); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 8 | ▶ | if ($p->has_pre_immutable_hook) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (!ref($pre_immutable_hook)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||