blib/lib/Refine.pm

filter: Statementstmt 100% 63 / 63 Branchbran 75.0% 9 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 94.4% 85 / 90 CC 10 SCAR 15.1
CC
10
Cov
95%
__ANON__
9.0
BEGIN
2.0
BEGIN
1.0
CRAP 4.5
↑ Summary
1 package Refine;
2  
3 =head1 NAME
4  
5 Refine - Refine an instance with new methods
6  
7 =head1 VERSION
8  
9 0.01
10  
11 =head1 DESCRIPTION
12  
13 L<Refine> is a module that export C<$_refine> which can be used to add
14 methods object instances. Each C<$_refine> call on the object will create a
15 new class with the new refined methods and rebless the instance into that
16 class, which keeps the original class as it was.
17  
18 This is an EXPERIMENTAL release. The class generator might change in future releases.
19  
20 =head1 SYNOPSIS
21  
22 use Refine;
23 use Data::Dumper ();
24  
25 my $obj = Some::Class->new;
26  
27 # add the dump() method to the $obj instance
28 $obj->$_refine(
29 dump => sub { Data::Dumper->new([$_[0])->Terse(1)->SortKeys(1)->Dump },
30 );
31  
32 =head1 OPTIONAL MODULES
33  
34 =over 4
35  
36 =item * Sub::Name
37  
38 If you have L<Sub::Name> installed, the methods will have proper names,
39 instead of "__ANON__". This will make stacktraces easier to read.
40  
41 =back
42  
43 =cut
44  
45 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use constant SUB_NAME => eval 'require Sub::Name;1' ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
eval 'require Sub::Name;1' ? : 0 3 3
49 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 3 our $VERSION = '0.01';
52 3 our @EXPORT = '$_refine';
53  
54 3 my %PRIVATE2PUBLIC;
55  
56 10 ▶our $_refine = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
57 10 my ($self, %patch) = @_;
58 10 my $class = ref $self;
59 10 my $private_name = join ':', $class, map { $_, $patch{$_} } sort keys %patch;
60 10 my $refined_class = $PRIVATE2PUBLIC{$private_name};
61  
62 10 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 1 9 10
63 1 Carp::confess("Can only add methods to instances, not $self");
64 }
65  
66 9 ▶ unless ($refined_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($refined_class) 9 0 9
67 9 my $base_class = $class;
68  
69 9 ▶ if ($class =~ s!::WITH::(.*)!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ s/::WITH::(.*)//) 3 6 9
70 3 ▶ $patch{$_} ||= '' for grep { !/^_\d+$/ } split /::/, $1;
Condition50%1/2
$patch{$_} ||= ""
l!l
60
MC/DC0%0/1
$patch{$_} ||= ""
$patch{$_}
Truth table50%1/2
A
A= $patch{$_}
Aresult
11
00
71 }
72  
73 9 my $i = 0;
74 9 my $public_name = substr +("$class\::WITH::" .join '::', sort keys %patch), 0, 180;
75  
76 9 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$refined_class->can("new") and do { do { $refined_class = "${public_name}::_$i"; ++$i } } 6 9 15
77 15 $refined_class = "$public_name\::_$i";
78 15 $i++;
79 } while ($refined_class->can('new'));
80 9 $PRIVATE2PUBLIC{$private_name} = $refined_class;
81 3 ▶ eval "package $refined_class;use base '$base_class';1" or Carp::confess("Failed to refine $class: $@");
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "package $refined_class;use base '${base_class}';1" or Carp::confess("Failed to refine ${class}: $@") 9 0 9
82  
83 9 for my $n (grep { $patch{$_} } keys %patch) {
84 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 17 *{"$refined_class\::$n"} = SUB_NAME ? Sub::Name::subname("$refined_class\::$n", $patch{$n}) : $patch{$n};
86 }
87 }
88  
89 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 9 bless $self, $refined_class;
91 9 $self;
92 3 };
93  
94 =head1 COPYRIGHT AND LICENSE
95  
96 Copyright (C) 2014, Jan Henning Thorsen
97  
98 This program is free software, you can redistribute it and/or modify it under
99 the terms of the Artistic License version 2.0.
100  
101 =head1 AUTHOR
102  
103 Jan Henning Thorsen - C<jhthorsen@cpan.org>
104  
105 =cut
106  
107 3 1;
↑ Summary