| 1 | package UR::AttributeHandlers; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 266 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 266 | ▶ | use attributes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 266 | our @CARP_NOT = qw(UR::Namespace); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # implement's UR's mechanism for sub/variable attributes. | ||||||||||||||||||||||||||||
| 12 | 266 | my %support_functions = ( | |||||||||||||||||||||||||||
| 13 | MODIFY_CODE_ATTRIBUTES => \&modify_attributes, | ||||||||||||||||||||||||||||
| 14 | FETCH_CODE_ATTRIBUTES => \&fetch_attributes, | ||||||||||||||||||||||||||||
| 15 | MODIFY_SCALAR_ATTRIBUTES => \&modify_attributes, | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub import_support_functions_to_package { | ||||||||||||||||||||||||||
|
Subroutine
import_support_functions_to_package: not called CC=2 SCAR=12.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 0 | my $package = shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | while( my($name, $code) = each %support_functions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | my $target = join('::', $package, $name); | |||||||||||||||||||||||||||
| 23 | 0 | do { | |||||||||||||||||||||||||||
| 24 | 266 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 0 | *$target = $code; | |||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 266 | my %modify_attribute_handlers = ( | |||||||||||||||||||||||||||
| 32 | CODE => { Overrides => \&modify_code_overrides }, | ||||||||||||||||||||||||||||
| 33 | SCALAR => { RoleParam => \&modify_scalar_role_property }, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | 266 | my %fetch_attribute_handlers = ( | |||||||||||||||||||||||||||
| 36 | CODE => { Overrides => \&fetch_code_overrides }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _modify_attribute_handler { | ||||||||||||||||||||||||||
|
Subroutine
_modify_attribute_handler: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my($ref, $attr) = @_; | |||||||||||||||||||||||||||
| 41 | 0 | my $reftype = attributes::reftype($ref); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | return (exists($modify_attribute_handlers{$reftype}) and $modify_attribute_handlers{$reftype}->{$attr}); | ||||||||||||||||||||||||||
|
Condition0%0/3
exists $modify_attribute_handlers{$reftype} && $modify_attribute_handlers{$reftype}{$attr}
MC/DC0%0/2
exists $modify_attribute_handlers{$reftype} && $modify_attribute_handlers{$reftype}{$attr}
exists $modify_attribute_handlers{$reftype}
$modify_attribute_handlers{$reftype}{$attr}
Truth table0%0/3
A && B
A= exists $modify_attribute_handlers{$reftype}B= $modify_attribute_handlers{$reftype}{$attr}
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _fetch_attribute_handler { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_attribute_handler: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my($ref, $attr) = @_; | |||||||||||||||||||||||||||
| 47 | 0 | my $reftype = attributes::reftype($ref); | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return (exists($fetch_attribute_handlers{$reftype}) and $fetch_attribute_handlers{$reftype}->{$attr}); | ||||||||||||||||||||||||||
|
Condition0%0/3
exists $fetch_attribute_handlers{$reftype} && $fetch_attribute_handlers{$reftype}{$attr}
MC/DC0%0/2
exists $fetch_attribute_handlers{$reftype} && $fetch_attribute_handlers{$reftype}{$attr}
exists $fetch_attribute_handlers{$reftype}
$fetch_attribute_handlers{$reftype}{$attr}
Truth table0%0/3
A && B
A= exists $fetch_attribute_handlers{$reftype}B= $fetch_attribute_handlers{$reftype}{$attr}
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _decompose_attr { | ||||||||||||||||||||||||||
|
Subroutine
_decompose_attr: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 52 | 0 | my($raw_attr) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | my($attr, $params_str) = $raw_attr =~ m/^(\w+)(?:\((.*)\))$/; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my @params = defined($params_str) ? split(/\s*,\s*/, $params_str) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $attr = $raw_attr unless defined $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return ($attr, @params); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub modify_attributes { | ||||||||||||||||||||||||||
|
Subroutine
modify_attributes: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my($package, $ref, @raw_attrs) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my @not_recognized; | |||||||||||||||||||||||||||
| 64 | 0 | foreach my $raw_attr ( @raw_attrs ) { | |||||||||||||||||||||||||||
| 65 | 0 | my($attr, @params) = _decompose_attr($raw_attr); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (my $handler = _modify_attribute_handler($ref, $attr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $handler->($package, $ref, $attr, @params); | |||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 0 | push @not_recognized, $raw_attr; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return @not_recognized; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 266 | my %stored_attributes_by_ref; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub fetch_attributes { | ||||||||||||||||||||||||||
|
Subroutine
fetch_attributes: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | my($package, $ref) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $reftype = attributes::reftype($ref); | |||||||||||||||||||||||||||
| 82 | 0 | my @attrs; | |||||||||||||||||||||||||||
| 83 | 0 | foreach my $attr ( keys %{ $stored_attributes_by_ref{$ref} } ) { | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (my $handler = _fetch_attribute_handler($ref, $attr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | push @attrs, $handler->($package, $ref); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 0 | return @attrs; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub modify_code_overrides { | ||||||||||||||||||||||||||
|
Subroutine
modify_code_overrides: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | my($package, $coderef, $attr, @params) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $list = $stored_attributes_by_ref{$coderef}->{overrides} ||= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$stored_attributes_by_ref{$coderef}{"overrides"} ||= []
MC/DC0%0/1
$stored_attributes_by_ref{$coderef}{"overrides"} ||= []
$stored_attributes_by_ref{$coderef}{"overrides"}
Truth table0%0/2
A
A= $stored_attributes_by_ref{$coderef}{"overrides"}
| |||||||||||||||||||||||||||||
| 95 | 0 | push @$list, @params; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub modify_scalar_role_property { | ||||||||||||||||||||||||||
|
Subroutine
modify_scalar_role_property: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 0 | my($package, $scalar_ref, $attr, $name) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unless ($name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | Carp::croak('RoleParam attribute requires a name in parens. For example: my $var : RoleParam(var)'); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | $$scalar_ref = UR::Role::Param->new(name => $name, role_name => $package, varref => $scalar_ref); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub fetch_code_overrides { | ||||||||||||||||||||||||||
|
Subroutine
fetch_code_overrides: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | my($package, $coderef) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | return sprintf('overrides(%s)', | ||||||||||||||||||||||||||||
| 111 | 0 | join(', ', @{ $stored_attributes_by_ref{$coderef}->{overrides} })); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub get_overrides_for_coderef { | ||||||||||||||||||||||||||
|
Subroutine
get_overrides_for_coderef: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 0 | my($ref) = @_; | |||||||||||||||||||||||||||
| 116 | return( exists($stored_attributes_by_ref{$ref}) && exists($stored_attributes_by_ref{$ref}->{overrides}) | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | ? @{ $stored_attributes_by_ref{$ref}->{overrides} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $stored_attributes_by_ref{$ref} and exists $stored_attributes_by_ref{$ref}{"overrides"}
MC/DC0%0/2
exists $stored_attributes_by_ref{$ref} and exists $stored_attributes_by_ref{$ref}{"overrides"}
exists $stored_attributes_by_ref{$ref}
exists $stored_attributes_by_ref{$ref}{"overrides"}
Truth table0%0/3
A and B
A= exists $stored_attributes_by_ref{$ref}B= exists $stored_attributes_by_ref{$ref}{"overrides"}
| |||||||||||||||||||||||||||||
| 118 | : () | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 266 | 1; | |||||||||||||||||||||||||||