blib/lib/Object/Adhoc.pm

filter: Statementstmt 82.9% 107 / 129 Branchbran 63.3% 38 / 60 Conditioncond 54.5% 18 / 33 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 85.0% 17 / 20 Podpod 100% 2 / 2 totaltotal 70.3% 185 / 263 CC 46 SCAR 21.9
CC
46
Cov
73%
make_class
13.1
_recurse
12.8
_DOES
12.0
CRAP 8.9
↑ Summary
1 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Object::Adhoc;
6  
7 4 our $AUTHORITY = 'cpan:TOBYINK';
8 4 our $VERSION = '0.008';
9  
10 4 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Exporter::Shiny qw( object make_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 our @EXPORT = qw( object );
14  
15 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
16 *USE_XS = eval 'use Class::XSAccessor 1.19 (); 1'
17 ? sub () { !!1 }
18 4 ▶ : sub () { !!0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval 'use Class::XSAccessor 1.19 (); 1' ? : 4 0 4
19 };
20  
21 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
22 4 require Hash::Util;
23 *lock_ref_keys = 'Hash::Util'->can('lock_ref_keys')
24 0 ▶ || sub { return; };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
'Hash::Util'->can('lock_ref_keys') || sub { return; }
l!l&&r!l&&!r
400
MC/DC0%0/2
'Hash::Util'->can('lock_ref_keys') || sub { return; }
'Hash::Util'->can('lock_ref_keys') sub { return; }
Truth table33%1/3
A || B
A= 'Hash::Util'->can('lock_ref_keys')B= sub { return; }
ABresult
1X1
011
000
25 };
26  
27 4 our $RESERVED_REGEXP;
28  
29 # Yes, you can push extra methods onto this array if you need to,
30 # but if you do that, then set $RESERVED_REGEXP to undef so that
31 # make_class will rebuild it!
32 #
33 4 our @RESERVED_METHODS = qw(
34 import unimport
35 DESTROY
36 AUTOLOAD
37 isa DOES does can VERSION
38 meta new
39 TO_JSON
40 );
41 #
42 # Note that tie-related stuff isn't on the list of reserved methods
43 # because people using those names isn't likely to cause any actual
44 # harm.
45  
46 11 ▶sub object {
Subroutine object: called CC=11 SCAR=24.7
Pod documented
47 11 ▶ my ($data, $keys, %opts) =
Branch: 2 branches
BranchTrueFalseTotal
@_ == 0 ? : 0 11 11
@_ <= 2 ? : 4 7 11
48 (@_ == 0) ? _croak('Expected hashref') :
49 (@_ <= 2) ? @_ : ($_[0], undef, @_[1..$#_]);
50 11 ▶ $keys ||= delete($opts{keys}) || [ keys %$data ];
Condition60%3/5
$keys ||= delete $opts{"keys"} || [keys %$data]
l!l&&r!l&&!r
1100
delete $opts{'keys'} || [keys %$data]
l!l
010
MC/DC0%0/2
$keys ||= delete $opts{"keys"} || [keys %$data]
$keys delete $opts{'keys'}
Truth table33%1/3
A ||= B
A= $keysB= delete $opts{'keys'}
ABresult
1X1
011
000
51 11 bless $data, make_class($keys, %opts);
52 11 ▶ if ($opts{recurse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"recurse"}) 7 4 11
53 7 ▶ _croak('Bad recurse option') if ref($opts{recurse}) || $opts{recurse} !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"recurse"} or not $opts{"recurse"} =~ /\A[0-9]+\z/ 0 7 7
Condition33%1/3
ref $opts{"recurse"} or not $opts{"recurse"} =~ /\A[0-9]+\z/
l!l&&r!l&&!r
007
MC/DC0%0/2
ref $opts{"recurse"} or not $opts{"recurse"} =~ /\A[0-9]+\z/
ref $opts{"recurse"} not $opts{"recurse"} =~ /\A[0-9]+\z/
Truth table33%1/3
A or not(B)
A= ref $opts{"recurse"}B= not $opts{"recurse"} =~ /\A[0-9]+\z/
ABresult
1X1
011
000
54 7 ▶ my %seen = (refaddr($data) => undef, %{ delete($opts{seen}) or {}});
Condition100%2/2
delete $opts{'seen'} || {}
l!l
61
MC/DC100%1/1
delete $opts{'seen'} || {}
delete $opts{'seen'}
Truth table100%2/2
A
A= delete $opts{'seen'}
Aresult
11
00
55 7 for my $k (keys %$data) {
56 11 ▶ ref $data->{$k} or next;
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$k} or next 6 5 11
57 6 $data->{$k} = _recurse($data->{$k}, %opts, seen => \%seen);
58 }
59 }
60 11 lock_ref_keys($data, @$keys);
61 11 $data;
62 }
63  
64 14 ▶sub _recurse {
Subroutine _recurse: called CC=11 SCAR=25.5
65 14 my ($ref, %opts) = @_;
66 14 my $lvl = $opts{recurse} - 1;
67 14 my $reftype = ref $ref;
68 14 ▶ my %seen = %{ delete($opts{seen}) or {} };
Condition50%1/2
delete $opts{'seen'} || {}
l!l
140
MC/DC0%0/1
delete $opts{'seen'} || {}
delete $opts{'seen'}
Truth table50%1/2
A
A= delete $opts{'seen'}
Aresult
11
00
69 14 ▶ return $ref if $lvl < 1 || !$reftype || exists $seen{refaddr($ref)};
Branch: 1 branch
BranchTrueFalseTotal
if $lvl < 1 or not $reftype or exists $seen{builtin::refaddr($ref)} 3 11 14
Condition66%4/6
$lvl < 1 or not $reftype or exists $seen{builtin::refaddr($ref)}
l!l&&r!l&&!r
3011
$lvl < 1 or not $reftype
l!l&&r!l&&!r
3011
MC/DC33%1/3
$lvl < 1 or not $reftype or exists $seen{builtin::refaddr($ref)}
$lvl < 1 not $reftype exists $seen{builtin::refaddr($ref)}
Truth table50%2/4
A or not(B) or C
A= $lvl < 1B= not $reftypeC= exists $seen{builtin::refaddr($ref)}
ABCresult
1XX1
01X1
0011
0000
70
71 11 $seen{refaddr($ref)} = undef;
72
73 11 ▶ if ($reftype eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "ARRAY") 3 8 11
74 3 ▶ @$ref = map { ref($_) ? _recurse($_, %opts, recurse => $lvl, seen => \%seen) : $_ } @$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 5 1 6
75 3 return $ref;
76 }
77
78 8 ▶ if ($reftype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "HASH") 6 2 8
79 6 return object($ref, %opts, recurse => $lvl, seen => \%seen);
80 }
81
82 2 ▶ if ($reftype eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "CODE") 1 1 2
83 2 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=21.7
84 2 my $wa = wantarray;
85 2 ▶ if ($wa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wa) { } 0 2 2
elsif (defined $wa) { } 2 0 2
86 0 ▶ return map { ref($_) ? _recurse($_, %opts, recurse => $lvl, seen => \%seen) : $_ } $ref->(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
87 }
88 elsif (defined $wa) {
89 2 local $_ = $ref->(@_);
90 2 ▶ return ref($_) ? _recurse($_, %opts, recurse => $lvl, seen => \%seen) : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 0 2
91 }
92 0 goto $ref;
93 1 };
94 }
95  
96 1 ▶ if ($reftype eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "REF") 1 0 1
97 1 ▶ ($$ref) = map { ref($_) ? _recurse($_, %opts, recurse => $lvl, seen => \%seen) : $_ } $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 0 1
98 1 return $ref;
99 }
100  
101 0 return $ref;
102 }
103  
104 4 my %made;
105 13 ▶sub make_class {
Subroutine make_class: called CC=11 SCAR=25.7
Pod documented
106 13 my ($keys, %opts) = @_;
107 13 my $joined = join "|", sort(@$keys);
108 13 ▶ $joined .= '*CTOR' if $opts{ctor};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"ctor"} 1 12 13
109 13 ▶ return $made{$joined} if $made{$joined};
Branch: 1 branch
BranchTrueFalseTotal
if $made{$joined} 4 9 13
110
111 9 my $class = sprintf('%s::__ANON__::%s', __PACKAGE__, md5_hex($joined));
112
113 9 my %getters = map(+($_ => $_), @$keys);
114 9 ▶ my %predicates = map(+((/^_/?"_has$_":"has_$_")=> $_), @$keys);
Branch: 1 branch
BranchTrueFalseTotal
/^_/ ? : 0 17 17
115
116 9 ▶ $RESERVED_REGEXP ||= do {
Condition66%2/3
$Object::Adhoc::RESERVED_REGEXP ||= do { my $re = join("|", map(quotemeta($_), @RESERVED_METHODS)); qr/\A(?:$re)\z/ }
l!l&&r!l&&!r
540
MC/DC0%0/2
$Object::Adhoc::RESERVED_REGEXP ||= do { my $re = join("|", map(quotemeta($_), @RESERVED_METHODS)); qr/\A(?:$re)\z/ }
$Object::Adhoc::RESERVED_REGEXP do { my $re = join("|", map(quotemeta($_), @RESERVED_METHODS)); qr/\A(?:$re)\z/ }
Truth table66%2/3
A ||= B
A= $Object::Adhoc::RESERVED_REGEXPB= do { my $re = join("|", map(quotemeta($_), @RESERVED_METHODS)); qr/\A(?:$re)\z/ }
ABresult
1X1
011
000
117 4 my $re = join "|", map quotemeta($_), @RESERVED_METHODS;
118 4 qr/\A(?:$re)\z/;
119 };
120
121 9 for my $key (@$keys) {
122 17 ▶ if (exists $predicates{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $predicates{$key}) 0 17 17
123 0 delete $predicates{$key};
124 0 require Carp;
125 0 Carp::carp("Ambiguous method '$key' is getter, not predicate");
126 }
127 17 ▶ if ($key !~ /^[^\W0-9]\w*$/s or $key =~ $RESERVED_REGEXP) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/) 0 17 17
Condition33%1/3
not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/
l!l&&r!l&&!r
0017
MC/DC0%0/2
not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/
not $key =~ /^[^\W0-9]\w*$/s $key =~ /$RESERVED_REGEXP/
Truth table33%1/3
not(A) or B
A= not $key =~ /^[^\W0-9]\w*$/sB= $key =~ /$RESERVED_REGEXP/
ABresult
1X1
011
000
128 0 require Carp;
129 0 Carp::carp("Key '$key' would be bad method name, not generating methods");
130 0 ▶ my $predicate = ($key =~ /^_/) ? "_has$key" : "has_$key";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^_/ ? : 0 0 0
131 0 delete $getters{$key};
132 0 delete $predicates{$predicate};
133 }
134 }
135
136 9 if (USE_XS) {
137 9 'Class::XSAccessor'->import(
138 class => $class,
139 getters => \%getters,
140 exists_predicates => \%predicates,
141 );
142 }
143 else {
144 require B;
145 my $code = "package $class;\n";
146 while (my ($predicate, $key) = each %predicates) {
147 my $qkey = B::perlstring($key);
148 $code .= "sub $predicate :method { &Object::Adhoc::_usage if \@_ > 1; CORE::exists \$_[0]{$qkey} }\n";
149 }
150 while (my ($getter, $key) = each %getters) {
151 my $qkey = B::perlstring($key);
152 $code .= "sub $getter :method { &Object::Adhoc::_usage if \@_ > 1; \$_[0]{$qkey} }\n";
153 }
154 $code .= "1;\n";
155 eval($code) or die($@);
156 }
157
158 9 do {
159 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 9 *{"$class\::DOES"} = \&_DOES;
161 9 *{"$class\::does"} = \&_DOES;
162 9 *{"$class\::VERSION"} = \$VERSION;
163 9 *{"$class\::TO_JSON"} = \&_TO_JSON;
164
165 9 ▶ if ( $opts{ctor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ctor"}) 1 8 9
166 1 my $re = join "|", map quotemeta($_), @$keys;
167 1 ▶ *{"$class\::new"} = sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
168 3 ▶ my ($class, %hash) = (@_ == 2 and ref $_[1] eq 'HASH') ? ($_[0], %{$_[1]}) : (@_ % 2 == 1) ? @_ : _usage('class', 'hashref');
Branch: 2 branches
BranchTrueFalseTotal
@_ == 2 && ref $_[1] eq 'HASH' ? : 1 2 3
@_ % 2 == 1 ? : 2 0 2
Condition66%2/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
201
MC/DC50%1/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
169 3 for (keys %hash) {
170 7 ▶ /\A(?:$re)\z/ or _croak("Bad key: $_");
Branch: 1 branch
BranchTrueFalseTotal
/\A(?:$re)\z/ or _croak("Bad key: $_") 6 1 7
171 }
172 2 ▶ return bless(\%hash, ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
173 1 };
174 }
175 };
176
177 9 $made{$joined} = $class;
178 }
179  
180 0 ▶sub _usage {
Subroutine _usage: not called CC=2 SCAR=17.9
181 0 my $caller = (caller(1))[3];
182 0 require Carp;
183 0 local $Carp::CarpLevel = 1 + $Carp::CarpLevel;
184 0 ▶ my @fields = @_ ? @_ : ('self');
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
185 0 Carp::croak("Usage: $caller\(@{[join q[, ], @fields]})"); # mimic XS usage message
186 }
187  
188 1 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
189 1 require Carp;
190 1 goto \&Carp::croak;
191 }
192  
193 0 ▶sub _DOES {
Subroutine _DOES: not called CC=3 SCAR=24.8
194 0 ▶ return !!1 if $_[1] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq __PACKAGE__ 0 0 0
195 0 ▶ return !!1 if $_[1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "HASH" 0 0 0
196 0 shift->isa(@_);
197 }
198  
199 1 ▶sub _TO_JSON {
Subroutine _TO_JSON: called CC=1 SCAR=0.0
200 1 my %hash = %{ +shift };
201 1 \%hash;
202 }
203  
204 4 1;
205  
206 __END__
↑ Summary