| 1 | 117 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 117 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 117 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Mite::Trait::HasConstructor; | ||||||||||||||||||||||||||||
| 6 | 117 | ▶ | use Mite::Miteception -role, -all; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 117 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 9 | 117 | our $VERSION = '0.013000'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 117 | requires qw( | |||||||||||||||||||||||||||
| 12 | linear_isa | ||||||||||||||||||||||||||||
| 13 | _get_parent | ||||||||||||||||||||||||||||
| 14 | _compile_meta | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 118 | ▶ | around compilation_stages => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 18 | 118 | my ( $next, $self ) = ( shift, shift ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Check if we are inheriting from a Mite class in this project | ||||||||||||||||||||||||||||
| 21 | 118 | my $inherit_from_mite = do { | |||||||||||||||||||||||||||
| 22 | # First parent | ||||||||||||||||||||||||||||
| 23 | 118 | my $first_isa = do { | |||||||||||||||||||||||||||
| 24 | 118 | my @isa = $self->linear_isa; | |||||||||||||||||||||||||||
| 25 | 118 | shift @isa; | |||||||||||||||||||||||||||
| 26 | 118 | shift @isa; | |||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | 118 | ▶ | !! ( $first_isa and $self->_get_parent( $first_isa ) ); | ||||||||||||||||||||||||||
|
Condition100%3/3
$first_isa && $self->_get_parent($first_isa)
MC/DC100%2/2
$first_isa && $self->_get_parent($first_isa)
$first_isa
$self->_get_parent($first_isa)
Truth table100%3/3
A && B
A= $first_isaB= $self->_get_parent($first_isa)
| |||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 118 | my @stages = $self->$next( @_ ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Need a constructor if we're not inheriting from Mite, | ||||||||||||||||||||||||||||
| 34 | # or if we define any new attributes. | ||||||||||||||||||||||||||||
| 35 | push @stages, '_compile_new' | ||||||||||||||||||||||||||||
| 36 | if !$inherit_from_mite | ||||||||||||||||||||||||||||
| 37 | 118 | ▶ | || keys %{ $self->attributes }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $inherit_from_mite or keys %{$self->attributes;}
MC/DC100%2/2
not $inherit_from_mite or keys %{$self->attributes;}
not $inherit_from_mite
keys %{$self->attributes;}
Truth table100%3/3
not(A) or B
A= not $inherit_from_miteB= keys %{$self->attributes;}
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Only need these stages if not already inheriting from Mite | ||||||||||||||||||||||||||||
| 40 | 118 | ▶ | push @stages, qw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | _compile_buildall_method | ||||||||||||||||||||||||||||
| 42 | ) unless $inherit_from_mite; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 118 | return @stages; | |||||||||||||||||||||||||||
| 45 | 117 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 106 | ▶ | sub _compile_new { | ||||||||||||||||||||||||||
|
Subroutine
_compile_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 106 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 106 | my @vars = ('$class', '$self', '$args', '$meta'); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 106 | return sprintf <<'CODE', $self->_compile_meta(@vars), $self->_compile_bless(@vars), $self->_compile_buildargs(@vars), $self->_compile_init_attributes(@vars), $self->_compile_buildall(@vars, '$no_build'), $self->_compile_strict_constructor(@vars); | |||||||||||||||||||||||||||
| 52 | # Standard Moose/Moo-style constructor | ||||||||||||||||||||||||||||
| 53 | sub new { | ||||||||||||||||||||||||||||
| 54 | my $class = ref($_[0]) ? ref(shift) : shift; | ||||||||||||||||||||||||||||
| 55 | my $meta = %s; | ||||||||||||||||||||||||||||
| 56 | my $self = %s; | ||||||||||||||||||||||||||||
| 57 | my $args = %s; | ||||||||||||||||||||||||||||
| 58 | my $no_build = delete $args->{__no_BUILD__}; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | %s | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Call BUILD methods | ||||||||||||||||||||||||||||
| 63 | %s | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Unrecognized parameters | ||||||||||||||||||||||||||||
| 66 | %s | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | return $self; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | CODE | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 106 | ▶ | sub _compile_bless { | ||||||||||||||||||||||||||
|
Subroutine
_compile_bless: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 74 | 106 | my ( $self, $classvar, $selfvar, $argvar, $metavar ) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 106 | my $simple_bless = "bless {}, $classvar"; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Force parents to be loaded | ||||||||||||||||||||||||||||
| 79 | 106 | $self->parents; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # First parent with &new | ||||||||||||||||||||||||||||
| 82 | 106 | my ( $first_isa ) = do { | |||||||||||||||||||||||||||
| 83 | 106 | my @isa = $self->linear_isa; | |||||||||||||||||||||||||||
| 84 | 106 | shift @isa; | |||||||||||||||||||||||||||
| 85 | 117 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 106 | grep +(defined &{$_.'::new'}), @isa; | |||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # If we're not inheriting from anything with a constructor: simple case | ||||||||||||||||||||||||||||
| 90 | 106 | ▶ | $first_isa or return $simple_bless; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Inheriting from a Mite class in this project: simple case | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my $first_parent = $self->_get_parent( $first_isa ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | and return $simple_bless; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Inheriting from a Moose/Moo/Mite/Class::Tiny class: | ||||||||||||||||||||||||||||
| 97 | # call buildargs | ||||||||||||||||||||||||||||
| 98 | # set $args->{__no_BUILD__} | ||||||||||||||||||||||||||||
| 99 | # call parent class constructor | ||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ( $first_isa->can( 'BUILDALL' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | return sprintf 'do { my %s = %s; %s->{__no_BUILD__} = 1; %s->SUPER::new( %s ) }', | |||||||||||||||||||||||||||
| 102 | $argvar, $self->_compile_buildargs($classvar, $selfvar, $argvar, $metavar), $argvar, $classvar, $argvar; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Inheriting from some random class | ||||||||||||||||||||||||||||
| 106 | # call FOREIGNBUILDARGS if it exists | ||||||||||||||||||||||||||||
| 107 | # pass return value or @_ to parent class constructor | ||||||||||||||||||||||||||||
| 108 | 1 | return sprintf '%s->SUPER::new( %s->{HAS_FOREIGNBUILDARGS} ? %s->FOREIGNBUILDARGS( @_ ) : @_ )', | |||||||||||||||||||||||||||
| 109 | $classvar, $metavar, $classvar; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 107 | ▶ | sub _compile_buildargs { | ||||||||||||||||||||||||||
|
Subroutine
_compile_buildargs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 107 | my ( $self, $classvar, $selfvar, $argvar, $metavar ) = @_; | |||||||||||||||||||||||||||
| 114 | 107 | return sprintf '%s->{HAS_BUILDARGS} ? %s->BUILDARGS( @_ ) : { ( @_ == 1 ) ? %%{$_[0]} : @_ }', | |||||||||||||||||||||||||||
| 115 | $metavar, $classvar; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 106 | ▶ | sub _compile_strict_constructor { | ||||||||||||||||||||||||||
|
Subroutine
_compile_strict_constructor: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 119 | 106 | my ( $self, $classvar, $selfvar, $argvar, $metavar ) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | my @allowed = | ||||||||||||||||||||||||||||
| 122 | 121 | grep { defined $_ } | |||||||||||||||||||||||||||
| 123 | 118 | map { ( $_->init_arg, $_->_all_aliases ) } | |||||||||||||||||||||||||||
| 124 | 106 | values %{ $self->all_attributes }; | |||||||||||||||||||||||||||
| 125 | 106 | my $check = do { | |||||||||||||||||||||||||||
| 126 | 106 | local $Type::Tiny::AvoidCallbacks = 1; | |||||||||||||||||||||||||||
| 127 | 106 | my $enum = Enum->of( @allowed ); | |||||||||||||||||||||||||||
| 128 | 106 | ▶ | $enum->can( '_regexp' ) # not part of official API | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | ? sprintf( '/\\A%s\\z/', $enum->_regexp ) | ||||||||||||||||||||||||||||
| 130 | : $enum->inline_check( '$_' ); | ||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 106 | my $code = sprintf 'my @unknown = grep not( %s ), keys %%{%s}; @unknown and %s( "Unexpected keys in constructor: " . join( q[, ], sort @unknown ) );', | |||||||||||||||||||||||||||
| 134 | $check, $argvar, $self->_function_for_croak; | ||||||||||||||||||||||||||||
| 135 | 106 | ▶ | if ( my $autolax = $self->autolax ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $code = "if ( $autolax ) { $code }"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 106 | return $code; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 106 | ▶ | sub _compile_buildall { | ||||||||||||||||||||||||||
|
Subroutine
_compile_buildall: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 106 | my ( $self, $classvar, $selfvar, $argvar, $metavar, $nobuildvar ) = @_; | |||||||||||||||||||||||||||
| 143 | 106 | return sprintf '%s->BUILDALL( %s ) if ( ! %s and @{ %s->{BUILD} || [] } );', | |||||||||||||||||||||||||||
| 144 | $selfvar, $argvar, $nobuildvar, $metavar; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 94 | ▶ | sub _compile_buildall_method { | ||||||||||||||||||||||||||
|
Subroutine
_compile_buildall_method: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 148 | 94 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 94 | return sprintf <<'CODE', $self->_compile_meta( '$class', '$_[0]', '$_[1]', '$meta' ), | |||||||||||||||||||||||||||
| 151 | # Used by constructor to call BUILD methods | ||||||||||||||||||||||||||||
| 152 | sub BUILDALL { | ||||||||||||||||||||||||||||
| 153 | my $class = ref( $_[0] ); | ||||||||||||||||||||||||||||
| 154 | my $meta = %s; | ||||||||||||||||||||||||||||
| 155 | $_->( @_ ) for @{ $meta->{BUILD} || [] }; | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | CODE | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 117 | 1; | |||||||||||||||||||||||||||