Gomor/Array.pm

filter: Statementstmt 90.3% 56 / 62 Branchbran 90.0% 9 / 10 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 73.3% 88 / 120 CC 15 SCAR 12.5
CC
15
Cov
79%
cgFullClone
6.4
cgDumper
6.0
new
4.0
CRAP 3.5
« Gomor.pm ↑ Summary Gomor/Hash.pm »
1 #
2 # $Id: Array.pm 2000 2015-01-13 18:24:09Z gomor $
3 #
4 package Class::Gomor::Array;
5 7 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '1.03';
8  
9 7 ▶use Class::Gomor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use base qw(Class::Gomor);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
17 4 my $self = shift;
18 4 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
19 4 my %h = @_;
20 4 ▶ $class->cgCheckParams(\%h, $class->cgGetAttributes)
Branch: 1 branch
BranchTrueFalseTotal
unless $Class::Gomor::NoCheck 3 1 4
21 unless $Class::Gomor::NoCheck;
22 4 my @obj;
23 4 my $base = $class.'::__';
24 4 $obj[${$base.$_}] = $h{$_} for keys %h;
25 4 bless(\@obj, $class);
26 }
27  
28 8 ▶sub cgGetIndice {
Subroutine cgGetIndice: called CC=2 SCAR=7.6
Pod documented
29 8 my $self = shift;
30 8 ▶ ${(ref($self) || $self).'::__'.shift()};
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
31 }
32  
33 4 ▶sub cgBuildIndices {
Subroutine cgBuildIndices: called CC=3 SCAR=11.4
Pod documented
34 4 my $self = shift;
35 4 my $i = 0;
36 4 ▶ ${(ref($self) || $self).'::__'.$_} = $i++ for @{$self->cgGetAttributes};
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
37 }
38  
39 11 ▶sub cgFullClone {
Subroutine cgFullClone: called CC=6 SCAR=18.6
Pod documented
40 11 my $self = shift;
41 11 my ($n) = @_;
42 11 ▶ return $self->SUPER::cgFullClone($n) if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 10 11
43 10 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
44 10 my @new;
45 10 for (@$self) {
46 30 ▶ (ref($_) && UNIVERSAL::isa($_, 'Class::Gomor'))
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && UNIVERSAL::isa($_, 'Class::Gomor') ? : 0 30 30
Condition66%2/3
ref $_ and UNIVERSAL::isa($_, "Class::Gomor")
!ll&&!rl&&r
20100
MC/DC0%0/2
ref $_ and UNIVERSAL::isa($_, "Class::Gomor")
ref $_ UNIVERSAL::isa($_, "Class::Gomor")
Truth table66%2/3
A and B
A= ref $_B= UNIVERSAL::isa($_, "Class::Gomor")
ABresult
0X0
100
111
47 ? push @new, $_->cgFullClone
48 : push @new, $_;
49 }
50 10 bless(\@new, $class);
51 }
52  
53 0 ▶sub cgDumper {
Subroutine cgDumper: not called CC=2 SCAR=17.9
Pod documented
54 0 my $self = shift;
55 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
56 0 my %h = map { $_ => $self->[$self->cgGetIndice($_)] }
57 0 @{$class->cgGetAttributes};
58 0 Dumper(\%h);
59 }
60  
61 15 ▶sub _cgAccessorScalar {
Subroutine _cgAccessorScalar: called CC=2 SCAR=6.9
62 15 my $self = shift;
63 15 my $a = shift;
64 7 @_ ? $self->[${ref($self).'::__'.$a}] = shift
65 15 ▶ : $self->[${ref($self).'::__'.$a}];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 7 8 15
66 }
67  
68 6 ▶sub _cgAccessorArray {
Subroutine _cgAccessorArray: called CC=2 SCAR=6.9
69 6 my $self = shift;
70 6 my $a = shift;
71 2 @_ ? $self->[${ref($self).'::__'.$a}] = shift
72 6 ▶ : @{$self->[${ref($self).'::__'.$a}]};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 4 6
73 }
74  
75 7 1;
76  
77 =head1 NAME
78  
79 Class::Gomor::Array - class and object builder, array version
80  
81 =head1 SYNPOSIS
82  
83 # Create a base class in BaseClass.pm
84 package My::BaseClass;
85  
86 require Class::Gomor::Array;
87 our @ISA = qw(Class::Gomor::Array);
88  
89 our @AS = qw(attribute1 attribute2);
90 our @AA = qw(attribute3 attribute4);
91 our @AO = qw(other);
92  
93 # You should initialize yourself array attributes
94 sub new { shift->SUPER::new(attribute3 => [], attribute4 => [], @_) }
95  
96 # Create indices and accessors
97 My::BaseClass->cgBuildIndices;
98 My::BaseClass->cgBuildAccessorsScalar(\@AS);
99 My::BaseClass->cgBuildAccessorsArray(\@AA);
100  
101 sub other {
102 my $self = shift;
103 @_ ? $self->[$self->cgGetIndice('other')] = [ split(/\n/, shift) ]
104 : @{$self->[$self->cgGetIndice('other')]};
105 }
106  
107 1;
108  
109 # Create a subclass in SubClass.pm
110 package My::SubClass;
111  
112 require My::BaseClass;
113 our @ISA = qw(My::BaseClass);
114  
115 our @AS = qw(subclassAttribute);
116  
117 My::SubClass->cgBuildIndices;
118 My::SubClass->cgBuildAccessorsScalar(\@AS);
119  
120 sub new {
121 shift->SUPER::new(
122 attribute1 => 'val1',
123 attribute2 => 'val2',
124 attribute3 => [ 'val3', ],
125 attribute4 => [ 'val4', ],
126 other => [ 'none', ],
127 subclassAttribute => 'subVal',
128 );
129 }
130  
131 1;
132  
133 # A program using those classes
134  
135 my $new = My::SubClass->new;
136  
137 my $val1 = $new->attribute1;
138 my @values3 = $new->attribute3;
139 my @otherOld = $new->other;
140  
141 $new->other("str1\nstr2\nstr3");
142 my @otherNew = $new->other;
143 print "@otherNew\n";
144  
145 $new->attribute2('newValue');
146 $new->attribute4([ 'newVal1', 'newVal2', ]);
147  
148 =head1 DESCRIPTION
149  
150 This class is a subclass from B<Class::Gomor>. It implements objects as array references, and inherits methods from B<Class::Gomor>.
151  
152 =head1 GLOBAL VARIABLES
153  
154 See B<Class::Gomor>.
155  
156 =head1 METHODS
157  
158 =over 4
159  
160 =item B<new> (hash)
161  
162 Object constructor. This is where user passed attributes (hash argument) are checked against valid attributes (gathered by B<cgGetAttributes> method). Valid attributes are those that exists (doh!), and have not an undef value. The default is to check this, you can avoid it by setting B<$NoCheck> global variable (see perldoc B<Class::Gomor>).
163  
164 =item B<cgBuildIndices>
165  
166 You MUST call this method one time at the beginning of your classes, and all subclasses (even if you do not add new attributes). It will build the matching between object attributes and their indices inside the array object. Global variables will be created in your class, with the following format: B<$__attributeName>.
167  
168 =item B<cgBuildAccessorsScalar> (array ref)
169  
170 =item B<cgBuildAccessorsArray> (array ref)
171  
172 See B<Class::Gomor>.
173  
174 =item B<cgGetIndice> (scalar)
175  
176 Returns the array indice of specified attribute passed as a parameter. You can use it in your programs to avoid calling directly the global variable giving indice information concerning requesting object, thus avoiding using `no strict 'vars';'. This method is usually used when you build your own accessors (those using attributes defined in B<@AO>).
177  
178 =item B<cgClone> [ (scalar) ]
179  
180 You can clone one of your objects by calling this method. An optional parameter may be used to create multiple clones. Cloning will occure only on the first level attributes, that is, if you have attributes containing other objects, they will not be cloned.
181  
182 =item B<cgFullClone> [ (scalar) ]
183  
184 This method is the same as B<cgClone>, but will clone all attributes recursively, but only if they are subclassed from B<Class::Gomor>. So, objects created with other modules than B<Class::Gomor::Array> or B<Class::Gomor::Hash> will not be cloned.
185  
186 Another thing to note, there is no catch for cycling references (when you link two objects with each others). You have been warned.
187  
188 =item B<cgDumper>
189  
190 Will return a string as with B<Data::Dumper> Dumper method. This is useful for debugging purposes, because an arrayref object does not include attributes names.
191  
192 =back
193  
194 =head1 SEE ALSO
195  
196 L<Class::Gomor>
197  
198 =head1 AUTHOR
199
200 Patrice E<lt>GomoRE<gt> Auffret
201
202 =head1 COPYRIGHT AND LICENSE
203
204 Copyright (c) 2004-2015, Patrice E<lt>GomoRE<gt> Auffret
205  
206 You may distribute this module under the terms of the Artistic license.
207 See LICENSE.Artistic file in the source distribution archive.
208  
209 =cut
« Gomor.pm ↑ Summary Gomor/Hash.pm »