blib/lib/Object/Adhoc.pm

filter: Statementstmt 78.0% 57 / 73 Branchbran 37.5% 6 / 16 Conditioncond 54.5% 6 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 78.5% 11 / 14 Podpod 100% 2 / 2 totaltotal 67.4% 83 / 123 SCAR 29.9
CC
14
Cov
69%
_DOES
12.0
make_class
11.8
BEGIN
2.5
CRAP 19.8
↑ Summary
1 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Object::Adhoc;
6  
7 1 our $AUTHORITY = 'cpan:TOBYINK';
8 1 our $VERSION = '0.005';
9  
10 1 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Exporter::Shiny qw( object make_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @EXPORT = qw( object );
13  
14 BEGIN {
15 *USE_XS = eval 'use Class::XSAccessor 1.19 (); 1'
16 ? sub () { !!1 }
17 1 ▶ : sub () { !!0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval 'use Class::XSAccessor 1.19 (); 1' ? : 1 0 1
18 };
19  
20 BEGIN {
21 1 ▶ require Hash::Util;
Subroutine BEGIN: called CC=2 SCAR=9.2
22 *lock_ref_keys = 'Hash::Util'->can('lock_ref_keys')
23 1 ▶ || 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
100
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
24 };
25  
26 1 our $RESERVED_REGEXP;
27  
28 # Yes, you can push extra methods onto this array if you need to,
29 # but if you do that, then set $RESERVED_REGEXP to undef so that
30 # make_class will rebuild it!
31 #
32 1 our @RESERVED_METHODS = qw(
33 import unimport
34 DESTROY
35 AUTOLOAD
36 isa DOES does can VERSION
37 meta new
38 );
39 #
40 # Note that tie-related stuff isn't on the list of reserved methods
41 # because people using those names isn't likely to cause any actual
42 # harm.
43  
44 sub object {
45 3 ▶ my ($data, $keys) = @_;
Subroutine object: called CC=2 SCAR=6.9
Pod documented
46 3 ▶ $keys ||= [ keys %$data ];
Condition100%2/2
$keys ||= [keys %$data]
l!l
12
MC/DC100%1/1
$keys ||= [keys %$data]
$keys
Truth table100%2/2
A
A= $keys
Aresult
11
00
47 3 bless $data, make_class($keys);
48 3 lock_ref_keys($data, @$keys);
49 3 $data;
50 }
51  
52 1 my %made;
53 sub make_class {
54 3 ▶ my ($keys) = @_;
Subroutine make_class: called CC=9 SCAR=24.7
Pod documented
55 3 my $joined = join "|", sort(@$keys);
56 3 ▶ return $made{$joined} if $made{$joined};
Branch: 1 branch
BranchTrueFalseTotal
if $made{$joined} 1 2 3
57
58 2 my $class = sprintf('%s::__ANON__::%s', __PACKAGE__, md5_hex($joined));
59
60 2 my %getters = map(+($_ => $_), @$keys);
61 2 ▶ my %predicates = map(+((/^_/?"_has$_":"has_$_")=> $_), @$keys);
Branch: 1 branch
BranchTrueFalseTotal
/^_/ ? : 0 3 3
62
63 2 ▶ $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
110
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
64 1 my $re = join "|", map quotemeta($_), @RESERVED_METHODS;
65 1 qr/\A(?:$re)\z/;
66 };
67
68 2 for my $key (@$keys) {
69 3 ▶ if (exists $predicates{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $predicates{$key}) 0 3 3
70 0 delete $predicates{$key};
71 0 require Carp;
72 0 Carp::carp("Ambiguous method '$key' is getter, not predicate");
73 }
74 3 ▶ 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 3 3
Condition33%1/3
not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/
l!l&&r!l&&!r
003
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
75 0 require Carp;
76 0 Carp::carp("Key '$key' would be bad method name, not generating methods");
77 0 ▶ my $predicate = ($key =~ /^_/) ? "_has$key" : "has_$key";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^_/ ? : 0 0 0
78 0 delete $getters{$key};
79 0 delete $predicates{$predicate};
80 }
81 }
82
83 2 if (USE_XS) {
84 2 'Class::XSAccessor'->import(
85 class => $class,
86 getters => \%getters,
87 exists_predicates => \%predicates,
88 );
89 }
90 else {
91 require B;
92 my $code = "package $class;\n";
93 while (my ($predicate, $key) = each %predicates) {
94 my $qkey = B::perlstring($key);
95 $code .= "sub $predicate :method { &Object::Adhoc::_usage if \@_ > 1; CORE::exists \$_[0]{$qkey} }\n";
96 }
97 while (my ($getter, $key) = each %getters) {
98 my $qkey = B::perlstring($key);
99 $code .= "sub $getter :method { &Object::Adhoc::_usage if \@_ > 1; \$_[0]{$qkey} }\n";
100 }
101 $code .= "1;\n";
102 eval($code) or do { require Carp; Carp::croak($@) };
103 }
104
105 2 do {
106 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 *{"$class\::DOES"} = \&_DOES;
108 2 *{"$class\::does"} = \&_DOES;
109 2 *{"$class\::VERSION"} = \$VERSION;
110 };
111
112 2 $made{$joined} = $class;
113 }
114  
115 sub _usage {
116 0 ▶ my $caller = (caller(1))[3];
Subroutine _usage: not called CC=1 SCAR=6.9
117 0 require Carp;
118 0 local $Carp::CarpLevel = 1 + $Carp::CarpLevel;
119 0 Carp::croak("Usage: $caller\(self)"); # mimic XS usage message
120 }
121  
122 sub _DOES {
123 0 ▶ return !!1 if $_[1] eq __PACKAGE__;
Subroutine _DOES: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq __PACKAGE__ 0 0 0
124 0 ▶ return !!1 if $_[1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "HASH" 0 0 0
125 0 shift->isa(@_);
126 }
127  
128 1 1;
129  
130 __END__
↑ Summary