| 1 | # | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # This file is part of MooX-LvalueAttribute | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2013 by Damien "dams" Krotkine. | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||
| 9 | package Method::Generate::Accessor::Role::LvalueAttribute; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | { | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | $Method::Generate::Accessor::Role::LvalueAttribute::VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use strictures 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # ABSTRACT: Provides Lvalue accessors to Moo class attributes | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Variable::Magic qw(wizard cast); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Hash::Util::FieldHash::Compat; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | Hash::Util::FieldHash::Compat::fieldhash my %LVALUES; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | require MooX::LvalueAttribute; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | around generate_method => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=28.5
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | # would like a better way to disable XS | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 8 | my ($into, $name, $spec, $quote_opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | $MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | ▶ | || $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} or $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
MC/DC0%0/2
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} or $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into}
$MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
Truth table66%2/3
A or B
A= $MooX::LvalueAttribute::INJECTED_IN_ROLE{$into}B= $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | or return $self->$orig(@_); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | ▶ | if ($spec->{lvalue}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | my $is = $spec->{is}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | if ($is eq 'rw') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | $spec->{accessor} = $name unless exists $spec->{accessor} | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | or ( $spec->{reader} and $spec->{writer} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
exists $spec->{"accessor"} or $spec->{"reader"} and $spec->{"writer"}
$spec->{"reader"} and $spec->{"writer"}
MC/DC0%0/3
exists $spec->{"accessor"} or $spec->{"reader"} and $spec->{"writer"}
exists $spec->{"accessor"}
$spec->{"reader"}
$spec->{"writer"}
Truth table25%1/4
A or B and C
A= exists $spec->{"accessor"}B= $spec->{"reader"}C= $spec->{"writer"}
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } elsif ($is eq 'rwp') { | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $spec->{writer} = "_set_${name}" unless exists $spec->{writer}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | exists $spec->{writer} || exists $spec->{accessor} | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 7 | ▶ | or die "lvalue was set but no accessor nor reader, and attribute is not rw"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $spec->{"writer"} or exists $spec->{"accessor"}
MC/DC50%1/2
exists $spec->{"writer"} or exists $spec->{"accessor"}
exists $spec->{"writer"}
exists $spec->{"accessor"}
Truth table66%2/3
A or B
A= exists $spec->{"writer"}B= exists $spec->{"accessor"}
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | foreach( qw(writer accessor) ) { | |||||||||||||||||||||||||||||||||||||||||
| 50 | 12 | ▶ | my $t = $spec->{$_} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | or next; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | $spec->{'lv_' . $_} = $t; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | $spec->{$_} = '_lv_' . $t; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | my $methods = $self->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 7 | foreach ( qw(writer accessor) ) { | |||||||||||||||||||||||||||||||||||||||||
| 60 | 14 | ▶ | my $lv_name = $spec->{'lv_' . $_} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | or next; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | my $name = $spec->{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 27 | ▶ | my $sub = sub : lvalue { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 27 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 27 | ▶ | if (! exists $LVALUES{$self}{$lv_name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | my $wiz = wizard( | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | set => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 7 | $self->$name(${$_[0]}); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 17 | ▶ | get => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 17 | ${$_[0]} = $self->$name(); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 17 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | cast $LVALUES{$self}{$lv_name}, $wiz; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 27 | ▶ | @_ and $self->$name(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 27 | $LVALUES{$self}{$lv_name}; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | $methods->{$lv_name} = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | *{"${into}::${lv_name}"} = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||||||||||||||||