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