| 1 | package MooX::Attributes::Shadow; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: shadow attributes of contained objects | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 12 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Params::Check; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | our %EXPORT_TAGS = ( all => [ qw( shadow_attrs shadowed_attrs xtract_attrs ) ], | |||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | 12 | Exporter::export_ok_tags('all'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 12 | my %MAP; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ## no critic (ProhibitAccessOfPrivateData) | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 16 | ▶ | sub shadow_attrs { | ||||||||||||||||||||||||||
|
Subroutine
shadow_attrs: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 16 | my $contained = shift; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | my $container = caller; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $args = Params::Check::check( { | ||||||||||||||||||||||||||||
| 31 | fmt => { | ||||||||||||||||||||||||||||
| 32 | 11 | ▶ | allow => sub { ref $_[0] eq 'CODE' } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | attrs => { allow => sub { 'ARRAY' eq ref $_[0] && @{ $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
"ARRAY" eq ref $_[0] and @{$_[0];}
MC/DC50%1/2
"ARRAY" eq ref $_[0] and @{$_[0];}
"ARRAY" eq ref $_[0]
@{$_[0];}
Truth table66%2/3
A and B
A= "ARRAY" eq ref $_[0]B= @{$_[0];}
| |||||||||||||||||||||||||||||
| 36 | or 'HASH' eq ref $_[0] }, | ||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | 16 | ▶ | private => { default => 1 }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | instance => {}, | ||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | {@_} ) or Carp::croak( "error parsing arguments: ", Params::Check::last_error, "\n" ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 15 | ▶ | unless ( exists $args->{attrs} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 9 | $args->{attrs} = [ eval { $contained->shadowable_attrs } ]; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | Carp::croak( "must specify attrs or call shadowable_attrs in shadowed class" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | if $@; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 14 | ▶ | my $has = $container->can( 'has' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or Carp::croak( "container class $container does not have a 'has' function.", | ||||||||||||||||||||||||||||
| 55 | " Is it really a Moo class?" ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | my %attr = | ||||||||||||||||||||||||||||
| 58 | 20 | 'ARRAY' eq ref $args->{attrs} ? ( map { $_ => undef } @{$args->{attrs}} ) | |||||||||||||||||||||||||||
| 59 | 13 | ▶ | : %{$args->{attrs}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 13 | my %map; | |||||||||||||||||||||||||||
| 62 | 13 | ▶ | while( my ( $attr, $alias ) = each %attr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 22 | ▶ | $alias = $args->{fmt} ? $args->{fmt}->( $attr ) : $attr | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 65 | unless defined $alias; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 22 | ▶ | my $priv = $args->{private} ? "_shadow_${contained}_${alias}" : $alias; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 22 | $priv =~ s/::/_/g; | |||||||||||||||||||||||||||
| 69 | 22 | $map{$attr} = { priv => $priv, alias => $alias }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||
| 72 | 12 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 22 | $has->( | |||||||||||||||||||||||||||
| 74 | $priv => ( | ||||||||||||||||||||||||||||
| 75 | is => 'ro', | ||||||||||||||||||||||||||||
| 76 | init_arg => $alias, | ||||||||||||||||||||||||||||
| 77 | predicate => "_has_${priv}", | ||||||||||||||||||||||||||||
| 78 | ) ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 13 | ▶ | if ( defined $args->{instance} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | $MAP{$contained}{$container}{instance}{ $args->{instance} } = \%map; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | $MAP{$contained}{$container}{default} = \%map; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 13 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 28 | ▶ | sub _resolve_attr_env { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_attr_env: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 28 | my ( $contained, $container, $options ) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # contained should be resolved into a class name | ||||||||||||||||||||||||||||
| 102 | 28 | ▶ | my $containedClass = Scalar::Util::blessed $contained || $contained; | ||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($contained) || $contained
MC/DC0%0/2
builtin::blessed($contained) || $contained
builtin::blessed($contained)
$contained
Truth table66%2/3
A || B
A= builtin::blessed($contained)B= $contained
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # allow $container to be either a class or an object | ||||||||||||||||||||||||||||
| 105 | 28 | ▶ | my $containerClass = Scalar::Util::blessed $container || $container; | ||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($container) || $container
MC/DC0%0/2
builtin::blessed($container) || $container
builtin::blessed($container)
$container
Truth table66%2/3
A || B
A= builtin::blessed($container)B= $container
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | my $map = defined $options->{instance} | ||||||||||||||||||||||||||||
| 108 | ? $MAP{$containedClass}{$containerClass}{instance}{$options->{instance}} | ||||||||||||||||||||||||||||
| 109 | 28 | ▶ | : $MAP{$containedClass}{$containerClass}{default}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 28 | ▶ | Carp::croak( "attributes must first be shadowed using ${containedClass}::shadow_attrs\n" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | unless defined $map; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 27 | return $map; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # call as | ||||||||||||||||||||||||||||
| 118 | # shadowed_attrs( $ContainedClass, [ $container ], \%options) | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 8 | ▶ | sub shadowed_attrs { | ||||||||||||||||||||||||||
|
Subroutine
shadowed_attrs: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | my $containedClass = shift; | |||||||||||||||||||||||||||
| 123 | 8 | ▶ | my $options = 'HASH' eq ref $_[-1] ? pop() : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 8 | ▶ | my $containerClass = @_ ? shift : caller(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 8 | my $map = _resolve_attr_env( $containedClass, $containerClass, $options ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 8 | return { map { $map->{$_}{alias}, $_ } keys %$map } | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # call as | ||||||||||||||||||||||||||||
| 133 | # xtract_attrs( $ContainedClass, $container_obj, \%options) | ||||||||||||||||||||||||||||
| 134 | 21 | ▶ | sub xtract_attrs { | ||||||||||||||||||||||||||
|
Subroutine
xtract_attrs: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 21 | my $containedClass = shift; | |||||||||||||||||||||||||||
| 137 | 21 | ▶ | my $options = 'HASH' eq ref $_[-1] ? pop() : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 21 | my $container = shift; | |||||||||||||||||||||||||||
| 139 | 21 | ▶ | my $containerClass = Scalar::Util::blessed $container or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | Carp::croak( "container_obj parameter is not a container object\n" ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 20 | my $map = _resolve_attr_env( $containedClass, $containerClass, $options ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 19 | my %attr; | |||||||||||||||||||||||||||
| 145 | 19 | ▶ | while( my ($attr, $names) = each %$map ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 34 | my $priv = $names->{priv}; | |||||||||||||||||||||||||||
| 148 | 34 | my $has = "_has_${priv}"; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 34 | ▶ | $attr{$attr} = $container->$priv | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | if $container->$has; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 19 | return %attr; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 12 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||
| 160 | # This file is part of MooX-Attributes-Shadow | ||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||
| 162 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 163 | # | ||||||||||||||||||||||||||||
| 164 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 165 | # | ||||||||||||||||||||||||||||
| 166 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||