blib/lib/UR/Object/Accessorized.pm untested

filter: Statementstmt 0.0% 0 / 178 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 271 SCAR 61.4
CC
21
Cov
0%
__get_array
20.0
explicit_accessorize
12.0
array_accessorize
12.0
CRAP 462.0
1 package UR::Object::Accessorized;
2  
3 use strict;
4 use warnings;
5  
6 use UR;
7 our $VERSION = "0.46"; # UR $VERSION;
8  
9 UR::Object::Type->define(
10 class_name => __PACKAGE__,
11 is => ['UR::Object'],
12 );
13  
14 #--- just because I'm tired of GSCApp and Class::Accessor not playing nice, here we go
15  
16 sub delegate{
17 my $class = shift;
18 my %p = @_;
19 foreach my $get_object_method(keys %p){
20 foreach my $delegated_function(@{$p{$get_object_method}}){
21 my $class_function = $class.'::'.$delegated_function;
22 no strict;
23 *$class_function = sub{
24 my $self = shift;
25 my $obj = $self->$get_object_method; #--- get the object of delgation
26 unless($obj){
27 $self->error_message("Failed to call $function on $self");
28 return;
29 }
30 return $obj->$delegated_function(@_);
31 };
32 }
33 }
34 1;
35 }
36  
37 sub ro_delegate{
38 my $class = shift;
39 my %p = @_;
40 foreach my $function (keys %p){
41 foreach my $delegator_func(@_){
42 my $class_function = $class.'::'.$delegator_func;
43 no strict;
44 *$class_function = sub{
45 my $self = shift;
46 my $obj = $self->$function(); #--- get the object of delgation
47 unless($obj){
48 $self->error_message("Failed to call $function on $self");
49 return;
50 }
51 return $obj->$delegator_func();
52 };
53 }
54 }
55 1;
56 }
57  
58 sub accessorize{
59 my $class = shift;
60 foreach my $accessor_func(@_){
61 my $setfunc = $class.'::'.$accessor_func;
62 no strict;
63 *$setfunc = sub{
64 my $self = shift;
65 if(@_){
66 return $self->__set($accessor_func, @_);
67 }
68 return $self->__get($accessor_func);
69 };
70 }
71 }
72  
73 sub explicit_accessorize{
74 my $class = shift;
75 foreach my $accessor_func(@_){
76 my $write_func = $class.'::set_'.$accessor_func;
77 my $read_func = $class.'::get_'.$accessor_func;
78 no strict;
79 *$write_func = sub{
80 my $self = shift;
81 return unless @_;
82 return $self->__set($accessor_func, @_);
83 };
84 *$read_func = sub{
85 my $self = shift;
86 return unless @_;
87 return $self->__get($accessor_func, @_);
88 };
89 }
90 }
91  
92 sub ro_accessorize{
93 my $class = shift;
94 foreach my $accessor_func(@_){
95 my $setfunc = $class.'::get_'.$accessor_func;
96 no strict;
97 *$setfunc = sub{
98 my $self = shift;
99 if(@_){
100 die "cannot set values for read only accessor $accessor_func";
101 }
102 return $self->__get($accessor_func);
103 };
104 }
105 }
106  
107 sub ro_array_accessorize{
108 my $class = shift;
109 foreach my $accessor_func(@_){
110 no strict;
111 #--- get
112 my $getf = $class.'::'.$accessor_func;
113 *$getf = sub{
114 my $self = shift;
115 return $self->__get_array($accessor_func);
116 };
117 }
118 }
119  
120 sub array_accessorize{
121 my $class = shift;
122 foreach my $accessor_func(@_){
123 no strict;
124 #--- get
125 my $getf = $class.'::get_'.$accessor_func;
126 *$getf = sub{
127 my $self = shift;
128 return $self->__get_array($accessor_func);
129 };
130 #--- set
131 my $setf = $class.'::set_'.$accessor_func;
132 *$setf = sub{
133 my $self = shift;
134 return $self->__set_array($accessor_func, @_);
135 };
136 #--- add
137 my $addf = $class.'::add_'.$accessor_func;
138 *$addf = sub{
139 my $self = shift;
140 return $self->__add_array($accessor_func, @_);
141 };
142 #--- remove
143 my $removef = $class.'::remove_'.$accessor_func;
144 *$removef = sub{
145 my $self = shift;
146 return $self->__remove_array($accessor_func, @_);
147 };
148 #--- clear
149 my $clearf = $class.'::clear_'.$accessor_func;
150 *$clearf = sub{
151 my $self = shift;
152 return $self->__clear_array($accessor_func);
153 };
154 #--- default
155 unless($class->can($accessor_func)){
156 my $defaultf = $class.'::'.$accessor_func;
157 *$defaultf = sub{
158 my $self = shift;
159 if(@_){
160 #--- with parameters, it is 'set'
161 return $self->__set_array($accessor_func, @_);
162 }
163 else{
164 return $self->__get_array($accessor_func, @_);
165 }
166 };
167 }
168 }
169 }
170  
171  
172 sub __get{
173 my $self = shift;
174 my $func = shift;
175 return unless ref $self;
176 return $self->{$func};
177 }
178  
179 sub __set{
180 my $self = shift;
181 my $func = shift;
182 return unless @_;
183 return unless ref $self;
184 $self->{$func} = shift;
185 return 1;
186 }
187  
188 sub __get_array{
189 my $self = shift;
190 my $func = shift;
191 return unless exists $self->{$func} && ref $self->{$func} eq 'ARRAY';
192 if(@_){
193 if(@_ == 1){
194 return $self->{$func}->[shift];
195 }
196 else{
197 return @{$self->{$func}}[@_];
198 }
199 }
200 return @{$self->{$func}};
201 }
202  
203 sub __set_array{
204 my $self = shift;
205 my $func = shift;
206 return unless @_;
207 $self->{$func} = [@_];
208 1;
209 }
210  
211 sub __add_array{
212 my $self = shift;
213 my $func = shift;
214 unless(exists $self->{$func}){
215 $self->__set_array($func => @_);
216 return 1;
217 }
218 return unless ref $self->{$func} eq 'ARRAY';
219 push @{$self->{$func}}, @_;
220 1;
221 }
222  
223 sub __remove_from_array{
224 my $self = shift;
225 my $func = shift;
226 return unless exists $self->{$func} && ref $self->{$func} eq 'ARRAY' && @{$self->{$func}};
227 my $count = 0;
228 my %bad = map {$_ => 1} @_;
229 my $i=0;
230 while($i < scalar(@{$self->{$func}})){
231 if($bad{$self->{$func}[$i]}){
232 splice @{$self->{$func}}, $i, 1;
233 ++$count;
234 next;
235 }
236 ++$i;
237 }
238 return $count;
239 }
240  
241 sub __clear_array{
242 my $self = shift;
243 my $func = shift;
244 return unless exists $self->{$func} && ref $self->{$func} eq 'ARRAY' && @{$self->{$func}};
245 $self->{$func} = [];
246 return;
247 }
248  
249  
250 1;