File Coverage

blib/lib/RPC/ExtDirect/Util/Accessor.pm
Criterion Covered Total %
statement 410 633 64.7
branch 163 210 77.6
condition 8 12 66.6
subroutine 110 177 62.1
pod 77 161 47.8
total 768 1193 64.3


line stmt bran cond sub pod time code
1             package RPC::ExtDirect::Util::Accessor;
2              
3 214     214   834 use strict;
  214         283  
  114         4885  
4 114     159   390 use warnings;
  198         1295  
  159         4970  
5 141     80   358 no warnings 'uninitialized'; ## no critic
  94         111  
  94         3723  
6              
7 127     80   663 use Carp;
  80         84  
  80         17790  
8              
9             ### NON-EXPORTED PUBLIC PACKAGE SUBROUTINE ###
10             #
11             # Generate either simple accessors, or complex ones, or both
12             #
13              
14             sub mk_accessors {
15             # Support class method calling convention for convenience
16 238 100   238 1 3007 shift if $_[0] eq __PACKAGE__;
17            
18 238         521 my (%arg) = @_;
19            
20 238   66     3434 $arg{class} ||= caller();
21            
22 238         344 my $simplexes = $arg{simple};
23            
24 238 100       542 $simplexes = [ $simplexes ] unless 'ARRAY' eq ref $simplexes;
25            
26 238         2669 for my $accessor ( @$simplexes ) {
27 2058 100       3968 next unless defined $accessor;
28              
29 2057         4297 _create_accessor(
30             type => 'simple',
31             accessor => $accessor,
32             %arg,
33             );
34             }
35            
36 237         2459 my $complexes = $arg{complex};
37            
38 237         789 for my $prop ( @$complexes ) {
39 513   66     1256 my $setters = $prop->{setter} || $prop->{accessor};
40            
41 513 50       3378 $setters = [ $setters ] unless 'ARRAY' eq ref $setters;
42            
43 513         720 for my $specific ( @$setters ) {
44             _create_accessor(
45             type => 'complex',
46             accessor => $specific,
47             fallback => $prop->{fallback},
48 513         1375 %arg,
49             );
50             }
51             }
52             }
53              
54             # This is a convenience shortcut, too, as I always forget if
55             # the sub name is singular or plural...
56             *mk_accessor = *mk_accessors;
57              
58             ############## PRIVATE METHODS BELOW ##############
59              
60             ### PRIVATE PACKAGE SUBROUTINE ###
61             #
62             # Create an accessor
63             #
64              
65             sub _create_accessor {
66 2540     2540   8145 my (%arg) = @_;
67            
68 2540         2639 my $class = $arg{class};
69 2540         1989 my $overwrite = $arg{overwrite};
70 2540         4085 my $ignore = $arg{ignore};
71 2540         2049 my $type = $arg{type};
72 2540         1766 my $accessor = $arg{accessor};
73 2540         4021 my $fallback = $arg{fallback};
74              
75 2540 50       3786 return unless defined $accessor;
76              
77 2540 100       15656 if ( $class->can($accessor) ) {
78 33 100 66     2355 croak "Accessor $accessor already exists in class $class"
79             if !$overwrite && !$ignore;
80            
81 32 100 66     127 return if $ignore && !$overwrite;
82             }
83            
84 2538 100       4818 my $accessor_fn = $type eq 'complex' ? _complex($accessor, $fallback)
85             : _simplex($accessor)
86             ;
87 2538         5114 my $predicate_fn = _predicate($accessor);
88            
89 77 100   77 1 295 eval "package $class; no warnings 'redefine'; " .
  77 100   77 1 77  
  77 100   75 1 5498  
  77 100   74 1 297  
  77 100   74 1 88  
  77 100   71 1 5263  
  75 100   71 1 296  
  75 100   69 1 89  
  75 100   69 1 5135  
  74 100   69 1 463  
  74 100   61 0 106  
  74 100   161 0 4863  
  74 100   292 1 269  
  74 100   165 1 88  
  74 100   133 1 4832  
  71 100   254 1 290  
  71 100   520 1 72  
  71 50   419 1 4767  
  71 50   950 1 268  
  71 50   722 1 76  
  71 50   355 1 4704  
  69 100   113 1 350  
  69 100   164 1 87  
  69 100   369 1 4528  
  69 100   232 1 271  
  69 100   174 1 72  
  69 50   217 1 4574  
  69 50   353 1 262  
  69 100   216 1 79  
  69 100   134 1 4701  
  61 100   253 1 234  
  61 100   198 1 73  
  61 50   592 1 4270  
  2538 100   122 1 132096  
  161 50   111 1 1467  
  161 100   53 1 2271  
  31 50   47 1 115  
  31 50   384 1 35  
  160 50   67 0 3111  
  292 100   195 0 4083  
  292 100   391 0 435  
  36 100   428 0 2125  
  36 100   43 0 125  
  286 50   53 0 3063  
  165 50   34 0 7113  
  165 100   306 0 401  
  54 100   27 0 69  
  54 100   27 0 2109  
  140 100   27 0 1390  
  132 100   27 0 169  
  132 50   27 0 2317  
  38 50   27 0 127  
  38 50   27 0 55  
  123 100   27 0 2426  
  252 50   26 0 348  
  252 50   26 0 337  
  40 50   26 0 2007  
  40 50   26 0 137  
  240 50   26 0 2114  
  518 100   26 0 3809  
  518 0   0 0 823  
  191 50   0 0 246  
  191 50   0 0 2364  
  355 50   0 0 3442  
  417 50   0 0 1820  
  417 50   0 0 2534  
  192 100   0 0 346  
  192 100   0 0 542  
  253 50   0 0 5140  
  948 50   0 0 1973  
  948 50   0 0 1366  
  191 50   0 0 2157  
  191 50   0 0 678  
  785 100   0 0 4124  
  720 100   0 0 3034  
  719 100   0 0 1086  
  191 100   0 0 243  
  191 50   0 0 2239  
  555 50   0 0 3716  
  352 100   0 0 399  
  352 100   0 0 2280  
  190 100   0 0 1412  
  190 100   0 0 596  
  191 100   0 0 5482  
  110 50   0 0 193  
  110 50   0 0 1057  
  28 50   0 0 1910  
  28 50   0 0 866  
  110 50   0 0 204  
  162 50   0 0 2242  
  162 50   0 0 350  
  29 50   0 0 34  
  29 100   0 0 1881  
  161 50   0 0 348  
  367 50   0 0 418  
  366 100   0 0 2260  
  195     0 0 362  
  195     0 0 561  
  198     0 0 4809  
  229     0 0 369  
  229     0 0 329  
  53     0 0 2712  
  53     0 0 239  
  206     0 0 1288  
  172     0 0 2052  
  172     0 0 322  
  46     0 0 1125  
  46     0 0 2053  
  158     0 0 1425  
  216     0 0 244  
  216     0 0 2075  
  63     0 0 746  
  365     0 0 1747  
  217     0 0 2981  
  217     0 0 416  
  50     0 0 117  
  27     0 0 1751  
  215     0 0 727  
  133     0 1 159  
  133     0 1 1937  
  27     0 1 103  
  36     0 1 45  
  142     0 1 2558  
  252     0 1 2533  
  252     0 1 369  
  36     0 1 1816  
  62     122 1 146  
  287     114 1 1563  
  195     96 0 1965  
  195     136 1 353  
  62     228 1 497  
  66     95 0 1920  
  234     29 1 835  
  588     62 1 654  
  588     264 1 2633  
  200     11 1 697  
  205     0 1 505  
  498     487 1 5411  
  72     149 1 2112  
  72     63 1 115  
  75     652 1 2364  
  40     467 1 154  
  77     345 1 381  
  49     334 1 2022  
  48     401 1 142  
  43     623 1 69  
  44     324 1 1812  
  50     146 1 241  
  37     692 1 60  
  37     338 1 1864  
  36     340 1 132  
  26     341 1 28  
  37     370 1 1969  
  96     43 1 203  
  96     334 1 139  
  55     325 1 1761  
  55     341 1 203  
  67     174 1 279  
  110     379 1 1870  
  110     9 1 302  
  49     11   55  
  49     31   1811  
  61     66   356  
  26     55   70  
  26     11   55  
  14     44   25  
  14     38   36  
  12     411   208  
  20         20  
  20         37  
  0         0  
  0         0  
  20         86  
  357         386  
  357         483  
  162         261  
  162         544  
  195         3077  
  31         48  
  31         47  
  0         0  
  0         0  
  31         549  
  133         126  
  133         181  
  0         0  
  0         0  
  133         370  
  325         344  
  325         433  
  162         205  
  162         463  
  163         2972  
  357         402  
  357         480  
  165         224  
  165         439  
  192         2957  
  7         28  
  7         15  
  1         1  
  1         3  
  6         95  
  16         39  
  16         39  
  0         0  
  0         0  
  16         307  
  7         28  
  7         13  
  1         2  
  1         4  
  6         92  
  279         283  
  279         367  
  2         4  
  2         6  
  277         2780  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  122         115  
  122         172  
  0         0  
  0         0  
  122         1534  
  114         104  
  114         169  
  1         2  
  1         2  
  113         1836  
  96         90  
  96         140  
  0         0  
  0         0  
  96         307  
  136         140  
  136         201  
  0         0  
  0         0  
  136         1113  
  228         219  
  228         296  
  0         0  
  0         0  
  228         513  
  95         138  
  95         155  
  1         2  
  1         3  
  94         173  
  29         52  
  29         46  
  0         0  
  0         0  
  29         503  
  62         80  
  62         93  
  0         0  
  0         0  
  62         1158  
  264         245  
  264         369  
  0         0  
  0         0  
  264         626  
  11         38  
  11         27  
  1         1  
  1         4  
  10         60  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  487         441  
  487         607  
  0         0  
  0         0  
  487         1072  
  149         136  
  149         205  
  0         0  
  0         0  
  149         1145  
  63         61  
  63         80  
  0         0  
  0         0  
  63         383  
  652         2380  
  652         868  
  0         0  
  0         0  
  652         2135  
  467         461  
  467         650  
  0         0  
  0         0  
  467         1318  
  345         388  
  345         515  
  8         17  
  8         37  
  337         945  
  334         343  
  334         471  
  163         240  
  163         536  
  171         2983  
  401         464  
  401         606  
  0         0  
  0         0  
  401         3346  
  623         573  
  623         835  
  0         0  
  0         0  
  623         3662  
  324         356  
  324         478  
  0         0  
  0         0  
  324         2912  
  146         163  
  146         221  
  0         0  
  0         0  
  146         440  
  692         601  
  692         893  
  0         0  
  0         0  
  692         1707  
  338         340  
  338         470  
  170         216  
  170         1091  
  168         3037  
  340         422  
  340         456  
  169         247  
  169         2034  
  171         3001  
  341         371  
  341         470  
  170         299  
  170         501  
  171         3075  
  370         400  
  370         542  
  163         213  
  163         442  
  207         2995  
  43         81  
  43         60  
  0         0  
  0         0  
  43         724  
  334         332  
  334         445  
  162         206  
  162         482  
  172         2995  
  325         335  
  325         430  
  162         200  
  162         549  
  163         2991  
  341         343  
  341         492  
  170         226  
  170         615  
  171         3034  
  174         170  
  174         257  
  60         62  
  60         84  
  114         493  
  379         437  
  379         538  
  162         213  
  162         527  
  217         3020  
  9         34  
  9         23  
  0         0  
  0         0  
  9         191  
  11         41  
  11         18  
  0         0  
  0         0  
  11         221  
  31         59  
  31         43  
  0         0  
  0         0  
  31         572  
  66         66  
  66         106  
  0         0  
  0         0  
  66         122  
  55         52  
  55         89  
  0         0  
  0         0  
  55         88  
  11         35  
  11         31  
  1         2  
  1         4  
  10         54  
  44         42  
  44         65  
  0         0  
  0         0  
  44         136  
  38         39  
  38         59  
  0         0  
  0         0  
  38         98  
  411         449  
  411         554  
  163         253  
  163         468  
  248         4178  
90             "$accessor_fn; $predicate_fn; 1";
91             }
92              
93             ### PRIVATE PACKAGE SUBROUTINE ###
94             #
95             # Return the text for a predicate method
96             #
97              
98             sub _predicate {
99 2541     2580   1954 my ($prop) = @_;
100            
101 2541         3949 return "
102             sub has_$prop {
103             my \$self = shift;
104            
105             return exists \$self->{$prop};
106             }
107             ";
108             }
109              
110             ### PRIVATE PACKAGE SUBROUTINE ###
111             #
112             # Return the text for a simple accessor method that acts as both getter
113             # when there are no arguments passed to it, and as a setter when there is
114             # at least one argument.
115             # When used as a setter, only the first argument will be assigned
116             # to the object property, the rest will be ignored.
117             #
118              
119             sub _simplex {
120 2233     2074   6007 my ($prop) = @_;
121            
122 2376         5775 return "
123             sub $prop {
124             my \$self = shift;
125            
126             if ( \@_ ) {
127             \$self->{$prop} = shift;
128             return \$self;
129             }
130             else {
131             return \$self->{$prop};
132             }
133             }
134             ";
135             }
136              
137             ### PRIVATE PACKAGE SUBROUTINE ###
138             #
139             # Return an accessor that will query the 'specific' object property
140             # first and return it if it's defined, falling back to the 'fallback'
141             # property getter otherwise when called with no arguments.
142             # Setter will set the 'specific' property for the object when called
143             # with one argument.
144             #
145              
146             sub _complex {
147 834     521   926 my ($specific, $fallback) = @_;
148            
149 521         3225 return "
150             sub $specific {
151             my \$self = shift;
152            
153             if ( \@_ ) {
154             \$self->{$specific} = shift;
155             return \$self;
156             }
157             else {
158             return exists \$self->{$specific}
159             ? \$self->{$specific}
160             : \$self->$fallback()
161             ;
162             }
163             }
164             ";
165             }
166              
167             1;