line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::VariantAttribute::Role; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
87692
|
use Moo::Role; |
|
9
|
|
|
|
|
22
|
|
|
9
|
|
|
|
|
56
|
|
4
|
9
|
|
|
9
|
|
3334
|
use Carp qw/croak/; |
|
9
|
|
|
|
|
31
|
|
|
9
|
|
|
|
|
539
|
|
5
|
9
|
|
|
9
|
|
72
|
use Scalar::Util qw/blessed refaddr reftype/; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
602
|
|
6
|
9
|
|
|
9
|
|
4142
|
use Combine::Keys qw/combine_keys/; |
|
9
|
|
|
|
|
115896
|
|
|
9
|
|
|
|
|
84
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has variant_last_value => ( |
9
|
|
|
|
|
|
|
is => 'rw', |
10
|
|
|
|
|
|
|
lazy => 1, |
11
|
|
|
|
|
|
|
default => sub { {} }, |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _given_when { |
15
|
82
|
|
|
82
|
|
10609
|
my ($self) = shift; |
16
|
82
|
|
|
|
|
193
|
my ( $set, $given, $when, $attr, $run ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
82
|
100
|
|
|
|
196
|
return if $self->_variant_last_value($attr, 'set', $set); |
19
|
|
|
|
|
|
|
|
20
|
45
|
|
|
|
|
165
|
my $find = $self->_find_from_given(@_); |
21
|
|
|
|
|
|
|
|
22
|
45
|
|
|
|
|
1203
|
$self->variant_last_value->{$attr}->{find} = $find; |
23
|
45
|
|
|
|
|
335
|
my @when = @{ $when }; |
|
45
|
|
|
|
|
148
|
|
24
|
45
|
|
|
|
|
136
|
while (scalar @when >= 2) { |
25
|
89
|
|
|
|
|
167
|
my $check = shift @when; |
26
|
89
|
|
|
|
|
146
|
my $found = shift @when; |
27
|
89
|
100
|
|
|
|
180
|
if ( _struct_the_same($check, $find) ) { |
28
|
44
|
100
|
|
|
|
123
|
if ( $found->{alias} ) { |
29
|
6
|
100
|
|
|
|
30
|
if (blessed $set) { |
30
|
5
|
|
|
|
|
9
|
for my $alias ( keys %{ $found->{alias} } ) { |
|
5
|
|
|
|
|
20
|
|
31
|
6
|
100
|
|
|
|
53
|
next if $set->can($alias); |
32
|
5
|
|
|
|
|
17
|
my $actual = $found->{alias}->{$alias}; |
33
|
|
|
|
|
|
|
{ |
34
|
9
|
|
|
9
|
|
2206
|
no strict 'refs'; |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
6690
|
|
|
5
|
|
|
|
|
10
|
|
35
|
5
|
|
|
5
|
|
25
|
*{"${find}::${alias}"} = sub { goto &{"${find}::${actual}"} }; |
|
5
|
|
|
|
|
31
|
|
|
5
|
|
|
|
|
3106
|
|
|
5
|
|
|
|
|
44
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} else { |
39
|
1
|
|
|
|
|
3
|
map { $set->{$_} = $set->{$found->{alias}->{$_}} } keys %{ $found->{alias} }; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
5
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
44
|
100
|
|
|
|
121
|
if ( $run = $found->{run} ) { |
44
|
|
|
|
|
|
|
my @new = ref $run eq 'CODE' |
45
|
38
|
100
|
|
|
|
182
|
? $found->{run}->( $self, $find, $set, ) |
46
|
|
|
|
|
|
|
: $self->$run($find, $set); |
47
|
38
|
50
|
|
|
|
296
|
$set = scalar @new > 1 ? \@new : shift @new; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
44
|
|
|
|
|
835
|
$self->variant_last_value->{$attr}->{set} = $set; |
51
|
44
|
|
|
|
|
955
|
return $self->$attr($set); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
164
|
croak sprintf 'Could not find - %s - in when spec for attribute - %s', |
56
|
|
|
|
|
|
|
$set, $attr; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _variant_last_value { |
60
|
87
|
|
|
87
|
|
11908
|
my ($self, $attr, $value, $set) = @_; |
61
|
|
|
|
|
|
|
|
62
|
87
|
100
|
|
|
|
1540
|
my $stored = $self->variant_last_value->{$attr}->{$value} or return undef; |
63
|
65
|
|
|
|
|
592
|
return _ref_the_same($stored, $set); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _ref_the_same { |
67
|
76
|
|
|
76
|
|
10146
|
my ($stored, $passed) = @_; |
68
|
|
|
|
|
|
|
|
69
|
76
|
100
|
100
|
|
|
274
|
if ( ref $passed and ref $stored ) { |
70
|
17
|
100
|
|
|
|
220
|
return refaddr($stored) == refaddr($passed) ? 1 : undef; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
59
|
100
|
|
|
|
1123
|
return ($stored =~ m/^$passed$/) ? 1 : undef; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _struct_the_same { |
77
|
137
|
|
|
137
|
|
10210
|
my ($stored, $passed) = @_; |
78
|
|
|
|
|
|
|
|
79
|
137
|
|
66
|
|
|
557
|
my $stored_ref = reftype($stored) // reftype(\$stored); |
80
|
137
|
|
66
|
|
|
460
|
my $passed_ref = reftype($passed) // reftype(\$passed); |
81
|
137
|
100
|
|
|
|
337
|
$stored_ref eq $passed_ref or return undef; |
82
|
|
|
|
|
|
|
|
83
|
124
|
100
|
|
|
|
292
|
if ( $stored_ref eq 'SCALAR') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
84
|
93
|
100
|
|
|
|
1173
|
return ($stored =~ m/^$passed$/) ? 1 : undef; |
85
|
|
|
|
|
|
|
} elsif ($stored_ref eq 'HASH') { |
86
|
24
|
|
|
|
|
84
|
for (combine_keys($stored, $passed)) { |
87
|
26
|
100
|
100
|
|
|
418
|
$stored->{$_} and $passed->{$_} or return undef; |
88
|
19
|
100
|
|
|
|
60
|
_struct_the_same($stored->{$_}, $passed->{$_}) or return undef; |
89
|
|
|
|
|
|
|
} |
90
|
16
|
|
|
|
|
64
|
return 1; |
91
|
|
|
|
|
|
|
} elsif ($stored_ref eq 'ARRAY') { |
92
|
7
|
|
|
|
|
15
|
my @count = (scalar @{$stored}, scalar @{$passed}); |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
24
|
|
93
|
7
|
100
|
|
|
|
33
|
$count[0] == $count[1] or return undef; |
94
|
5
|
|
|
|
|
20
|
for ( 0 .. $count[1] - 1 ) { |
95
|
12
|
100
|
|
|
|
33
|
_struct_the_same($stored->[$_], $passed->[$_]) or return undef; |
96
|
|
|
|
|
|
|
} |
97
|
4
|
|
|
|
|
17
|
return 1; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
return 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _find_from_given { |
104
|
52
|
|
|
52
|
|
10094
|
my ( $self, $set, $given, $when ) = @_; |
105
|
|
|
|
|
|
|
|
106
|
52
|
|
|
|
|
132
|
my $ref_given = ref $given; |
107
|
52
|
100
|
|
|
|
155
|
if ( $ref_given eq 'Type::Tiny' ) { |
|
|
50
|
|
|
|
|
|
108
|
42
|
|
|
|
|
394
|
$set = $given->($set); |
109
|
42
|
100
|
|
|
|
9476
|
return $given->display_name eq 'Object' ? ref $set : $set; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
elsif ( $ref_given eq 'CODE' ) { |
112
|
10
|
|
|
|
|
37
|
return $given->( $self, $set ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
return $set; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |