GX/Meta/Attribute/Object.pm

filter: Statementstmt 91.7% 78 / 85 Branchbran 60.0% 12 / 20 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 93.7% 15 / 16 Podpod 83.3% 5 / 6 totaltotal 82.8% 116 / 140 CC 16 SCAR 12.8
CC
16
Cov
84%
__initialize_delegators
7.1
install_delegators
5.5
create_delegator
4.1
CRAP 3.6
« GX/Meta/Method.pm ↑ Summary GX/Logger.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Meta/Attribute/Object.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Meta::Attribute::Object;
7  
8 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 ▶use base 'GX::Meta::Attribute::Scalar';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 37 ▶use GX::Meta::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 37 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 # ----------------------------------------------------------------------------------------------------------------------
19 # Constants
20 # ----------------------------------------------------------------------------------------------------------------------
21  
22 37 ▶use constant TYPE_CONSTRAINT => sub { blessed $_[1] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24  
25 # ----------------------------------------------------------------------------------------------------------------------
26 # Public methods
27 # ----------------------------------------------------------------------------------------------------------------------
28  
29 16 ▶sub add_delegator {
Subroutine add_delegator: called CC=2 SCAR=7.1
Pod documented
30  
31 16 my $self = shift;
32  
33 16 my $delegator = eval {
34 16 $self->create_delegator( @_ );
35 };
36  
37 16 ▶ if ( ! $delegator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delegator) 0 16 16
38 0 GX::Exception->complain(
39 message => "Cannot create delegator",
40 subexception => $@
41 );
42 }
43  
44 16 push @{$self->{'delegators'}}, $delegator;
45  
46 16 return $delegator;
47  
48 }
49  
50 113 ▶sub delegators {
Subroutine delegators: called CC=2 SCAR=6.9
Pod documented
51  
52 113 ▶ return @{ $_[0]->{'delegators'} || return };
Condition100%2/2
$_[0]{'delegators'} || (return)
l!l
2489
MC/DC100%1/1
$_[0]{'delegators'} || (return)
$_[0]{'delegators'}
Truth table100%2/2
A
A= $_[0]{'delegators'}
Aresult
11
00
53  
54 }
55  
56 99 ▶sub install {
Subroutine install: called CC=2 SCAR=7.1
Pod documented
57  
58 99 my $self = shift;
59  
60 99 eval {
61 99 $self->SUPER::install;
62 99 $self->install_delegators;
63 };
64  
65 99 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 99 99
66 0 complain $@;
67 }
68  
69 99 return;
70  
71 }
72  
73 101 ▶sub install_delegators {
Subroutine install_delegators: called CC=4 SCAR=17.1
Pod documented
74  
75 101 my $self = shift;
76  
77 101 for my $delegator ( $self->delegators ) {
78  
79 16 ▶ if ( my $method = $self->class->method( $delegator->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->class->method($delegator->name)) { } 0 16 16
80 0 ▶ next if $method->code == $delegator->code;
Branch: 1 branch
BranchTrueFalseTotal
if $method->code == $delegator->code 0 0 0
81 0 complain sprintf( "Cannot install delegator (method &%s already exists)", $method->full_name );
82 }
83 else {
84 16 $self->class->add_method( $delegator->name, $delegator->code );
85 }
86  
87 }
88  
89 101 return;
90  
91 }
92  
93 1 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
94  
95 1 return 'Object';
96  
97 }
98  
99  
100 # ----------------------------------------------------------------------------------------------------------------------
101 # Internal methods
102 # ----------------------------------------------------------------------------------------------------------------------
103  
104 16 ▶sub create_delegator {
Subroutine create_delegator: called CC=4 SCAR=14.2
Pod undocumented
105  
106 16 my $self = shift;
107 16 my %args = @_;
108  
109 16 ▶ my $name = $args{'name'} // $self->name;
Condition33%1/3
$args{"name"} // $self->name
l!l&&r!l&&!r
1600
MC/DC0%0/2
$args{"name"} // $self->name
$args{"name"} $self->name
Truth table33%1/3
A // B
A= $args{"name"}B= $self->name
ABresult
1X1
011
000
110 16 ▶ my $to = $args{'to'} // $name;
Condition66%2/3
$args{"to"} // $name
l!l&&r!l&&!r
880
MC/DC0%0/2
$args{"to"} // $name
$args{"to"} $name
Truth table66%2/3
A // B
A= $args{"to"}B= $name
ABresult
1X1
011
000
111  
112 16 my $data = {};
113 16 my $code = [];
114 16 my $vars = {};
115  
116 16 push @$code,
117 'package ' . ref( $self ) . ';',
118 'sub {',
119 'my $invocant = shift;';
120  
121 16 $data->{'invocant'} = '$invocant';
122  
123 16 $self->_inline_accessor_prologue( $data, $code, $vars );
124  
125 16 $self->_inline_prepare_slot( $data, $code, $vars );
126  
127 push @$code,
128 16 'return ' . $data->{'slot'} . '->' . $to . '( @_ );',
129 '}';
130  
131 16 my $delegator = eval {
132 16 GX::Meta::Delegator->new(
133 attribute => $self,
134 name => $name,
135 code => GX::Meta::Util::eval_code( $code, $vars )
136 )
137 };
138  
139 16 ▶ if ( ! $delegator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delegator) 0 16 16
140 0 complain $@;
141 }
142  
143 16 return $delegator;
144  
145 }
146  
147  
148 # ----------------------------------------------------------------------------------------------------------------------
149 # Private methods
150 # ----------------------------------------------------------------------------------------------------------------------
151  
152 0 ▶sub _get_delegators {
Subroutine _get_delegators: not called CC=1 SCAR=6.9
153  
154 0 return $_[0]->{'delegators'};
155  
156 }
157  
158  
159 # ----------------------------------------------------------------------------------------------------------------------
160 # Object initialization
161 # ----------------------------------------------------------------------------------------------------------------------
162  
163 122 ▶sub __initialize_delegators {
Subroutine __initialize_delegators: called CC=7 SCAR=19.5
164  
165 122 my $self = shift;
166 122 my $args = shift;
167  
168 122 my @delegators;
169  
170 122 ▶ if ( exists $args->{'delegator'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"delegator"}) 4 118 122
171  
172 4 ▶ if ( defined $args->{'delegator'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"delegator"}) 4 0 4
173 4 push @delegators, $args->{'delegator'};
174 }
175  
176 4 delete $args->{'delegator'};
177  
178 }
179  
180 122 ▶ if ( exists $args->{'delegators'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"delegators"}) 6 116 122
181  
182 6 ▶ if ( ref $args->{'delegators'} ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"delegators"} ne "ARRAY") 0 6 6
183 0 throw "Invalid option (\"delegators\" must be an array reference)";
184 }
185  
186 6 push @delegators, @{$args->{'delegators'}};
187  
188 6 delete $args->{'delegators'};
189  
190 }
191  
192 122 for my $delegator ( @delegators ) {
193  
194 12 ▶ if ( ref $delegator eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delegator eq 'HASH') { } 4 8 12
195 4 $self->add_delegator( %$delegator );
196 }
197 else {
198 8 $self->add_delegator( name => $delegator );
199 }
200  
201 }
202  
203 122 return;
204  
205 }
206  
207 122 ▶sub __initialize_other {
Subroutine __initialize_other: called CC=1 SCAR=0.0
208  
209 122 my $self = shift;
210 122 my $args = shift;
211  
212 122 $self->__initialize_delegators( $args );
213  
214 122 return;
215  
216 }
217  
218  
219 37 1;
220  
221 __END__
« GX/Meta/Method.pm ↑ Summary GX/Logger.pm »