File Coverage

blib/lib/Autodia/Diagram/Class.pm
Criterion Covered Total %
statement 9 177 5.0
branch 0 42 0.0
condition 0 5 0.0
subroutine 3 24 12.5
pod 0 19 0.0
total 12 267 4.4


line stmt bran cond sub pod time code
1             package Autodia::Diagram::Class;
2 1     1   6 use strict;
  1         3  
  1         43  
3              
4             =head1 NAME
5              
6             Autodia::Diagram::Class - Class that holds, updates and outputs the values of a diagram element of type class.
7              
8             =head1 SYNOPSIS
9              
10             use Autodia::Diagram::Class;
11              
12             my $Class = Autodia::Diagram::Class->new;
13              
14             =head2 Description
15              
16             Autodia::Diagram::Class is an object that represents the Dia UML Class element within a Dia diagram. It holds, outputs and allows the addition of attributes, relationships and methods.
17              
18             =cut
19              
20 1     1   6 use Data::Dumper;
  1         2  
  1         138  
21              
22 1     1   7 use base qw(Autodia::Diagram::Object);
  1         2  
  1         688  
23              
24             =head1 METHODS
25              
26             =head2 Constructor
27              
28             my $Class = Autodia::Diagram::Class->new($name);
29              
30             creates and returns a simple Autodia::Diagram::Class object, containing its name and its original position (default 0,0).
31              
32             =head2 Accessors
33              
34             Autodia::Diagram::Class attributes are accessed through methods, rather than directly. Each attribute is available through calling the method of its name, ie Inheritances(). The methods available are :
35              
36             Operations, Attributes, Inheritances, Dependancies, Parent, and has_child. The first 4 return a list, the later return a string.
37              
38             Adding elements to the Autodia::Diagram::Class is acheived through the add_ methods, ie add_inheritance().
39              
40             Rather than remove an element from the diagram it is marked as redundant and replaced with a superceding element, as Autodia::Diagram::Class has highest precedence it won't be superceded and so doesn't have a redundant() method. Superclass and Component do.
41              
42             =head2 Accessing and manipulating the Autodia::Diagram::Class
43              
44             $Class->Attributes(), Inheritances(), Operations(), and Dependancies() all return a list of their respective elements.
45              
46             $Class->Parent(), and has_child() return the value of the parent or child respectively if present otherwise a false.
47              
48             $Class->add_attribute(), add_inheritance(), add_operation(), and add_dependancy() all add a new element of their respective types.
49              
50             =cut
51              
52             #####################
53             # Constructor Methods
54              
55             sub new {
56 0     0 0   my $class = shift;
57 0           my $name = shift;
58 0           my $self = {};
59 0   0       bless ($self, ref($class) || $class);
60 0           $self->_initialise($name);
61 0           return $self;
62             }
63              
64             #-------------------------------------------------------------------------
65              
66             ################
67             # Access Methods
68              
69             sub Dependancies {
70 0     0 0   my $self = shift;
71 0 0         if (defined $self->{"dependancies"}) {
72 0           my @dependancies = @{$self->{"dependancies"}};
  0            
73 0           return @dependancies;
74             } else {
75 0           return;
76             }
77             }
78              
79              
80             sub add_dependancy {
81 0     0 0   my $self = shift;
82 0           my $new_dependancy = shift;
83 0           my @dependancies;
84              
85 0 0         if (defined $self->{"dependancies"}) {
86 0           @dependancies = @{$self->{"dependancies"}};
  0            
87             }
88              
89 0           push(@dependancies, $new_dependancy);
90 0           $self->{"dependancies"} = \@dependancies;
91              
92 0           return scalar(@dependancies);
93             }
94              
95             sub Inheritances {
96 0     0 0   my $self = shift;
97 0 0         if (ref $self->{"inheritances"}) {
98 0           return $self->{"inheritances"};
99             } else {
100 0           return undef;
101             }
102             }
103              
104             sub add_inheritance {
105 0     0 0   my $self = shift;
106 0           my $new_inheritance = shift;
107 0           my @inheritances;
108              
109 0 0         if (defined $self->{"inheritances"}) {
110 0           @inheritances = @{$self->{"inheritances"}};
  0            
111             }
112              
113 0           push(@inheritances, $new_inheritance);
114 0           $self->{"inheritances"} = \@inheritances;
115 0           $self->Parent($new_inheritance->Id);
116              
117 0           return scalar(@inheritances);
118             }
119              
120              
121             sub Relations {
122 0     0 0   my $self = shift;
123 0 0         return (ref $self->{"relations"}) ? @{$self->{"relations"}} : () ;
  0            
124             }
125              
126             sub add_relation {
127 0     0 0   my $self = shift;
128 0           my $new_relation = shift;
129 0   0       $self->{relations} ||= [];
130 0           push(@{$self->{relations}}, $new_relation);
  0            
131 0           return 1;
132             }
133              
134              
135             sub Attributes {
136 0     0 0   my $self = shift;
137              
138 0 0         if (defined $self->{"attributes"}) {
139 0           my @attributes = @{$self->{"attributes"}};
  0            
140 0           return \@attributes;
141             } else {
142 0           return;
143             }
144             }
145              
146             sub add_attribute {
147 0     0 0   my $self = shift;
148 0           my %new_attribute = %{shift()};
  0            
149              
150             # discard new attribute if duplicate
151 0           my $discard = 0;
152 0           foreach my $attribute ( @{$self->{"attributes"}} ) {
  0            
153 0           my %attribute = %$attribute;
154 0 0         if ($attribute{name} eq $new_attribute{name}) {
155 0           $discard = 1;
156             }
157             }
158              
159 0 0         unless ($discard) {
160 0           push (@{$self->{"attributes"}},\%new_attribute);
  0            
161 0           $self->_set_updated("attributes");
162 0           $self->_update;
163             }
164              
165 0           return scalar(@{$self->{"attributes"}});
  0            
166             }
167              
168             sub has_child {
169 0     0 0   my $self = shift;
170 0           my $child = shift;
171 0           my $return = 0;
172              
173 0 0         if (defined $child) {
174 0           $self->{"child"} = $child;
175             } else {
176 0           $return = $self->{"child"};
177             }
178             }
179              
180             sub Parent {
181 0     0 0   my $self = shift;
182 0           my $parent = shift;
183 0           my $return = 0;
184              
185 0 0         if (defined $parent) {
186 0           $self->{"parent"} = $parent;
187             } else {
188 0           $return = $self->{"parent"};
189             }
190             }
191              
192             sub replace_superclass {
193 0     0 0   my $self = shift;
194 0           my $superclass = shift;
195              
196 0 0         if (ref ($superclass->Inheritances)) {
197 0           my @inheritances = @{$superclass->Inheritances};
  0            
198 0           foreach my $inheritance (@inheritances) {
199 0           $inheritance->Parent($self->Id);
200             }
201             }
202              
203 0 0         if (ref ($superclass->Relations)) {
204 0           my @relations = @{$superclass->Relations};
  0            
205 0           foreach my $relation (@relations) {
206 0           $relation->Parent($self->Id);
207             }
208             }
209              
210 0           return 1;
211             }
212              
213             sub replace_component {
214 0     0 0   my $self = shift;
215 0           my $component = shift;
216              
217 0 0         if (ref ($component->Dependancies) ) {
218 0           my @dependancies = $component->Dependancies;
219 0           foreach my $dependancy (@dependancies) {
220 0           $dependancy->Parent($self->Id);
221             }
222             }
223              
224 0           return 1;
225             }
226              
227             sub Operations {
228 0     0 0   my $self = shift;
229              
230 0 0         if (defined $self->{"operations"}) {
231 0           my @operations = $self->{"operations"};
232 0           return @operations;
233             } else {
234 0           return;
235             }
236             }
237              
238             sub add_operation {
239 0     0 0   my $self = shift;
240 0           my $operation = shift();
241 0 0         $operation->{_id} = ( ref $self->{"operations"} ) ? scalar @{$self->{"operations"}} : 0 ;
  0            
242 0           push (@{$self->{"operations"}},$operation);
  0            
243 0           $self->{operation_index}{$operation->{name}} = $operation;
244              
245 0           $self->_set_updated("operations");
246 0           $self->_update;
247              
248 0           return scalar(@{$self->{"operations"}});
  0            
249             }
250              
251             sub get_operation {
252 0     0 0   my ($self, $name) = @_;
253 0           return $self->{operation_index}{$name};
254             }
255              
256             sub update_operation {
257 0     0 0   my $self = shift;
258 0           my $operation = shift;
259            
260 0           $self->{"operations"}[$operation->{_id}] = $operation;
261 0           $self->{operation_index}{$operation->{name}} = $operation;
262              
263 0           $self->_set_updated("operations");
264 0           $self->_update;
265              
266 0           return;
267             }
268              
269             sub Realizations {
270 0     0 0   my $self = shift;
271 0 0         if( defined $self->{"realizations"} ) {
272 0           my @realizations = @{ $self->{"realizations"} };
  0            
273 0           return @realizations;
274             }
275             else {
276 0           return;
277             }
278             }
279            
280             sub add_realization {
281 0     0 0   my $self = shift;
282 0           my $new_realization = shift;
283 0           my @realizations;
284            
285 0 0         if( defined $self->{"realizations"} ) {
286 0           @realizations = @{ $self->{"realizations"} };
  0            
287             }
288            
289 0           push( @realizations, $new_realization );
290 0           $self->{"realizations"} = \@realizations;
291            
292 0           return scalar(@realizations);
293             }
294              
295              
296             #-----------------------------------------------------------------------
297              
298             ##################
299             # Internal Methods
300              
301             # over-rides method in DiagramObject
302             sub _initialise {
303 0     0     my $self = shift;
304 0           $self->{"name"} = shift;
305 0           $self->{"type"} = "class";
306 0           $self->{"top_y"} = 1;
307 0           $self->{"left_x"} = 1;
308 0           $self->{"width"} = 2; # arbitary
309 0           $self->{"height"} = 2; # arbitary
310             #$self->{"operations"} = [];
311             #$self->{"attributes"} = [];
312 0           $self->{operation_index} = {};
313              
314 0           return 1;
315             }
316              
317             sub _update {
318 0     0     my $self = shift;
319              
320 0           my %updated = %{$self->{_updated}};
  0            
321              
322 0 0         if ($updated{"attributes"}) {
323 0           my $longest_element = ($self->{"width"} -1) / 0.5;
324 0           my @attributes = @{$self->{"attributes"}};
  0            
325 0           my $last_element = pop @attributes;
326 0 0         if (length $last_element > $longest_element) {
327 0           $self->{"width"} = (length $last_element * 0.5) + 1;
328             }
329 0           $self->{height} += 0.8;
330             }
331              
332 0 0         if ($updated{"operations"}) {
333 0           my $longest_element = ($self->{width} -1) / 0.5;
334 0           my @operations = @{$self->{"operations"}};
  0            
335 0           my $last_element = pop @operations;
336 0 0         if (length $last_element > $longest_element) {
337 0           $self->{"width"} = (length $last_element * 0.5) + 1;
338             }
339 0           $self->{"height"} += 0.8;
340             }
341              
342 0           undef $self->{"_updated"};
343              
344 0           return 1;
345             }
346              
347              
348             1;
349              
350             ##############################################################################
351              
352              
353             =head2 See Also
354              
355             L
356              
357             L
358              
359             L
360              
361             L
362              
363             =head1 AUTHOR
364              
365             Aaron Trevena, Eaaron.trevena@gmail.comE
366              
367             =head1 COPYRIGHT AND LICENSE
368              
369             Copyright (C) 2004 by Aaron Trevena
370              
371             This library is free software; you can redistribute it and/or modify
372             it under the same terms as Perl itself, either Perl version 5.8.1 or,
373             at your option, any later version of Perl 5 you may have available.
374              
375             =cut
376              
377             ########################################################################