blib/lib/Class/MakeMethods/Utility/Inheritable.pm

filter: Statementstmt 100% 32 / 32 Branchbran 91.6% 11 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 93.1% 54 / 58 CC 8 SCAR 11.1
CC
8
Cov
96%
find_vself
5.0
import
2.1
get_vvalue
2.0
CRAP 3.0
« blib/lib/Class/MakeMethods/Emulator/accessors.pm ↑ Summary tests/xemulator/class_methodmaker/Test.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Utility::Inheritable - "Inheritable" data
4  
5  
6 =head1 SYNOPSIS
7  
8 package MyClass;
9 sub new { ... }
10
11 package MySubclass;
12 @ISA = 'MyClass';
13 ...
14 my $obj = MyClass->new(...);
15 my $subobj = MySubclass->new(...);
16
17 use Class::MakeMethods::Utility::Inheritable qw(get_vvalue set_vvalue );
18
19 my $dataset = {};
20 set_vvalue($dataset, 'MyClass', 'Foobar'); # Set value for class
21 get_vvalue($dataset, 'MyClass'); # Gets value "Foobar"
22
23 get_vvalue($dataset, $obj); # Objects "inherit"
24 set_vvalue($dataset, $obj, 'Foible'); # Until you override
25 get_vvalue($dataset, $obj); # Now finds "Foible"
26
27 get_vvalue($dataset, 'MySubclass'); # Subclass "inherits"
28 get_vvalue($dataset, $subobj); # As do its objects
29 set_vvalue($dataset, 'MySubclass', 'Foozle'); # Until we override it
30 get_vvalue($dataset, 'MySubclass'); # Now finds "Foozle"
31
32 get_vvalue($dataset, $subobj); # Change cascades down
33 set_vvalue($dataset, $subobj, 'Foolish'); # Until we override again
34
35 get_vvalue($dataset, 'MyClass'); # Superclass is unchanged
36  
37 =head1 DESCRIPTION
38  
39 This module provides several functions which allow you to store values in a hash corresponding to both objects and classes, and to retrieve those values by searching a object's inheritance tree until it finds a matching entry.
40  
41 This functionality is used by Class::MakeMethods::Standard::Inheritable and Class::MakeMethods::Composite::Inheritable to construct methods that can both store class data and be overriden on a per-object level.
42  
43 =cut
44  
45 ########################################################################
46  
47 package Class::MakeMethods::Utility::Inheritable;
48  
49 9 $VERSION = 1.000;
50  
51 9 @EXPORT_OK = qw( get_vvalue set_vvalue find_vself );
52 9 ▶sub import { require Exporter and goto &Exporter::import } # lazy Exporter
Subroutine import: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
require Exporter and goto \&Exporter::import 9 0 9
53  
54 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 ########################################################################
57  
58 =head1 REFERENCE
59  
60 =head2 find_vself
61  
62 $vself = find_vself( $dataset, $instance );
63  
64 Searches $instance's inheritance tree until it finds a matching entry in the dataset, and returns either the instance, the class that matched, or undef.
65  
66 =cut
67  
68 72 ▶sub find_vself {
Subroutine find_vself: called CC=5 SCAR=16.1
Pod documented
69 72 my $dataset = shift;
70 72 my $instance = shift;
71  
72 72 ▶ return $instance if ( exists $dataset->{$instance} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $dataset->{$instance} 31 41 72
73
74 41 my $v_self;
75 41 ▶ my @isa_search = ( ref($instance) || $instance );
Condition66%2/3
ref $instance || $instance
l!l&&r!l&&!r
27140
MC/DC0%0/2
ref $instance || $instance
ref $instance $instance
Truth table66%2/3
A || B
A= ref $instanceB= $instance
ABresult
1X1
011
000
76 41 ▶ while ( scalar @isa_search ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @isa_search) 49 15 64
77 49 $v_self = shift @isa_search;
78 49 ▶ return $v_self if ( exists $dataset->{$v_self} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $dataset->{$v_self} 26 23 49
79 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 23 unshift @isa_search, @{"$v_self\::ISA"};
81 }
82 15 return;
83 }
84  
85 =head2 get_vvalue
86  
87 $value = get_vvalue( $dataset, $instance );
88  
89 Searches $instance's inheritance tree until it finds a matching entry in the dataset, and returns that value
90  
91 =cut
92  
93 49 ▶sub get_vvalue {
Subroutine get_vvalue: called CC=2 SCAR=6.9
Pod documented
94 49 my $dataset = shift;
95 49 my $instance = shift;
96 49 my $v_self = find_vself($dataset, $instance);
97 # warn "Dataset: " . join( ', ', %$dataset );
98 # warn "Retrieving $dataset -> $instance ($v_self): '$dataset->{$v_self}'";
99 49 ▶ return $v_self ? $dataset->{$v_self} : ();
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 41 8 49
100 }
101  
102 =head2 set_vvalue
103  
104 $value = set_vvalue( $dataset, $instance, $value );
105  
106 Searches $instance's inheritance tree until it finds a matching entry in the dataset, and returns that value
107  
108 =cut
109  
110 15 ▶sub set_vvalue {
Subroutine set_vvalue: called CC=2 SCAR=6.9
Pod documented
111 15 my $dataset = shift;
112 15 my $instance = shift;
113 15 my $value = shift;
114 15 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 13 2 15
115 # warn "Setting $dataset -> $instance = $value";
116 13 $dataset->{$instance} = $value;
117 } else {
118 # warn "Clearing $dataset -> $instance";
119 2 delete $dataset->{$instance};
120 2 undef;
121 }
122 }
123  
124 ########################################################################
125  
126 9 1;
« blib/lib/Class/MakeMethods/Emulator/accessors.pm ↑ Summary tests/xemulator/class_methodmaker/Test.pm »