| 1 | package MooseX::Traits::SetScalarByRef; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Moose::Util::TypeConstraints qw(find_type_constraint); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # Supply a default for "is" | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | around _process_is_option => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my $next = shift; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my ($name, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | if (not exists $options->{is}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | $options->{is} = "rw"; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | $self->$next(@_); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Supply a default for "isa" | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | my $default_type; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | around _process_isa_option => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=16.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my $next = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | my ($name, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | if (not exists $options->{isa}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (not defined $default_type) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | $default_type = | |||||||||||||||||||||||||||||||||||||||||
| 34 | find_type_constraint('ScalarRef')->create_child_constraint; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | $default_type->coercion('Moose::Meta::TypeCoercion'->new) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | ->add_type_coercions('Value', sub { my $r = $_; \$r }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $options->{isa} = $default_type; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | $self->$next(@_); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # Automatically coerce | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | around _process_coerce_option => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.5
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | my $next = shift; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my ($name, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | if ( defined $options->{type_constraint} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $options->{"type_constraint"} and $options->{"type_constraint"}->has_coercion and not exists $options->{"coerce"}
defined $options->{"type_constraint"} and $options->{"type_constraint"}->has_coercion
MC/DC0%0/3
defined $options->{"type_constraint"} and $options->{"type_constraint"}->has_coercion and not exists $options->{"coerce"}
defined $options->{"type_constraint"}
$options->{"type_constraint"}->has_coercion
not exists $options->{"coerce"}
Truth table25%1/4
A and B and not(C)
A= defined $options->{"type_constraint"}B= $options->{"type_constraint"}->has_coercionC= not exists $options->{"coerce"}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | and $options->{type_constraint}->has_coercion | ||||||||||||||||||||||||||||||||||||||||||
| 52 | and not exists $options->{coerce}) { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $options->{coerce} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $self->$next(@_); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # This allows handles => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | around _canonicalize_handles => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | my $next = shift; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my $handles = $self->handles; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if (!ref($handles) and $handles eq '1') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $handles and $handles eq 1
MC/DC50%1/2
not ref $handles and $handles eq 1
not ref $handles
$handles eq 1
Truth table66%2/3
not(A) and B
A= not ref $handlesB= $handles eq 1
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | return ($self->init_arg, 'set_by_ref'); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | $self->$next(@_); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | # Actually install the wrapper | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | around install_delegation => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | my $next = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my %handles = $self->_canonicalize_handles; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | for my $key (sort keys %handles) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | $handles{$key} eq 'set_by_ref' or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | delete $handles{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $self->associated_class->add_method($key, | |||||||||||||||||||||||||||||||||||||||||
| 82 | $self->_make_set_by_ref($key)); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | # When we call $next, we're going to temporarily | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # replace $self->handles, so that $next cannot see | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # the set_by_ref bits which were there. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | my $orig = $self->handles; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $self->_set_handles(\%handles); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | $self->$next(@_); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | $self->_set_handles($orig); # and restore! | |||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # This generates the coderef for the method that we're | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # going to install | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub _make_set_by_ref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_set_by_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | my ($method_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | my $reader = $self->get_read_method; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my $type = $self->type_constraint; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $coerce = $self->should_coerce; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | my $obj = shift; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | my $new_ref = | ||||||||||||||||||||||||||||||||||||||||||
| 108 | $coerce | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ? $type->assert_coerce(@_) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | : do { $type->assert_valid(@_); $_[0] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | ${ $obj->$reader } = $$new_ref; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | $obj->$reader; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | 1; # /MooseX::Traits::SetScalarByRef | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||||||||||||