| 1 | package yEd::PropertyBasedObject; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | 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 (e.g. 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 | =head2 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 | 1 | our @EXPORT = qw(_PROPERTY %match @ignoreProperties setProperties getProperties hasProperties); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | 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 | 1 | our @ignoreProperties = ( 'id' ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 setProperties | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Takes arguments of the form 'property1 => value, property2 => value2, ...' and sets the provided properties accordingly for the element in whichs 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 | sub setProperties { | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
setProperties: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my %ignore = map{ $_ => 1 } @ignoreProperties; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | confess 'properties must be supplied as key => value pairs (odd number supplied)' if(@_ % 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | my %props = @_; | |||||||||||||||||||||||||||
| 88 | 0 | foreach my $key (keys %props) { | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless ($ignore{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | confess "no such property: $key" unless ($self->can($key)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->$key($props{$key}); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 getProperties | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Returns a hash of the properties and their current values for the element in whichs context this funktion is called. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head3 EXAMPLE | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | my %properties = $node->getProperties(); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub getProperties { | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return %{$_[0]->{'properties'}}; | ||||||||||||||||||||||||||
|
Subroutine
getProperties: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 hasProperties | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Takes arguments of the form 'property1 => value, property2 => value2, ...'. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Returns true if the element in whichs 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 | sub hasProperties { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
hasProperties: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | confess 'properties must be supplied as key => value pairs (odd number supplied)' if(@_ % 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | my %props = @_; | |||||||||||||||||||||||||||
| 129 | 0 | foreach my $key (keys %props) { | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | return 0 unless ($self->can($key)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $v1 = $self->$key(); | |||||||||||||||||||||||||||
| 132 | 0 | my $v2 = $props{$key}; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($v1 =~ m/$match{'float'}/ and $v2 =~ m/$match{'float'}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$v1 =~ /$yEd::PropertyBasedObject::match{"float"}/ and $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
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 table0%0/3
A and B
A= $v1 =~ /$yEd::PropertyBasedObject::match{"float"}/B= $v2 =~ /$yEd::PropertyBasedObject::match{"float"}/
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return 0 unless ($v1 == $v2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } else { | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return 0 unless ($v1 eq $v2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | return 1; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub _PROPERTY { | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $ACCESSKEY = (caller(1))[3]; | ||||||||||||||||||||||||||
|
Subroutine
_PROPERTY: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 144 | 0 | $ACCESSKEY =~ s/^.*:://; | |||||||||||||||||||||||||||
| 145 | 0 | my ($writemask, $self, $value) = @_; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ($writemask) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($writemask eq 'ro') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 149 | 0 | confess "tried to write to a read only property ($ACCESSKEY)"; | |||||||||||||||||||||||||||
| 150 | } elsif ($writemask eq 'bool') { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $value = $value ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | confess "value for property $ACCESSKEY doesn't match $writemask (given value: $value)" unless ($value =~ m/$writemask/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | $self->{'properties'}{$ACCESSKEY} = $value; | |||||||||||||||||||||||||||
| 160 | 0 | return; | |||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 0 | 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 | 1 | 1; | |||||||||||||||||||||||||||