| 1 | package MooseX::Singleton::Role::Meta::Method::Constructor; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 7 | our $VERSION = '0.30'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | if ( $Moose::VERSION < 1.9900 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | override _initialize_body => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 8 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # TODO: | ||||||||||||||||||||||||||||
| 11 | # the %options should also include a both | ||||||||||||||||||||||||||||
| 12 | # a call 'initializer' and call 'SUPER::' | ||||||||||||||||||||||||||||
| 13 | # options, which should cover approx 90% | ||||||||||||||||||||||||||||
| 14 | # of the possible use cases (even if it | ||||||||||||||||||||||||||||
| 15 | # requires some adaption on the part of | ||||||||||||||||||||||||||||
| 16 | # the author, after all, nothing is free) | ||||||||||||||||||||||||||||
| 17 | 0 | my $source = 'sub {'; | |||||||||||||||||||||||||||
| 18 | 0 | $source .= "\n" . 'my $class = shift;'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | $source .= "\n" | |||||||||||||||||||||||||||
| 21 | . 'my $existing = do { no strict "refs"; no warnings "once"; \${"$class\::singleton"}; };'; | ||||||||||||||||||||||||||||
| 22 | 0 | $source .= "\n" . 'return ${$existing} if ${$existing};'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | $source .= "\n" . 'return $class->Moose::Object::new(@_)'; | |||||||||||||||||||||||||||
| 25 | 0 | $source | |||||||||||||||||||||||||||
| 26 | .= "\n" | ||||||||||||||||||||||||||||
| 27 | . ' if $class ne \'' | ||||||||||||||||||||||||||||
| 28 | . $self->associated_metaclass->name . '\';'; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | $source .= $self->_generate_params( '$params', '$class' ); | |||||||||||||||||||||||||||
| 31 | 0 | $source .= $self->_generate_instance( '$instance', '$class' ); | |||||||||||||||||||||||||||
| 32 | 0 | $source .= $self->_generate_slot_initializers; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | $source .= ";\n" . $self->_generate_triggers(); | |||||||||||||||||||||||||||
| 35 | 0 | $source .= ";\n" . $self->_generate_BUILDALL(); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | $source .= ";\n" . 'return ${$existing} = $instance'; | |||||||||||||||||||||||||||
| 38 | 0 | $source .= ";\n" . '}'; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | warn $source if $self->options->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $attrs = $self->_attributes; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my @type_constraints | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | = map { $_->can('type_constraint') ? $_->type_constraint : undef } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | @$attrs; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my @type_constraint_bodies | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | = map { defined $_ ? $_->_compiled_type_constraint : undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | @type_constraints; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $defaults = [map { $_->default } @$attrs]; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my ( $code, $e ) = $self->_compile_code( | |||||||||||||||||||||||||||
| 54 | code => $source, | ||||||||||||||||||||||||||||
| 55 | environment => { | ||||||||||||||||||||||||||||
| 56 | '$meta' => \$self, | ||||||||||||||||||||||||||||
| 57 | '$attrs' => \$attrs, | ||||||||||||||||||||||||||||
| 58 | '$defaults' => \$defaults, | ||||||||||||||||||||||||||||
| 59 | '@type_constraints' => \@type_constraints, | ||||||||||||||||||||||||||||
| 60 | '@type_constraint_bodies' => \@type_constraint_bodies, | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $self->throw_error( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e", | ||||||||||||||||||||||||||||
| 66 | error => $e, data => $source ) | ||||||||||||||||||||||||||||
| 67 | if $e; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $self->{'body'} = $code; | |||||||||||||||||||||||||||
| 70 | 0 | }; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Ideally we'd be setting this in the constructor, but the new() methods in | ||||||||||||||||||||||||||||
| 74 | # what the parent classes are not well-factored. | ||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||
| 76 | # This is all a nasty hack, though. We need to fix Class::MOP::Inlined to | ||||||||||||||||||||||||||||
| 77 | # allow constructor class roles to say "if the parent class has role X, | ||||||||||||||||||||||||||||
| 78 | # inline". | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | override _expected_method_class => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | my $super_value = super(); | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | if ( $super_value eq 'Moose::Object' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | for my $parent ( map { Class::MOP::class_of($_) } | |||||||||||||||||||||||||||
| 85 | $self->associated_metaclass->superclasses ) { | ||||||||||||||||||||||||||||
| 86 | return $parent->name | ||||||||||||||||||||||||||||
| 87 | if $parent->is_anon_class | ||||||||||||||||||||||||||||
| 88 | 2 | && grep { $_->name eq 'Moose::Object' } | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | map { Class::MOP::class_of($_) } $parent->superclasses; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$parent->is_anon_class and grep {$_->name eq "Moose::Object";} map({Class::MOP::class_of($_);} $parent->superclasses)
MC/DC0%0/2
$parent->is_anon_class and grep {$_->name eq "Moose::Object";} map({Class::MOP::class_of($_);} $parent->superclasses)
$parent->is_anon_class
grep {$_->name eq "Moose::Object";} map({Class::MOP::class_of($_);} $parent->superclasses)
Truth table33%1/3
A and B
A= $parent->is_anon_classB= grep {$_->name eq "Moose::Object";} map({Class::MOP::class_of($_);} $parent->superclasses)
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $super_value; | |||||||||||||||||||||||||||
| 94 | 7 | }; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 7 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # ABSTRACT: Constructor method role for MooseX::Singleton | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||