PropertyBasedObject.pm

filter: Statementstmt 82.0% 41 / 50 Branchbran 50.0% 16 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 69.0% 67 / 97 CC 20 SCAR 21.9
CC
20
Cov
68%
hasProperties
11.4
_PROPERTY
10.5
setProperties
8.1
CRAP 8.9
« Node/GenericNode.pm ↑ Summary Edge.pm »
1 package yEd::PropertyBasedObject;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 yEd::PropertyBasedObject - common methods for property based yEd Objects (Nodes,Edges,Labels)
10  
11 =head1 DESCRIPTION
12  
13 This module is just a collection of commonly used subroutines for handling property access.
14  
15 All properties can be accessed by using the property name as a funktion.
16 Without any parameters they will act as a getter and as a setter if a value is provided.
17 An exception will be thrown if the value is invalid.
18 All properties will have very similar default values to yEd.
19  
20 For a list of available properties for each Object have a look at the documentation for
21  
22 =over 4
23  
24 =item *
25  
26 L<yEd::Node>
27  
28 =item *
29  
30 L<yEd::Edge>
31  
32 =item *
33  
34 L<yEd::Label>
35  
36 =back
37  
38 and its subclasses.
39  
40 =head3 EXAMPLE
41  
42 $node->x(3.5);
43 $node->fillColor('#cccccc');
44 # which is equivalent to:
45 $node->setProperties('x' => 3.5, 'fillColor' => '#cccccc');
46  
47 my $text = $label->text();
48  
49 =cut
50  
51 3 our @EXPORT = qw(_PROPERTY %match @ignoreProperties setProperties getProperties hasProperties);
52  
53 3 our %match = (
54 'color' => '^(?:#[0-9a-f]{6}(?:[0-9a-f]{2})?|none)$',
55 'uint' => '^\d+$',
56 'ufloat' => '^\d+(?:\.\d+)?$',
57 'float' => '^-?\d+(?:\.\d+)?$',
58 'ratio' => '^(?:1(?:\.0+)?|0(?:\.\d+)?)$',
59 'linetype' => '^(?:line|dotted|dashed|dashed_dotted)$',
60 'arrowtype' => '^(?:standard|delta|white_delta|diamond|white_diamond|short|plain|concave|convex|circle|transparent_circle|dash|skewed_dash|t_shape|crows_foot_one_mandatory|crows_foot_many_mandatory|crows_foot_one_optional|crows_foot_many_optional|crows_foot_one|crows_foot_many|crows_foot_optional|none)$',
61 'alignment' => '^(?:center|right|left)$',
62 'fontstyle' => '^(?:plain|bold|italic|bolditalic)$',
63 'autosizepolicy' => '^(?:content|node_size|node_height|node_width)$',
64 'arctype' => '^(?:fixedRatio|fixedHeight)$',
65 );
66  
67 3 our @ignoreProperties = ( 'id' );
68  
69 =head1 SUBROUTINES/METHODS
70  
71 =head2 setProperties
72  
73 Takes arguments of the form C<property1 =E<gt> value, property2 =E<gt> value2, ...> and sets the provided properties accordingly for the element in which's context this funktion is called.
74  
75 Properties not provided will be left unchanged.
76  
77 =head3 EXAMPLE
78  
79 $node->setProperties('fillColor' => '#ffffff', 'borderColor' => 'none');
80  
81 =cut
82  
83 11 ▶sub setProperties {
Subroutine setProperties: called CC=5 SCAR=20.9
Pod documented
84 11 my $self = shift;
85 11 my %ignore = map{ $_ => 1 } @ignoreProperties;
86 11 ▶ confess 'properties must be supplied as key => value pairs (odd number supplied)' if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 11 11
87 11 my %props = @_;
88 11 foreach my $key (keys %props) {
89 0 ▶ unless ($ignore{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore{$key}) 0 0 0
90 0 ▶ confess "no such property: $key" unless ($self->can($key));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($key) 0 0 0
91 0 $self->$key($props{$key});
92 }
93 }
94 11 return;
95 }
96  
97 =head2 getProperties
98  
99 Returns a hash of the properties and their current values for the element in which's context this funktion is called.
100  
101 =head3 EXAMPLE
102  
103 my %properties = $node->getProperties();
104  
105 =cut
106  
107 0 ▶sub getProperties {
Subroutine getProperties: not called CC=1 SCAR=6.9
Pod documented
108 0 return %{$_[0]->{'properties'}};
109 }
110  
111 =head2 hasProperties
112  
113 Takes arguments of the form C<property1 =E<gt> value, property2 =E<gt> value2, ...>.
114  
115 Returns true if the element in which's context this funktion is called has all provided properties set to the provided values.
116  
117 Returns false otherwise or if an invalid property is provided.
118  
119 =head3 EXAMPLE
120  
121 if ($node->hasProperties('fillColor' => '#ffffff', 'borderColor' => 'none')) { ...
122  
123 =cut
124  
125 2 ▶sub hasProperties {
Subroutine hasProperties: called CC=8 SCAR=24.3
Pod documented
126 2 my $self = shift;
127 2 ▶ confess 'properties must be supplied as key => value pairs (odd number supplied)' if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 2 2
128 2 my %props = @_;
129 2 foreach my $key (keys %props) {
130 2 ▶ return 0 unless ($self->can($key));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($key) 0 2 2
131 2 my $v1 = $self->$key();
132 2 my $v2 = $props{$key};
133 2 ▶ if ($v1 =~ m/$match{'float'}/ and $v2 =~ m/$match{'float'}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 =~ /$yEd::PropertyBasedObject::match{'float'}/ and $v2 =~ /$yEd::PropertyBasedObject::match{'float'}/) { } 2 0 2
Condition33%1/3
$v1 =~ /$yEd::PropertyBasedObject::match{"float"}/ and $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
!ll&&!rl&&r
002
MC/DC0%0/2
$v1 =~ /$yEd::PropertyBasedObject::match{"float"}/ and $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
$v1 =~ /$yEd::PropertyBasedObject::match{"float"}/ $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
Truth table33%1/3
A and B
A= $v1 =~ /$yEd::PropertyBasedObject::match{"float"}/B= $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
ABresult
0X0
100
111
134 2 ▶ return 0 unless ($v1 == $v2);
Branch: 1 branch
BranchTrueFalseTotal
unless $v1 == $v2 0 2 2
135 } else {
136 0 ▶ return 0 unless ($v1 eq $v2);
Branch: 1 branch
BranchTrueFalseTotal
unless $v1 eq $v2 0 0 0
137 }
138 }
139 2 return 1;
140 }
141  
142 384 ▶sub _PROPERTY {
Subroutine _PROPERTY: called CC=9 SCAR=23.5
143 384 my $ACCESSKEY = (caller(1))[3];
144 384 $ACCESSKEY =~ s/^.*:://;
145 384 my ($writemask, $self, $value) = @_;
146 384 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 152 232 384
147 152 ▶ if ($writemask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writemask) 148 4 152
148 148 ▶ if ($writemask eq 'ro') {
Branch: 3 branches
BranchTrueFalseTotal
if ($writemask eq 'ro') { } 0 148 148
elsif ($writemask eq 'bool') { } 13 135 148
elsif ($writemask =~ /^isa\((.+)\)$/) { } 0 135 135
149 0 confess "tried to write to a read only property ($ACCESSKEY)";
150 } elsif ($writemask eq 'bool') {
151 13 ▶ $value = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 3 10 13
152 } elsif ($writemask =~ m/^isa\((.+)\)$/) {
153 0 my $type = $1;
154 0 ▶ confess "value for property $ACCESSKEY must be a reference of the type $type (or one of its subtypes) (given value: $value)" unless ($value->isa($type));
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa($type) 0 0 0
155 } else {
156 135 ▶ confess "value for property $ACCESSKEY doesn't match $writemask (given value: $value)" unless ($value =~ m/$writemask/);
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$writemask/ 0 135 135
157 }
158 }
159 152 $self->{'properties'}{$ACCESSKEY} = $value;
160 152 return;
161 } else {
162 232 return $self->{'properties'}{$ACCESSKEY};
163 }
164 }
165  
166 =head1 SEE ALSO
167  
168 L<yEd::Document> for further informations about the whole package
169  
170 L<yEd::Node> for information about Node elements
171  
172 L<yEd::Edge> for information about Edge elements
173  
174 L<yEd::Label> for information about Label elements
175  
176 =cut
177  
178 3 1;
« Node/GenericNode.pm ↑ Summary Edge.pm »