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 252     252   1016 use strict;
  252         314  
  131         5203  
4 131     212   463 use warnings;
  219         1529  
  212         5176  
5 194     80   435 no warnings 'uninitialized'; ## no critic
  106         125  
  106         3917  
6              
7 168     80   923 use Carp;
  80         78  
  80         18552  
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 2507 shift if $_[0] eq __PACKAGE__;
17            
18 238         493 my (%arg) = @_;
19            
20 238   66     3778 $arg{class} ||= caller();
21            
22 238         325 my $simplexes = $arg{simple};
23            
24 238 100       518 $simplexes = [ $simplexes ] unless 'ARRAY' eq ref $simplexes;
25            
26 238         2570 for my $accessor ( @$simplexes ) {
27 2058 100       4109 next unless defined $accessor;
28              
29 2057         4381 _create_accessor(
30             type => 'simple',
31             accessor => $accessor,
32             %arg,
33             );
34             }
35            
36 237         2659 my $complexes = $arg{complex};
37            
38 237         791 for my $prop ( @$complexes ) {
39 513   66     1251 my $setters = $prop->{setter} || $prop->{accessor};
40            
41 513 50       3400 $setters = [ $setters ] unless 'ARRAY' eq ref $setters;
42            
43 513         729 for my $specific ( @$setters ) {
44             _create_accessor(
45             type => 'complex',
46             accessor => $specific,
47             fallback => $prop->{fallback},
48 513         1409 %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   8146 my (%arg) = @_;
67            
68 2540         2667 my $class = $arg{class};
69 2540         2019 my $overwrite = $arg{overwrite};
70 2540         4136 my $ignore = $arg{ignore};
71 2540         2040 my $type = $arg{type};
72 2540         1935 my $accessor = $arg{accessor};
73 2540         4123 my $fallback = $arg{fallback};
74              
75 2540 50       3487 return unless defined $accessor;
76              
77 2540 100       15842 if ( $class->can($accessor) ) {
78 33 100 66     2292 croak "Accessor $accessor already exists in class $class"
79             if !$overwrite && !$ignore;
80            
81 32 100 66     124 return if $ignore && !$overwrite;
82             }
83            
84 2538 100       5039 my $accessor_fn = $type eq 'complex' ? _complex($accessor, $fallback)
85             : _simplex($accessor)
86             ;
87 2538         5198 my $predicate_fn = _predicate($accessor);
88            
89 77 100   77 1 315 eval "package $class; no warnings 'redefine'; " .
  77 100   77 1 79  
  77 100   75 1 5703  
  77 100   74 1 296  
  77 100   74 1 89  
  77 100   71 1 5382  
  75 100   71 1 319  
  75 100   69 1 87  
  75 100   69 1 5345  
  74 100   69 1 479  
  74 100   61 0 125  
  74 100   141 0 5046  
  74 100   218 1 286  
  74 100   220 1 97  
  74 100   208 1 5025  
  71 100   205 1 285  
  71 100   538 1 79  
  71 50   419 1 4880  
  71 50   950 1 278  
  71 50   722 1 81  
  71 50   355 1 4920  
  69 100   113 1 342  
  69 100   164 1 91  
  69 100   369 1 4622  
  69 100   235 1 265  
  69 100   194 1 77  
  69 50   191 1 4809  
  69 50   356 1 261  
  69 100   172 1 76  
  69 100   207 1 4904  
  61 100   252 1 229  
  61 100   211 1 70  
  61 50   479 1 4411  
  2538 100   122 1 134543  
  141 50   111 1 1361  
  141 100   53 1 2253  
  31 50   47 1 116  
  31 50   384 1 38  
  140 50   68 0 3182  
  218 100   194 0 2704  
  218 100   397 0 324  
  38 100   422 0 2257  
  38 100   43 0 133  
  210 50   53 0 1442  
  220 50   34 0 6889  
  220 100   306 0 395  
  38 100   27 0 42  
  38 100   27 0 2122  
  211 100   27 0 2369  
  207 50   27 0 235  
  207 50   27 0 2589  
  46 100   27 0 147  
  46 50   27 0 68  
  190 100   27 0 3879  
  203 50   26 0 295  
  203 50   26 0 268  
  46 50   26 0 2041  
  46 50   26 0 148  
  185 50   26 0 1273  
  536 100   26 0 4149  
  536 0   0 0 832  
  191 50   0 0 279  
  191 50   0 0 2339  
  373 50   0 0 3783  
  417 50   0 0 2562  
  417 50   0 0 2519  
  192 100   0 0 380  
  192 100   0 0 602  
  253 50   0 0 5215  
  948 50   0 0 1876  
  948 50   0 0 1421  
  191 50   0 0 2180  
  191 50   0 0 670  
  785 100   0 0 4216  
  720 100   0 0 3020  
  719 100   0 0 1068  
  191 100   0 0 241  
  191 50   0 0 2291  
  555 50   0 0 3804  
  352 100   0 0 343  
  352 100   0 0 2386  
  190 100   0 0 1366  
  190 100   0 0 531  
  191 100   0 0 5475  
  110 50   0 0 206  
  110 50   0 0 830  
  28 50   0 0 1902  
  28 50   0 0 782  
  110 50   0 0 211  
  162 50   0 0 2369  
  162 50   0 0 350  
  29 50   0 0 35  
  29 100   0 0 2015  
  161 50   0 0 348  
  367 50   0 0 387  
  366 100   0 0 2287  
  195     0 0 342  
  195     0 0 1145  
  198     0 0 5014  
  232     0 0 334  
  232     0 0 329  
  47     0 0 2752  
  47     0 0 575  
  215     0 0 1124  
  192     0 0 2019  
  192     0 0 357  
  39     0 0 851  
  39     0 0 1971  
  185     0 0 2015  
  190     0 0 227  
  190     0 0 2050  
  69     0 0 552  
  362     0 0 1851  
  173     0 0 2931  
  173     0 0 317  
  50     0 0 128  
  27     0 0 1786  
  171     0 0 679  
  206     0 1 512  
  206     0 1 2117  
  27     0 1 105  
  37     0 1 42  
  216     0 1 3085  
  251     0 1 2602  
  251     0 1 380  
  37     0 1 1874  
  61     115 1 147  
  285     131 1 1613  
  208     145 0 1949  
  208     77 1 388  
  61     228 1 491  
  72     95 0 1804  
  253     29 1 608  
  475     62 1 573  
  475     264 1 2407  
  177     11 1 878  
  170     0 1 387  
  408     497 1 4692  
  55     139 1 2512  
  55     63 1 96  
  69     585 1 2116  
  40     603 1 133  
  60     276 1 224  
  42     334 1 1861  
  41     373 1 126  
  43     544 1 63  
  44     479 1 1737  
  43     146 1 211  
  37     708 1 64  
  37     338 1 1811  
  36     340 1 120  
  26     341 1 29  
  37     370 1 1850  
  96     43 1 194  
  96     334 1 149  
  55     325 1 1729  
  55     341 1 198  
  67     174 1 280  
  110     379 1 1742  
  110     9 1 223  
  49     11   69  
  49     31   1721  
  61     66   357  
  26     55   52  
  26     11   60  
  14     53   25  
  14     29   38  
  12     411   201  
  20         22  
  20         43  
  0         0  
  0         0  
  20         81  
  357         456  
  357         530  
  162         232  
  162         479  
  195         3121  
  31         55  
  31         52  
  0         0  
  0         0  
  31         526  
  133         131  
  133         182  
  0         0  
  0         0  
  133         354  
  325         345  
  325         453  
  162         278  
  162         536  
  163         3023  
  357         473  
  357         496  
  165         228  
  165         478  
  192         3107  
  7         31  
  7         17  
  1         2  
  1         3  
  6         92  
  16         42  
  16         31  
  0         0  
  0         0  
  16         298  
  7         29  
  7         15  
  1         1  
  1         4  
  6         100  
  279         266  
  279         364  
  2         3  
  2         4  
  277         2752  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  115         112  
  115         168  
  0         0  
  0         0  
  115         1217  
  131         133  
  131         172  
  0         0  
  0         0  
  131         1150  
  145         149  
  145         191  
  0         0  
  0         0  
  145         1624  
  77         78  
  77         117  
  1         2  
  1         2  
  76         798  
  228         213  
  228         296  
  0         0  
  0         0  
  228         524  
  95         183  
  95         164  
  1         2  
  1         3  
  94         176  
  29         55  
  29         44  
  0         0  
  0         0  
  29         502  
  62         78  
  62         102  
  0         0  
  0         0  
  62         1106  
  264         259  
  264         366  
  0         0  
  0         0  
  264         631  
  11         40  
  11         29  
  1         2  
  1         4  
  10         64  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  497         470  
  497         620  
  0         0  
  0         0  
  497         1214  
  139         129  
  139         188  
  0         0  
  0         0  
  139         1009  
  63         61  
  63         204  
  0         0  
  0         0  
  63         103  
  585         552  
  585         814  
  0         0  
  0         0  
  585         1656  
  603         2285  
  603         848  
  0         0  
  0         0  
  603         1929  
  276         303  
  276         403  
  8         17  
  8         23  
  268         807  
  334         359  
  334         470  
  163         256  
  163         530  
  171         3074  
  373         382  
  373         509  
  0         0  
  0         0  
  373         2432  
  544         548  
  544         765  
  0         0  
  0         0  
  544         3951  
  479         457  
  479         683  
  0         0  
  0         0  
  479         2965  
  146         152  
  146         214  
  0         0  
  0         0  
  146         434  
  708         619  
  708         937  
  0         0  
  0         0  
  708         1759  
  338         340  
  338         469  
  170         227  
  170         569  
  168         3208  
  340         351  
  340         476  
  169         246  
  169         1824  
  171         3051  
  341         332  
  341         493  
  170         225  
  170         460  
  171         3062  
  370         388  
  370         503  
  163         217  
  163         534  
  207         3081  
  43         66  
  43         58  
  0         0  
  0         0  
  43         740  
  334         375  
  334         483  
  162         219  
  162         464  
  172         3038  
  325         340  
  325         458  
  162         231  
  162         628  
  163         3130  
  341         409  
  341         465  
  170         282  
  170         564  
  171         3211  
  174         171  
  174         256  
  60         62  
  60         75  
  114         465  
  379         454  
  379         496  
  162         219  
  162         501  
  217         3133  
  9         36  
  9         30  
  0         0  
  0         0  
  9         191  
  11         33  
  11         19  
  0         0  
  0         0  
  11         202  
  31         50  
  31         45  
  0         0  
  0         0  
  31         581  
  66         72  
  66         104  
  0         0  
  0         0  
  66         125  
  55         49  
  55         76  
  0         0  
  0         0  
  55         93  
  11         45  
  11         41  
  1         2  
  1         4  
  10         52  
  53         48  
  53         91  
  0         0  
  0         0  
  53         150  
  29         32  
  29         51  
  0         0  
  0         0  
  29         75  
  411         410  
  411         586  
  163         217  
  163         610  
  248         4439  
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 2548     2563   2075 my ($prop) = @_;
100            
101 2548         3696 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 2200     2067   5366 my ($prop) = @_;
121            
122 2379         5980 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 837     521   943 my ($specific, $fallback) = @_;
148            
149 527         3220 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;