Attribute.pm

filter: Statementstmt 95.1% 79 / 83 Branchbran 85.0% 17 / 20 Conditioncond 59.0% 13 / 22 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 96.6% 29 / 30 Podpod 100% 23 / 23 totaltotal 86.3% 164 / 190 CC 22 SCAR 23.0
CC
22
Cov
87%
initialise
22.6
storage_type
2.1
set_on_change
2.0
CRAP 10.0
« Attribute/Method.pm ↑ Summary Class.pm »
1 package Abstract::Meta::Attribute;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use base 'Abstract::Meta::Attribute::Method';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 $VERSION = 0.04;
10  
11 =head1 NAME
12  
13 Abstract::Meta::Attribute - Meta object attribute.
14  
15 =head1 SYNOPSIS
16  
17 use Abstract::Meta::Class ':all';
18 has '$.attr1' => (default => 0);
19  
20 =head1 DESCRIPTION
21  
22 An object that describes an attribute.
23 It includes required, data type, association validation, default value, lazy retrieval.
24 Name of attribute must begin with one of the follwoing prefix:
25 $. => Scalar,
26 @. => Array,
27 %. => Hash,
28 &. => Code,
29  
30  
31 =head1 EXPORT
32  
33 None.
34  
35 =head2 METHODS
36  
37 =over
38  
39 =item new
40  
41 =cut
42  
43  
44 56 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 56 my $class = shift;
46 56 unshift @_, $class;
47 56 bless {&initialise}, $class;
48 }
49  
50  
51 =item initialise
52  
53 Initialises attribute
54  
55 =cut
56  
57 {
58 5 my %supported_type = (
59 '$' => 'Scalar',
60 '@' => 'Array',
61 '%' => 'Hash',
62 '&' => 'Code',
63 );
64  
65 56 ▶ sub initialise {
Subroutine initialise: called CC=21 SCAR=31.2
Pod documented
66 56 my ($class, %args) = @_;
67 56 foreach my $k (keys %args) {
68 242 ▶ confess "unknown attribute $k"
Branch: 1 branch
BranchTrueFalseTotal
unless "Abstract::Meta::Attribute"->can($k) 0 242 242
69 unless Abstract::Meta::Attribute->can($k);
70 }
71 56 ▶ my $name = $args{name} or confess "name is requried";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $args{"name"} or confess("name is requried") 56 0 56
72 56 ▶ my $storage_type = $args{storage_type} = $args{transistent} ? 'Hash' : $args{storage_type} || '';
Branch: 1 branch
BranchTrueFalseTotal
$args{'transistent'} ? : 3 53 56
Condition50%1/2
$args{"storage_type"} or ""
l!l
530
MC/DC0%0/1
$args{"storage_type"} or ""
$args{"storage_type"}
Truth table50%1/2
A
A= $args{"storage_type"}
Aresult
11
00
73
74 56 my $attribute_index = 0;
75 56 ▶ if($storage_type eq 'Array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage_type eq "Array") 2 54 56
76 2 my $meta_class= Abstract::Meta::Class::meta_class($args{class});
77 2 $attribute_index = $#{$meta_class->all_attributes} + 1;
78 }
79
80 56 my ($type, $accessor_name) = ($name =~ /^([\$\@\%\&])\.(.*)$/);
81 confess "invalid attribute defintion ${class}::" .($accessor_name || $name) .", supported prefixes are \$.,%.,\@.,&."
82 56 ▶ if ! $type || ! $supported_type{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $supported_type{$type} 0 56 56
Condition16%1/6
$type and $supported_type{$type}
!ll&&!rl&&r
0056
$accessor_name || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$type and $supported_type{$type}
$type $supported_type{$type}
MC/DC0%0/2
$accessor_name || $name
$accessor_name $name
Truth table33%1/3
A and B
A= $typeB= $supported_type{$type}
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $accessor_nameB= $name
ABresult
1X1
011
000
83  
84 56 my %options;
85 $args{data_type_validation} = 1
86 if (! exists($args{data_type_validation})
87 56 ▶ && ($type eq '@' || $type eq '%' || $args{associated_class}));
Branch: 1 branch
BranchTrueFalseTotal
if not exists $args{"data_type_validation"} and $type eq "\@" || $type eq "%" || $args{"associated_class"} 30 26 56
Condition88%8/9
not exists $args{"data_type_validation"} and $type eq "\@" || $type eq "%" || $args{"associated_class"}
!ll&&!rl&&r
02630
$type eq "\@" or $type eq "%" or $args{"associated_class"}
l!l&&r!l&&!r
22826
$type eq "\@" or $type eq "%"
l!l&&r!l&&!r
111134
MC/DC75%3/4
not exists $args{"data_type_validation"} and $type eq "\@" || $type eq "%" || $args{"associated_class"}
not exists $args{"data_type_validation"} $type eq "\@" $type eq "%" $args{"associated_class"}
Truth table80%4/5
not(A) and B or C or D
A= not exists $args{"data_type_validation"}B= $type eq "\@"C= $type eq "%"D= $args{"associated_class"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
88  
89 $options{'&.' . $_ } = $args{$_}
90 56 for grep {exists $args{$_}} (qw(on_read on_change on_validate));
91
92
93 56 ▶ my $storage_key = $storage_type eq 'Array' ? $attribute_index : $args{storage_key} || $args{name};
Branch: 1 branch
BranchTrueFalseTotal
$storage_type eq 'Array' ? : 2 54 56
Condition66%2/3
$args{"storage_key"} or $args{"name"}
l!l&&r!l&&!r
2520
MC/DC0%0/2
$args{"storage_key"} or $args{"name"}
$args{"storage_key"} $args{"name"}
Truth table66%2/3
A or B
A= $args{"storage_key"}B= $args{"name"}
ABresult
1X1
011
000
94  
95 56 $options{'$.name'} = $accessor_name;
96 56 $options{'$.storage_key'} = $storage_key;
97 56 $options{'$.mutator'} = "set_$accessor_name";
98 56 $options{'$.accessor'} = $accessor_name;
99 $options{'$.' . $_ } = $args{$_}
100 56 for grep {exists $args{$_}}
101 (qw(class required default item_accessor associated_class data_type_validation index_by the_other_end transistent storage_type));
102
103 56 $options{'$.perl_type'} = $supported_type{$type};
104 56 ▶ unless ($args{default}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"default"}) 48 8 56
105 48 ▶ if($type eq '%') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '%') { } 9 39 48
elsif ($type eq '@') { } 9 30 39
106 9 ▶ $options{'$.default'} = sub{ {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
107 } elsif ($type eq '@') {
108 9 ▶ $options{'$.default'} = sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
109 }
110 }
111 56 %options;
112 }
113 }
114  
115  
116 =item name
117  
118 Returns attribute name
119  
120 =cut
121  
122 87 ▶sub name { shift()->{'$.name'} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
123  
124  
125 =item class
126  
127 Attribute's class name.
128  
129 =cut
130  
131 2 ▶sub class { shift()->{'$.class'} }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
132  
133  
134 =item storage_key
135  
136 Returns storage attribute key in object
137  
138 =cut
139  
140 252 ▶sub storage_key { shift()->{'$.storage_key'} }
Subroutine storage_key: called CC=1 SCAR=0.0
Pod documented
141  
142  
143  
144 =item perl_type
145  
146 Returns attribute type, Scalar, Hash, Array, Code
147  
148 =cut
149  
150 359 ▶sub perl_type { shift()->{'$.perl_type'} }
Subroutine perl_type: called CC=1 SCAR=0.0
Pod documented
151  
152  
153 =item accessor
154  
155 Returns accessor name
156  
157 =cut
158  
159 332 ▶sub accessor { shift()->{'$.accessor'} }
Subroutine accessor: called CC=1 SCAR=0.0
Pod documented
160  
161  
162 =item mutator
163  
164 Returns mutator name
165  
166 =cut
167  
168 158 ▶sub mutator { shift()->{'$.mutator'} }
Subroutine mutator: called CC=1 SCAR=0.0
Pod documented
169  
170  
171 =item required
172  
173 Returns required flag
174  
175 =cut
176  
177 165 ▶sub required { shift()->{'$.required'} }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
178  
179  
180 =item default
181  
182 Returns default value
183  
184 =cut
185  
186 130 ▶sub default { shift()->{'$.default'} }
Subroutine default: called CC=1 SCAR=0.0
Pod documented
187  
188  
189 =item storage_type
190  
191 Hash|Array
192  
193 =cut
194  
195 295 ▶sub storage_type { shift()->{'$.storage_type'} ||= 'Hash' }
Subroutine storage_type: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"\$.storage_type"} ||= "Hash"
l!l
2950
MC/DC0%0/1
+(shift())->{"\$.storage_type"} ||= "Hash"
+(shift())->{"\$.storage_type"}
Truth table50%1/2
A
A= +(shift())->{"\$.storage_type"}
Aresult
11
00
196  
197  
198 =item transistent
199  
200 If this flag is set, than storage of that attribte, will be force outside the object,
201 so you cant serialize that attribute,
202 It is especially useful when using callback, that cant be serialised (Storable dclone)
203 This option will generate cleanup and DESTORY methods.
204  
205 =cut
206  
207 374 ▶sub transistent { shift()->{'$.transistent'} }
Subroutine transistent: called CC=1 SCAR=0.0
Pod documented
208  
209  
210 =item item_accessor
211  
212 Returns name that will be used to construct the hash or array item accessor.
213 It will be used to retrieve or set array or hash item item
214  
215  
216 has '%.items' => (item_accessor => 'item');
217 ...
218 my $item_ref = $obj->items;
219 $obj->item(x => 3);
220 my $value = $obj->item('y')'
221  
222  
223 =cut
224  
225 65 ▶sub item_accessor { shift()->{'$.item_accessor'} }
Subroutine item_accessor: called CC=1 SCAR=0.0
Pod documented
226  
227  
228  
229 =item associated_class
230  
231 Return name of the associated class.
232  
233 =cut
234  
235 245 ▶sub associated_class { shift()->{'$.associated_class'} }
Subroutine associated_class: called CC=1 SCAR=0.0
Pod documented
236  
237  
238 =item index_by
239  
240 Name of the asscessor theat will return unique attribute for associated objects.
241 Only for toMany associaion, by deault uses objecy reference as index.
242  
243 package Class;
244 use Abstract::Meta::Class ':all';
245 has '$.name' => (required => 1);
246 has '%.details' => (
247 index_by => 'id',
248 item_accessor => 'detail',
249 );
250 my $obj = Class->
251  
252  
253  
254  
255 =cut
256  
257 133 ▶sub index_by { shift()->{'$.index_by'} }
Subroutine index_by: called CC=1 SCAR=0.0
Pod documented
258  
259  
260 =item the_other_end
261  
262 Name of the asscessor/mutator on associated class to keep bideriectional association
263 This option will generate cleanup method.
264  
265 =cut
266  
267 87 ▶sub the_other_end { shift()->{'$.the_other_end'} }
Subroutine the_other_end: called CC=1 SCAR=0.0
Pod documented
268  
269  
270 =item data_type_validation
271  
272 Flag that turn on/off data type validation.
273 Data type validation happens when using association_class or Array or Hash data type
274 unless you explicitly disable it by seting data_type_validation => 0.
275  
276 =cut
277  
278 58 ▶sub data_type_validation { shift()->{'$.data_type_validation'} }
Subroutine data_type_validation: called CC=1 SCAR=0.0
Pod documented
279  
280  
281 =item on_read
282  
283 Returns code reference that will be replace data read routine
284  
285 has '%.attrs.' => (
286 item_accessor => 'attr'
287 on_read => sub {
288 my ($self, $attribute, $scope, $key) = @_;
289 my $values = $attribute->get_values($self);
290 if ($scope eq 'accessor') {
291 return $values;
292 } else {
293 return $values->{$key};
294 }
295 },
296 );
297 has '@.array_attrs.' => (
298 item_accessor => 'array_item'
299 on_read => sub {
300 my ($self, $attribute, $scope, $index) = @_;
301 my $values = $attribute->get_values($self);
302 if ($scope eq 'accessor') {
303 return $values;
304 } else {
305 return $values->[$index];
306 }
307 },
308 );
309  
310 =cut
311  
312 76 ▶sub on_read { shift()->{'&.on_read'} }
Subroutine on_read: called CC=1 SCAR=0.0
Pod documented
313  
314  
315 =item set_on_read
316  
317 Sets code reference that will be replace data read routine
318  
319 my $attr = MyClass->meta->attribute('attrs');
320 $attr->set_on_read(sub {
321 my ($self, $attribute, $scope, $key) = @_;
322 #do some stuff
323 });
324  
325 =cut
326  
327 1 ▶sub set_on_read {
Subroutine set_on_read: called CC=1 SCAR=0.0
Pod documented
328 1 my ($attr, $value) = @_;
329 1 $attr->{'&.on_read'} = $value;
330 1 my $meta= $attr->class->meta;
331 1 $meta->install_attribute_methods($attr, 1);
332 }
333  
334  
335 =item on_change
336  
337 Code reference that will be executed when data is set,
338 Takes reference to the variable to be set.
339  
340 =cut
341  
342 72 ▶sub on_change { shift()->{'&.on_change'} }
Subroutine on_change: called CC=1 SCAR=0.0
Pod documented
343  
344  
345  
346 =item set_on_change
347  
348 Sets code reference that will be executed when data is set,
349  
350 my $attr = MyClass->meta->attribute('attrs');
351 $attr->set_on_change(sub {
352 my ($self, $attribute, $scope, $value, $key) = @_;
353 if($scope eq 'mutator') {
354 my $hash = $$value;
355 foreach my $k (keys %$hash) {
356 # do some stuff
357 #$self->validate_trigger($k, $hash->{$k});
358 }
359 } else {
360 # do some stuff
361 $self->validate_trigger($key. $$value);
362 }
363 $self;
364 });
365  
366 =cut
367  
368 0 ▶sub set_on_change {
Subroutine set_on_change: not called CC=1 SCAR=6.9
Pod documented
369 0 my ($attr, $value) = @_;
370 0 $attr->{'&.on_change'} = $value;
371 0 my $meta= $attr->class->meta;
372 0 $meta->install_attribute_methods($attr, 1);
373 }
374  
375  
376  
377  
378  
379 =item on_validate
380  
381 Returns on validate code reference.
382 It is executed before the data type validation happens.
383  
384 =cut
385  
386 58 ▶sub on_validate { shift()->{'&.on_validate'} }
Subroutine on_validate: called CC=1 SCAR=0.0
Pod documented
387  
388  
389 =item set_on_validate
390  
391 Sets code reference that will be replace data read routine
392  
393 my $attr = MyClass->meta->attribute('attrs');
394 $attr->set_on_read(sub {
395 my ($self, $attribute, $scope, $key) = @_;
396 #do some stuff
397 });
398  
399 =cut
400  
401 1 ▶sub set_on_validate {
Subroutine set_on_validate: called CC=1 SCAR=0.0
Pod documented
402 1 my ($attr, $value) = @_;
403 1 $attr->{'&.on_validate'} = $value;
404 1 my $meta= $attr->class->meta;
405 1 $meta->install_attribute_methods($attr, 1);
406 }
407  
408  
409  
410  
411 5 1;
412  
413 __END__
« Attribute/Method.pm ↑ Summary Class.pm »