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 339     339   1188 use strict;
  339         478  
  160         5067  
4 160     184   496 use warnings;
  277         1734  
  184         5054  
5 166     80   423 no warnings 'uninitialized'; ## no critic
  92         96  
  92         3861  
6              
7 154     80   777 use Carp;
  80         85  
  80         18242  
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 2057 shift if $_[0] eq __PACKAGE__;
17            
18 238         474 my (%arg) = @_;
19            
20 238   66     3222 $arg{class} ||= caller();
21            
22 238         317 my $simplexes = $arg{simple};
23            
24 238 100       514 $simplexes = [ $simplexes ] unless 'ARRAY' eq ref $simplexes;
25            
26 238         2510 for my $accessor ( @$simplexes ) {
27 2058 100       3961 next unless defined $accessor;
28              
29 2057         4450 _create_accessor(
30             type => 'simple',
31             accessor => $accessor,
32             %arg,
33             );
34             }
35            
36 237         2568 my $complexes = $arg{complex};
37            
38 237         743 for my $prop ( @$complexes ) {
39 513   66     1234 my $setters = $prop->{setter} || $prop->{accessor};
40            
41 513 50       3229 $setters = [ $setters ] unless 'ARRAY' eq ref $setters;
42            
43 513         694 for my $specific ( @$setters ) {
44             _create_accessor(
45             type => 'complex',
46             accessor => $specific,
47             fallback => $prop->{fallback},
48 513         1371 %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   8010 my (%arg) = @_;
67            
68 2540         2630 my $class = $arg{class};
69 2540         2020 my $overwrite = $arg{overwrite};
70 2540         4045 my $ignore = $arg{ignore};
71 2540         1995 my $type = $arg{type};
72 2540         1858 my $accessor = $arg{accessor};
73 2540         4013 my $fallback = $arg{fallback};
74              
75 2540 50       3488 return unless defined $accessor;
76              
77 2540 100       15518 if ( $class->can($accessor) ) {
78 33 100 66     2233 croak "Accessor $accessor already exists in class $class"
79             if !$overwrite && !$ignore;
80            
81 32 100 66     116 return if $ignore && !$overwrite;
82             }
83            
84 2538 100       4816 my $accessor_fn = $type eq 'complex' ? _complex($accessor, $fallback)
85             : _simplex($accessor)
86             ;
87 2538         5144 my $predicate_fn = _predicate($accessor);
88            
89 77 100   77 1 304 eval "package $class; no warnings 'redefine'; " .
  77 100   77 1 97  
  77 100   75 1 5456  
  77 100   74 1 296  
  77 100   74 1 94  
  77 100   71 1 5501  
  75 100   71 1 308  
  75 100   69 1 81  
  75 100   69 1 4983  
  74 100   69 1 471  
  74 100   61 0 97  
  74 100   189 0 4903  
  74 100   254 1 280  
  74 100   154 1 88  
  74 100   179 1 4813  
  71 100   222 1 267  
  71 100   527 1 94  
  71 50   419 1 5042  
  71 50   950 1 276  
  71 50   722 1 69  
  71 50   355 1 4867  
  69 100   113 1 351  
  69 100   164 1 89  
  69 100   369 1 4470  
  69 100   236 1 529  
  69 100   175 1 81  
  69 50   199 1 4417  
  69 50   366 1 246  
  69 100   208 1 78  
  69 100   184 1 4735  
  61 100   126 1 227  
  61 100   311 1 76  
  61 50   405 1 4132  
  2538 100   122 1 131909  
  189 50   111 1 1159  
  189 100   53 1 2320  
  31 50   47 1 113  
  31 50   384 1 44  
  188 50   63 0 3630  
  254 100   199 0 2208  
  254 100   385 0 370  
  42 100   434 0 2191  
  42 100   44 0 136  
  242 50   52 0 2385  
  154 50   34 0 5704  
  154 100   306 0 331  
  54 100   27 0 67  
  54 100   27 0 2322  
  129 100   27 0 1172  
  178 50   27 0 197  
  178 50   27 0 2479  
  42 100   27 0 134  
  42 50   27 0 58  
  165 100   27 0 3643  
  220 50   26 0 322  
  220 50   26 0 320  
  30 50   26 0 2074  
  30 50   26 0 140  
  218 50   26 0 2144  
  525 100   26 0 3642  
  525 0   0 0 823  
  191 50   0 0 302  
  191 50   0 0 2241  
  362 50   0 0 3884  
  417 50   0 0 1401  
  417 50   0 0 2587  
  192 100   0 0 347  
  192 100   0 0 374  
  253 50   0 0 5276  
  948 50   0 0 1574  
  948 50   0 0 1347  
  191 50   0 0 2375  
  191 50   0 0 426  
  785 100   0 0 4282  
  720 100   0 0 2953  
  719 100   0 0 1037  
  191 100   0 0 362  
  191 50   0 0 2157  
  555 50   0 0 3784  
  352 100   0 0 369  
  352 100   0 0 2206  
  190 100   0 0 1079  
  190 100   0 0 336  
  191 100   0 0 5457  
  110 50   0 0 197  
  110 50   0 0 738  
  28 50   0 0 1851  
  28 50   0 0 629  
  110 50   0 0 198  
  162 50   0 0 2332  
  162 50   0 0 355  
  29 50   0 0 53  
  29 100   0 0 1920  
  161 50   0 0 359  
  367 50   0 0 413  
  366 100   0 0 2225  
  195     0 0 427  
  195     0 0 331  
  198     0 0 4823  
  233     0 0 404  
  233     0 0 371  
  40     0 0 2634  
  40     0 0 143  
  223     0 0 1471  
  173     0 0 2069  
  173     0 0 348  
  57     0 0 878  
  57     0 0 1928  
  148     0 0 1720  
  198     0 0 239  
  198     0 0 2065  
  62     0 0 741  
  379     0 0 2050  
  209     0 0 2710  
  209     0 0 425  
  50     0 0 115  
  27     0 0 1872  
  207     0 0 1326  
  183     0 1 211  
  183     0 1 2029  
  27     0 1 113  
  32     0 1 37  
  188     0 1 2901  
  125     0 1 209  
  125     0 1 167  
  32     0 1 1731  
  66     139 1 162  
  164     119 1 508  
  308     136 0 2519  
  308     74 1 533  
  66     228 1 539  
  60     95 0 1725  
  341     29 1 1326  
  401     62 1 506  
  401     264 1 2276  
  150     11 1 483  
  167     0 1 267  
  361     487 1 4217  
  88     149 1 2178  
  88     63 1 149  
  81     703 1 2647  
  41     608 1 128  
  94     153 1 283  
  42     334 1 1726  
  41     588 1 133  
  44     466 1 63  
  43     308 1 1646  
  42     146 1 201  
  37     702 1 64  
  37     338 1 1696  
  35     340 1 121  
  26     341 1 30  
  37     370 1 1911  
  96     43 1 198  
  96     334 1 138  
  55     325 1 1749  
  55     341 1 161  
  67     174 1 286  
  110     379 1 1785  
  110     9 1 224  
  49     11   61  
  49     31   1644  
  61     66   379  
  26     55   68  
  26     11   55  
  14     37   24  
  14     45   27  
  12     411   289  
  20         23  
  20         47  
  0         0  
  0         0  
  20         104  
  357         366  
  357         528  
  162         213  
  162         293  
  195         3175  
  31         54  
  31         54  
  0         0  
  0         0  
  31         665  
  133         128  
  133         220  
  0         0  
  0         0  
  133         351  
  325         308  
  325         456  
  162         225  
  162         289  
  163         3085  
  357         427  
  357         531  
  165         256  
  165         307  
  192         3487  
  7         42  
  7         17  
  1         3  
  1         2  
  6         96  
  16         39  
  16         31  
  0         0  
  0         0  
  16         306  
  7         29  
  7         20  
  1         3  
  1         4  
  6         102  
  279         288  
  279         359  
  2         3  
  2         3  
  277         2718  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  139         138  
  139         199  
  0         0  
  0         0  
  139         2119  
  119         124  
  119         178  
  0         0  
  0         0  
  119         1247  
  136         141  
  136         200  
  0         0  
  0         0  
  136         1325  
  74         87  
  74         116  
  1         2  
  1         3  
  73         546  
  228         214  
  228         288  
  0         0  
  0         0  
  228         505  
  95         163  
  95         145  
  1         2  
  1         2  
  94         185  
  29         55  
  29         47  
  0         0  
  0         0  
  29         518  
  62         89  
  62         109  
  0         0  
  0         0  
  62         1182  
  264         252  
  264         434  
  0         0  
  0         0  
  264         608  
  11         32  
  11         27  
  1         1  
  1         2  
  10         50  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  487         456  
  487         642  
  0         0  
  0         0  
  487         1025  
  149         157  
  149         217  
  0         0  
  0         0  
  149         1309  
  63         61  
  63         89  
  0         0  
  0         0  
  63         110  
  703         675  
  703         960  
  0         0  
  0         0  
  703         1944  
  608         1984  
  608         869  
  0         0  
  0         0  
  608         1742  
  153         167  
  153         240  
  8         14  
  8         574  
  145         574  
  334         343  
  334         480  
  163         295  
  163         306  
  171         3126  
  588         561  
  588         843  
  0         0  
  0         0  
  588         4237  
  466         496  
  466         658  
  0         0  
  0         0  
  466         3059  
  308         307  
  308         389  
  0         0  
  0         0  
  308         1800  
  146         157  
  146         208  
  0         0  
  0         0  
  146         431  
  702         606  
  702         908  
  0         0  
  0         0  
  702         1741  
  338         365  
  338         468  
  170         264  
  170         1763  
  168         3114  
  340         366  
  340         466  
  169         214  
  169         296  
  171         3030  
  341         351  
  341         493  
  170         272  
  170         295  
  171         3057  
  370         381  
  370         508  
  163         262  
  163         290  
  207         3197  
  43         73  
  43         69  
  0         0  
  0         0  
  43         817  
  334         347  
  334         480  
  162         207  
  162         281  
  172         3083  
  325         364  
  325         490  
  162         291  
  162         382  
  163         3142  
  341         363  
  341         466  
  170         359  
  170         296  
  171         3072  
  174         181  
  174         272  
  60         67  
  60         85  
  114         549  
  379         445  
  379         500  
  162         323  
  162         274  
  217         3125  
  9         33  
  9         24  
  0         0  
  0         0  
  9         177  
  11         34  
  11         18  
  0         0  
  0         0  
  11         202  
  31         52  
  31         49  
  0         0  
  0         0  
  31         598  
  66         75  
  66         107  
  0         0  
  0         0  
  66         128  
  55         59  
  55         87  
  0         0  
  0         0  
  55         98  
  11         35  
  11         34  
  1         2  
  1         2  
  10         46  
  37         41  
  37         68  
  0         0  
  0         0  
  37         133  
  45         50  
  45         80  
  0         0  
  0         0  
  45         116  
  411         412  
  411         604  
  163         332  
  163         292  
  248         4317  
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 2544     2596   2039 my ($prop) = @_;
100            
101 2544         3596 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 2212     2067   5413 my ($prop) = @_;
121            
122 2389         5657 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 847     521   951 my ($specific, $fallback) = @_;
148            
149 520         3160 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;