| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package MooseX::Role::TraitConstructor; | ||||||||||||||||||||
| 4 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use List::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | our $VERSION = "0.01"; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use constant constructor_trait_param => "traits"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 4 | ▶ | sub new_with_traits { | ||||||||||||||||||
|
Subroutine
new_with_traits: called CC=4 SCAR=20.1
Pod
documented
| |||||||||||||||||||||
| 13 | 4 | my ( $class, @opts ) = @_; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 4 | my %params; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 4 | ▶ | if (scalar @opts == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 0 | ▶ | if (defined $opts[0]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 0 | ▶ | (ref($opts[0]) eq 'HASH') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | || confess "Single parameters to new() must be a HASH ref"; | ||||||||||||||||||||
| 21 | 0 | %params = %{$opts[0]}; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||
| 25 | 4 | %params = @opts; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 4 | $class->interpolate_class_from_params(\%params)->new(%params); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 4 | ▶ | sub interpolate_class_from_params { | ||||||||||||||||||
|
Subroutine
interpolate_class_from_params: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||
| 32 | 4 | my ($class, $params) = @_; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 4 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 4 | my @traits; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 4 | ▶ | if (my $traits = delete $params->{$class->constructor_trait_param($params)}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 4 | ▶ | if ( @traits = $class->process_constructor_traits($params, @$traits) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 3 | my $anon_class = Moose::Meta::Class->create_anon_class( | |||||||||||||||||||
| 41 | superclasses => [ $class ], | ||||||||||||||||||||
| 42 | roles => [ @traits ], | ||||||||||||||||||||
| 43 | cache => 1, | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | $class = $anon_class->name; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | ▶ | return ( wantarray ? ( $class, @traits ) : $class ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 4 | ▶ | sub process_constructor_traits { | ||||||||||||||||||
|
Subroutine
process_constructor_traits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 54 | 4 | my ( $class, $params, @traits ) = @_; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 4 | $class->filter_constructor_traits( $params, $class->resolve_constructor_traits( $params, @traits ) ); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 5 | ▶ | sub resolve_constructor_traits { | ||||||||||||||||||
|
Subroutine
resolve_constructor_traits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 61 | 5 | my ( $class, $params, @traits ) = @_; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 5 | my $root = $class->guess_original_class_name($params); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 5 | map { $class->resolve_constructor_trait($params, $root, $_) } @traits; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 7 | ▶ | sub guess_original_class_name { | ||||||||||||||||||
|
Subroutine
guess_original_class_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 69 | 7 | my ( $class, $params ) = @_; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 7 | my $meta = $class->meta; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 7 | ▶ | if ( $meta->is_anon_class ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 1 | ▶ | if ( my $root = List::Util::first(sub { not $_->meta->is_anon_class }, $meta->linearized_isa ) ) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 1 | return $root; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 6 | return $class; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 7 | ▶ | sub resolve_constructor_trait { | ||||||||||||||||||
|
Subroutine
resolve_constructor_trait: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||
| 83 | 7 | my ( $class, $params, $possible_root, $trait ) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 7 | ▶ | if ( ref $trait ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | return $trait->anme; | |||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||
| 88 | 7 | my $processed_trait; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | { | ||||||||||||||||||||
| 91 | 7 | local $@; | |||||||||||||||||||
| 92 | 7 | ▶ | if ( $processed_trait = $class->process_trait_name($trait, $params, $possible_root) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 7 | ▶ | if ( eval { Class::MOP::load_class($processed_trait); 1 } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 4 | return $processed_trait; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 3 | ▶ | if ( eval { Class::MOP::load_class($trait); 1 } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 2 | return $trait; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | require Carp; | |||||||||||||||||||
| 104 | 1 | ▶ | Carp::croak("Couldn't load $trait" . ( $processed_trait ? " or $processed_trait" : "" ) . " to mix in with $class" . ( $class->meta->is_anon_class ? " ($possible_root)" : "" )); | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 7 | ▶ | sub process_trait_name { | ||||||||||||||||||
|
Subroutine
process_trait_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 109 | 7 | my ( $class, $trait, $params, $possible_root) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 7 | return join "::", $possible_root, $trait; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 5 | ▶ | sub filter_constructor_traits { | ||||||||||||||||||
|
Subroutine
filter_constructor_traits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 115 | 5 | my ( $class, $params, @traits ) = @_; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 5 | return grep { not $class->does($_) } @traits; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 1 | __PACKAGE__ | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||