line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::Attributes::Shadow; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: shadow attributes of contained objects |
4
|
|
|
|
|
|
|
|
5
|
10
|
|
|
10
|
|
2001791
|
use strict; |
|
10
|
|
|
|
|
82
|
|
|
10
|
|
|
|
|
290
|
|
6
|
10
|
|
|
10
|
|
54
|
use warnings; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
357
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.04'; # TRIAL |
9
|
|
|
|
|
|
|
|
10
|
10
|
|
|
10
|
|
55
|
use Carp (); |
|
10
|
|
|
|
|
34
|
|
|
10
|
|
|
|
|
167
|
|
11
|
10
|
|
|
10
|
|
4926
|
use Params::Check; |
|
10
|
|
|
|
|
39993
|
|
|
10
|
|
|
|
|
924
|
|
12
|
10
|
|
|
10
|
|
79
|
use Scalar::Util; |
|
10
|
|
|
|
|
41
|
|
|
10
|
|
|
|
|
365
|
|
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
64
|
use Exporter 'import'; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
3872
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [ qw( shadow_attrs shadowed_attrs xtract_attrs ) ], |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
Exporter::export_ok_tags('all'); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %MAP; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
## no critic (ProhibitAccessOfPrivateData) |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub shadow_attrs { |
25
|
|
|
|
|
|
|
|
26
|
16
|
|
|
16
|
1
|
1132
|
my $contained = shift; |
27
|
|
|
|
|
|
|
|
28
|
16
|
|
|
|
|
53
|
my $container = caller; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $args = Params::Check::check( { |
31
|
|
|
|
|
|
|
fmt => { |
32
|
11
|
|
|
11
|
|
1027
|
allow => sub { ref $_[0] eq 'CODE' } |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
|
35
|
7
|
100
|
66
|
7
|
|
766
|
attrs => { allow => sub { 'ARRAY' eq ref $_[0] && @{ $_[0] } |
|
5
|
|
|
|
|
31
|
|
36
|
|
|
|
|
|
|
or 'HASH' eq ref $_[0] }, |
37
|
|
|
|
|
|
|
}, |
38
|
16
|
100
|
|
|
|
236
|
private => { default => 1 }, |
39
|
|
|
|
|
|
|
instance => {}, |
40
|
|
|
|
|
|
|
}, |
41
|
|
|
|
|
|
|
{@_} ) or Carp::croak( "error parsing arguments: ", Params::Check::last_error, "\n" ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
15
|
100
|
|
|
|
595
|
unless ( exists $args->{attrs} ) { |
45
|
|
|
|
|
|
|
|
46
|
9
|
|
|
|
|
23
|
$args->{attrs} = [ eval { $contained->shadowable_attrs } ]; |
|
9
|
|
|
|
|
48
|
|
47
|
|
|
|
|
|
|
|
48
|
9
|
100
|
|
|
|
145
|
Carp::croak( "must specify attrs or call shadowable_attrs in shadowed class" ) |
49
|
|
|
|
|
|
|
if $@; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
14
|
100
|
|
|
|
355
|
my $has = $container->can( 'has' ) |
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
|
|
|
|
|
65
|
'ARRAY' eq ref $args->{attrs} ? ( map { $_ => undef } @{$args->{attrs}} ) |
|
12
|
|
|
|
|
37
|
|
59
|
13
|
100
|
|
|
|
107
|
: %{$args->{attrs}}; |
|
1
|
|
|
|
|
5
|
|
60
|
|
|
|
|
|
|
|
61
|
13
|
|
|
|
|
38
|
my %map; |
62
|
13
|
|
|
|
|
77
|
while( my ( $attr, $alias ) = each %attr ) { |
63
|
|
|
|
|
|
|
|
64
|
22
|
100
|
|
|
|
5348
|
$alias = $args->{fmt} ? $args->{fmt}->( $attr ) : $attr |
|
|
100
|
|
|
|
|
|
65
|
|
|
|
|
|
|
unless defined $alias; |
66
|
|
|
|
|
|
|
|
67
|
22
|
100
|
|
|
|
131
|
my $priv = $args->{private} ? "_shadow_${contained}_${alias}" : $alias; |
68
|
22
|
|
|
|
|
76
|
$priv =~ s/::/_/g; |
69
|
22
|
|
|
|
|
76
|
$map{$attr} = { priv => $priv, alias => $alias }; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
## no critic (ProhibitNoStrict) |
72
|
10
|
|
|
10
|
|
88
|
no strict 'refs'; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
5462
|
|
73
|
22
|
|
|
|
|
102
|
$has->( |
74
|
|
|
|
|
|
|
$priv => ( |
75
|
|
|
|
|
|
|
is => 'ro', |
76
|
|
|
|
|
|
|
init_arg => $alias, |
77
|
|
|
|
|
|
|
predicate => "_has_${priv}", |
78
|
|
|
|
|
|
|
) ); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
13
|
100
|
|
|
|
5634
|
if ( defined $args->{instance} ) { |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
24
|
$MAP{$contained}{$container}{instance}{ $args->{instance} } = \%map; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
|
90
|
7
|
|
|
|
|
33
|
$MAP{$contained}{$container}{default} = \%map; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
13
|
|
|
|
|
58
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _resolve_attr_env { |
98
|
|
|
|
|
|
|
|
99
|
28
|
|
|
28
|
|
77
|
my ( $contained, $container, $options ) = @_; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# contained should be resolved into a class name |
102
|
28
|
|
66
|
|
|
141
|
my $containedClass = Scalar::Util::blessed $contained || $contained; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# allow $container to be either a class or an object |
105
|
28
|
|
66
|
|
|
114
|
my $containerClass = Scalar::Util::blessed $container || $container; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $map = defined $options->{instance} |
108
|
|
|
|
|
|
|
? $MAP{$containedClass}{$containerClass}{instance}{$options->{instance}} |
109
|
28
|
100
|
|
|
|
112
|
: $MAP{$containedClass}{$containerClass}{default}; |
110
|
|
|
|
|
|
|
|
111
|
28
|
100
|
|
|
|
206
|
Carp::croak( "attributes must first be shadowed using ${containedClass}::shadow_attrs\n" ) |
112
|
|
|
|
|
|
|
unless defined $map; |
113
|
|
|
|
|
|
|
|
114
|
27
|
|
|
|
|
58
|
return $map; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# call as |
118
|
|
|
|
|
|
|
# shadowed_attrs( $ContainedClass, [ $container ], \%options) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub shadowed_attrs { |
121
|
|
|
|
|
|
|
|
122
|
8
|
|
|
8
|
1
|
2522
|
my $containedClass = shift; |
123
|
8
|
100
|
|
|
|
28
|
my $options = 'HASH' eq ref $_[-1] ? pop() : {}; |
124
|
|
|
|
|
|
|
|
125
|
8
|
100
|
|
|
|
20
|
my $containerClass = @_ ? shift : caller(); |
126
|
|
|
|
|
|
|
|
127
|
8
|
|
|
|
|
49
|
my $map = _resolve_attr_env( $containedClass, $containerClass, $options ); |
128
|
|
|
|
|
|
|
|
129
|
8
|
|
|
|
|
24
|
return { map { $map->{$_}{alias}, $_ } keys %$map } |
|
16
|
|
|
|
|
92
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# call as |
133
|
|
|
|
|
|
|
# xtract_attrs( $ContainedClass, $container_obj, \%options) |
134
|
|
|
|
|
|
|
sub xtract_attrs { |
135
|
|
|
|
|
|
|
|
136
|
21
|
|
|
21
|
1
|
45362
|
my $containedClass = shift; |
137
|
21
|
100
|
|
|
|
91
|
my $options = 'HASH' eq ref $_[-1] ? pop() : {}; |
138
|
21
|
|
|
|
|
39
|
my $container = shift; |
139
|
21
|
100
|
|
|
|
308
|
my $containerClass = Scalar::Util::blessed $container or |
140
|
|
|
|
|
|
|
Carp::croak( "container_obj parameter is not a container object\n" ); |
141
|
|
|
|
|
|
|
|
142
|
20
|
|
|
|
|
73
|
my $map = _resolve_attr_env( $containedClass, $containerClass, $options ); |
143
|
|
|
|
|
|
|
|
144
|
19
|
|
|
|
|
35
|
my %attr; |
145
|
19
|
|
|
|
|
89
|
while( my ($attr, $names) = each %$map ) { |
146
|
|
|
|
|
|
|
|
147
|
34
|
|
|
|
|
63
|
my $priv = $names->{priv}; |
148
|
34
|
|
|
|
|
77
|
my $has = "_has_${priv}"; |
149
|
|
|
|
|
|
|
|
150
|
34
|
100
|
|
|
|
244
|
$attr{$attr} = $container->$priv |
151
|
|
|
|
|
|
|
if $container->$has; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
19
|
|
|
|
|
266
|
return %attr; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
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__ |