| 1 | package MooX::SetOnce; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.001002'; | |||||||||||||||||||||||||||
| 5 | 2 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Class::Method::Modifiers qw(install_modifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub import { | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 12 | 2 | my $target = caller; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | install_modifier $target, 'around', 'has', sub { | ||||||||||||||||||||||||||||
| 15 | 14 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.2
| |||||||||||||||||||||||||||||
| 16 | 14 | my ($attr, %opts) = @_; | |||||||||||||||||||||||||||
| 17 | return $orig->($attr, %opts) | ||||||||||||||||||||||||||||
| 18 | 14 | ▶ | unless delete $opts{once}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 12 | my $is = $opts{is}; | |||||||||||||||||||||||||||
| 21 | 12 | my $writer = $opts{writer}; | |||||||||||||||||||||||||||
| 22 | 12 | ▶ | if ($is eq 'rw') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 23 | 10 | ▶ | $writer ||= $attr; | ||||||||||||||||||||||||||
|
Condition66%2/3
$writer ||= $attr
MC/DC0%0/2
$writer ||= $attr
$writer
$attr
Truth table66%2/3
A ||= B
A= $writerB= $attr
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | elsif ($is eq 'rwp') { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | $writer ||= "_set_$attr"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$writer ||= "_set_$attr"
MC/DC0%0/1
$writer ||= "_set_$attr"
$writer
Truth table50%1/2
A
A= $writer
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||||||||||
| 29 | 0 | croak "SetOnce can't be used on read-only accessors"; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 12 | ▶ | my $predicate = $opts{predicate} ||= '_has_' . $attr; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"predicate"} ||= '_has_' . $attr
MC/DC100%1/1
$opts{"predicate"} ||= '_has_' . $attr
$opts{"predicate"}
Truth table100%2/2
A
A= $opts{"predicate"}
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 12 | ▶ | $opts{moosify} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"moosify"} ||= []
MC/DC0%0/1
$opts{"moosify"} ||= []
$opts{"moosify"}
Truth table50%1/2
A
A= $opts{"moosify"}
| |||||||||||||||||||||||||||||
| 34 | 12 | push @{$opts{moosify}}, sub { | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ($spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 36 | require # hide from CPANTS | ||||||||||||||||||||||||||||
| 37 | 0 | MooseX::SetOnce; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $spec->{traits} ||= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$spec->{"traits"} ||= []
MC/DC0%0/1
$spec->{"traits"} ||= []
$spec->{"traits"}
Truth table0%0/2
A
A= $spec->{"traits"}
| |||||||||||||||||||||||||||||
| 39 | 0 | push @{$spec->{traits}}, 'SetOnce'; | |||||||||||||||||||||||||||
| 40 | 12 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | $orig->($attr, %opts); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | $target->can('before')->($writer, sub { | ||||||||||||||||||||||||||||
| 45 | 24 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 46 | 24 | ▶ | if (@_ > 1 && $self->$predicate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ > 1 and $self->$predicate
MC/DC100%2/2
@_ > 1 and $self->$predicate
@_ > 1
$self->$predicate
Truth table100%3/3
A and B
A= @_ > 1B= $self->$predicate
| |||||||||||||||||||||||||||||
| 47 | 10 | croak "cannot change value of SetOnce attribute $attr"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 12 | }); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 2 | } | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | 1; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | __END__ | ||||||||||||||||||||||||||||