blib/lib/Object/Declare.pm

filter: Statementstmt 95.9% 142 / 148 Branchbran 70.3% 45 / 64 Conditioncond 45.0% 9 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 30 / 30 Podpod n/a totaltotal 82.8% 227 / 274 CC 46 SCAR 21.8
CC
46
Cov
84%
import
24.4
__ANON__
10.7
unroll
8.0
CRAP 8.9
↑ Summary
1 package Object::Declare;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.24';
8  
9 sub import
10 1 ▶{
Subroutine import: called CC=17 SCAR=31.9
11 1 my $class = shift;
12 1 ▶ my %args = ( ( @_ and ref( $_[0] ) ) ? ( mapping => $_[0] ) : @_ )
Branch: 2 branches
BranchTrueFalseTotal
my(%args) = @_ && ref $_[0] ? ("mapping", $_[0]) : @_ or return 1 0 1
@_ && ref $_[0] ? : 0 1 1
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
010
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
13 or return;
14 1 my $from = caller;
15  
16 1 ▶ my $mapping = $args{mapping} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mapping = $args{"mapping"} or return 1 0 1
17 1 ▶ my $aliases = $args{aliases} || {};
Condition50%1/2
$args{'aliases'} || {}
l!l
10
MC/DC0%0/1
$args{'aliases'} || {}
$args{'aliases'}
Truth table50%1/2
A
A= $args{'aliases'}
Aresult
11
00
18 1 ▶ my $declarator = $args{declarator} || ['declare'];
Condition50%1/2
$args{'declarator'} || ['declare']
l!l
01
MC/DC0%0/1
$args{'declarator'} || ['declare']
$args{'declarator'}
Truth table50%1/2
A
A= $args{'declarator'}
Aresult
11
00
19 1 ▶ my $copula = $args{copula} || [ 'is', 'are' ];
Condition50%1/2
$args{'copula'} || ['is', 'are']
l!l
10
MC/DC0%0/1
$args{'copula'} || ['is', 'are']
$args{'copula'}
Truth table50%1/2
A
A= $args{'copula'}
Aresult
11
00
20  
21 # Both declarator and copula can contain more than one entries;
22 # normalize into an arrayref if we only have on entry.
23 1 ▶ $mapping = [$mapping] unless ref($mapping);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mapping 0 1 1
24 1 ▶ $declarator = [$declarator] unless ref($declarator);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $declarator 0 1 1
25 1 ▶ $copula = [$copula] unless ref($copula);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $copula 0 1 1
26  
27 1 ▶ if ( ref($mapping) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mapping eq "ARRAY") 0 1 1
28 {
29 # rewrite "MyApp::Foo" into simply "foo"
30 $mapping = {
31 map {
32 0 my $helper = $_;
33 0 $helper =~ s/.*:://;
34 0 ( lc($helper) => $_ );
35 } @$mapping
36 };
37 }
38  
39 # Convert mapping targets into instantiation closures
40 1 ▶ if ( ref($mapping) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mapping eq "HASH") 1 0 1
41 {
42 1 foreach my $key ( keys %$mapping )
43 {
44 2 my $val = $mapping->{$key};
45 2 ▶ next if ref($val); # already a callback, don't bother
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 1 1 2
46 1 ▶ $mapping->{$key} = sub { scalar( $val->new(@_) ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48 }
49  
50 1 ▶ if ( ref($copula) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $copula eq "ARRAY") 0 1 1
51 {
52 # add an empty prefix to all copula
53 0 $copula = { map { $_ => '' } @$copula };
54 }
55  
56 # Install declarator functions into caller's package, remembering
57 # the mapping and copula set for this declarator.
58 1 foreach my $sym (@$declarator)
59 {
60 ## no critic (ProhibitNoStrict)
61 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 ▶ *{"$from\::$sym"} = sub (&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 unshift @_, ( $mapping, $copula, $aliases );
65 2 goto &_declare;
66 1 };
67 ## use critic
68 }
69  
70 # Establish prototypes (same as "use subs") so Sub::Override can work
71 {
72 ## no critic (ProhibitNoStrict)
73 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 _predeclare(
75 2 ( map { "$from\::$_" } keys %$mapping ),
76 1 ( map { ( "UNIVERSAL::$_", "$_\::AUTOLOAD" ) } keys %$copula ),
77 );
78 ## use critic
79 }
80 }
81  
82 # Same as "use sub". All is fair if you predeclare.
83 sub _predeclare
84 13 ▶{
Subroutine _predeclare: called CC=2 SCAR=6.9
85 ## no critic (ProhibitNoStrict)
86 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 13 foreach my $sym (@_)
89 {
90 18 *$sym = \&$sym;
91 }
92 ## use critic
93 }
94  
95 sub _declare
96 2 ▶{
Subroutine _declare: called CC=5 SCAR=16.3
97 2 my ( $mapping, $copula, $aliases, $code ) = @_;
98 2 my $from = caller;
99  
100 # Table of collected objects.
101 2 my @objects;
102  
103 # Establish a lexical extent for overrided symbols; they will be
104 # restored automagically upon scope exit.
105 my %subs_replaced;
106 12 ▶ my $replace = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
107 ## no critic (ProhibitNoStrict)
108 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 12 my ( $sym, $code ) = @_;
111  
112 # Do the "use subs" predeclaration again before overriding, because
113 # Sub::Override cannot handle empty symbol slots. This is normally
114 # redundant (&import already did that), but we do it here anyway to
115 # guard against runtime deletion of symbol table entries.
116 12 _predeclare($sym);
117  
118 # Now replace the symbol for real.
119 12 ▶ $subs_replaced{$sym} ||= *$sym{CODE};
Condition50%1/2
$subs_replaced{$sym} ||= *$sym{"CODE"}
l!l
012
MC/DC0%0/1
$subs_replaced{$sym} ||= *$sym{"CODE"}
$subs_replaced{$sym}
Truth table50%1/2
A
A= $subs_replaced{$sym}
Aresult
11
00
120 12 *$sym = $code;
121 ## use critic
122 2 };
123  
124 # In DSL (domain-specific language) mode; install AUTOLOAD to handle all
125 # unrecognized calls for "foo is 1" (which gets translated to "is->foo(1)",
126 # and UNIVERSAL to collect "is foo" (which gets translated to "foo->is".
127 # The arguments are rolled into a Katamari structure for later analysis.
128 2 ▶ while ( my ( $sym, $prefix ) = each %$copula )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$copula) 4 2 6
129 {
130 $replace->(
131 14 ▶ "UNIVERSAL::$sym" => sub {
Subroutine __ANON__: called CC=7 SCAR=23.7
132  
133 # Turn "is some_field" into "some_field is 1"
134 14 ▶ my ( $key, @vals ) =
Branch: 2 branches
BranchTrueFalseTotal
() = ref $prefix ? &$prefix(@_) : ($prefix . $_[0], 1) or return 14 0 14
ref $prefix ? : 0 14 14
135 ref($prefix) ? $prefix->(@_) : ( $prefix . $_[0] => 1 )
136 or return;
137  
138 # If the copula returns a ready-to-use katamari object,
139 # don't try to roll it by ourself.
140 14 ▶ return $key
Branch: 1 branch
BranchTrueFalseTotal
if ref $key and ref $key eq "Object::Declare::Katamari" 0 14 14
Condition33%1/3
ref $key and ref $key eq "Object::Declare::Katamari"
!ll&&!rl&&r
1400
MC/DC0%0/2
ref $key and ref $key eq "Object::Declare::Katamari"
ref $key ref $key eq "Object::Declare::Katamari"
Truth table33%1/3
A and B
A= ref $keyB= ref $key eq "Object::Declare::Katamari"
ABresult
0X0
100
111
141 if ref($key) && ref($key) eq 'Object::Declare::Katamari';
142 14 ▶ $key = $aliases->{$key} if $aliases and exists $aliases->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $aliases and exists $aliases->{$key} 0 14 14
Condition33%1/3
$aliases and exists $aliases->{$key}
!ll&&!rl&&r
0140
MC/DC0%0/2
$aliases and exists $aliases->{$key}
$aliases exists $aliases->{$key}
Truth table33%1/3
A and B
A= $aliasesB= exists $aliases->{$key}
ABresult
0X0
100
111
143 14 unshift @vals, $key;
144 14 bless( \@vals => 'Object::Declare::Katamari' );
145 }
146 4 );
147 $replace->(
148 12 ▶ "$sym\::AUTOLOAD" => sub {
Subroutine __ANON__: called CC=6 SCAR=18.5
149  
150 # Handle "some_field is $some_value"
151 12 shift;
152  
153 12 my $field = our $AUTOLOAD;
154 12 ▶ return if $field =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /DESTROY$/ 0 12 12
155  
156 12 $field =~ s/^\Q$sym\E:://;
157  
158 12 ▶ my ( $key, @vals ) =
Branch: 2 branches
BranchTrueFalseTotal
() = ref $prefix ? &$prefix($field, @_) : ($prefix . $field, @_) or return 12 0 12
ref $prefix ? : 0 12 12
159 ref($prefix)
160 ? $prefix->( $field, @_ )
161 : ( $prefix . $field => @_ )
162 or return;
163  
164 12 ▶ $key = $aliases->{$key} if $aliases and exists $aliases->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $aliases and exists $aliases->{$key} 2 10 12
Condition66%2/3
$aliases and exists $aliases->{$key}
!ll&&!rl&&r
0102
MC/DC50%1/2
$aliases and exists $aliases->{$key}
$aliases exists $aliases->{$key}
Truth table66%2/3
A and B
A= $aliasesB= exists $aliases->{$key}
ABresult
0X0
100
111
165 12 unshift @vals, $key;
166 12 bless( \@vals, 'Object::Declare::Katamari' );
167 }
168 4 );
169 }
170  
171 2 my @overridden = map { "$from\::$_" } keys %$mapping;
172  
173 # Now install the collector symbols from class mappings
174 20 ▶ my $toggle_subs = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
175 20 foreach my $sym (@overridden)
176 {
177 ## no critic (ProhibitNoStrict)
178 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 ( $subs_replaced{$sym}, *$sym ) =
181 40 ( *$sym{CODE}, $subs_replaced{$sym} );
182 ## use critic
183 }
184 2 };
185  
186 2 ▶ while ( my ( $sym, $build ) = each %$mapping )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$mapping) 4 2 6
187 {
188 4 $replace->(
189 "$from\::$sym" => _make_object( $build => \@objects, $toggle_subs )
190 );
191 }
192  
193 # Let's play Katamari!
194 2 &$code;
195  
196 # Restore overriden subs
197 2 ▶ while ( my ( $sym, $code ) = each %subs_replaced )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %subs_replaced) 12 2 14
198 {
199 ## no critic (ProhibitNoStrict)
200 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
202 12 *$sym = $code;
203 ## use critic
204 }
205  
206 # In scalar context, returns hashref; otherwise preserve ordering
207 2 ▶ return ( wantarray ? @objects : {@objects} );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
208 }
209  
210 # Make a star from the Katamari!
211 sub _make_object
212 4 ▶{
Subroutine _make_object: called CC=1 SCAR=0.0
213 4 my ( $build, $schema, $toggle_subs ) = @_;
214  
215 10 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
216  
217 # Restore overriden subs
218 ## no critic (ProhibitNoStrict)
219 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 ## use critic
222  
223 10 ▶ my $name = ( ref( $_[0] ) ? undef : shift );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 6 4 10
224 10 my $args = \@_;
225 my $damacy = bless(
226 10 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
227 10 $toggle_subs->();
228  
229 my $rv = $build->(
230 ( $_[0] ? ( name => $_[0] ) : () ),
231 10 ▶ map { $_->unroll } @$args
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 6 4 10
232 );
233  
234 10 $toggle_subs->();
235  
236 10 return $rv;
237 10 } => 'Object::Declare::Damacy'
238 );
239  
240 10 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 6 4 10
241 {
242 6 return ($damacy);
243 }
244 else
245 {
246 4 push @$schema, $name => $damacy->($name);
247 }
248 4 };
249 }
250  
251 package Object::Declare::Katamari;
252  
253 2 ▶use overload "!" => \&negation, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
254  
255 sub negation
256 4 ▶{
Subroutine negation: called CC=2 SCAR=7.0
257 4 ▶ my @katamari = @{ $_[0] } or return ();
Branch: 1 branch
BranchTrueFalseTotal
my(@katamari) = @{$_[0];} or return () 4 0 4
258 4 $katamari[1] = !$katamari[1];
259 4 return bless( \@katamari, ref( $_[0] ) );
260 }
261  
262 # Unroll a Katamari structure into constructor arguments.
263 sub unroll
264 26 ▶{
Subroutine unroll: called CC=8 SCAR=20.8
265 26 ▶ my @katamari = @{ $_[0] } or return ();
Branch: 1 branch
BranchTrueFalseTotal
my(@katamari) = @{$_[0];} or return () 26 0 26
266 26 ▶ my $field = shift @katamari or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $field = shift @katamari or return () 26 0 26
267 26 my @unrolled;
268  
269 26 ▶ unshift @unrolled, pop(@katamari)->unroll
Branch: 1 branch
BranchTrueFalseTotal
ref $katamari[-1] eq __PACKAGE__ and do { unshift @unrolled, pop(@katamari)->unroll } 10 26 36
270 while ref( $katamari[-1] ) eq __PACKAGE__;
271  
272 26 ▶ if ( @katamari == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@katamari == 1) { } 22 4 26
273 {
274 # single value: "is foo"
275 22 ▶ if ( ref( $katamari[0] ) eq 'Object::Declare::Damacy' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $katamari[0] eq "Object::Declare::Damacy") 2 20 22
276 {
277 2 $katamari[0] = $katamari[0]->($field);
278 }
279 22 return ( $field => @katamari, @unrolled );
280 }
281 else
282 {
283 # Multiple values: "are qw( foo bar baz )"
284 4 foreach my $kata (@katamari)
285 {
286 8 ▶ $kata = $kata->() if ref($kata) eq 'Object::Declare::Damacy';
Branch: 1 branch
BranchTrueFalseTotal
if ref $kata eq "Object::Declare::Damacy" 4 4 8
287 }
288 4 return ( $field => \@katamari, @unrolled );
289 }
290 }
291  
292 2 1;
293  
294 __END__
↑ Summary