| 1 | package Games::Object::Manager; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Carp qw(carp croak confess); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Games::Object::Common qw(FetchParams LoadData SaveData ANAME_MANAGER); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 9 | $VERSION = "0.11"; | |||||||||||||||||||||||||||||||||||
| 13 | 9 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||
| 14 | 9 | @EXPORT_OK = qw($CompareFunction REL_NO_CIRCLE); | |||||||||||||||||||||||||||||||||||
| 15 | 9 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||
| 16 | flags => [ qw(REL_NO_CIRCLE) ], | ||||||||||||||||||||||||||||||||||||
| 17 | variables => [ qw($CompareFunction) ], | ||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use vars qw($CompareFunction); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | # Define flags. | ||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use constant REL_NO_CIRCLE => 0x00000001; # Don't allow cir. relates | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | # Define the comparison function to use for processing order. | ||||||||||||||||||||||||||||||||||||
| 26 | 9 | $CompareFunction = '_CompareDefault'; | |||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | # Define the default process info. | ||||||||||||||||||||||||||||||||||||
| 29 | 9 | my @ProcessList = ( | |||||||||||||||||||||||||||||||||||
| 30 | 'process_queue', | ||||||||||||||||||||||||||||||||||||
| 31 | 'process_pmod', | ||||||||||||||||||||||||||||||||||||
| 32 | 'process_tend_to', | ||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||
| 34 | 9 | my $ProcessLimit = 100; | |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | #### | ||||||||||||||||||||||||||||||||||||
| 37 | ## INTERNAL FUNCTIONS | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | # Default comparison function when determining the order of processing of | ||||||||||||||||||||||||||||||||||||
| 40 | # two objects. | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | 27 | ▶ | sub _CompareDefault { $b->priority() <=> $a->priority() } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_CompareDefault: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | # Comparison function when using the creation order option | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub _CompareAddOrder { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_CompareAddOrder: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 47 | 0 | my $cmp = $b->priority() <=> $a->priority(); | |||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $cmp == 0 ? $a->order() <=> $b->order() : $cmp; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | # Create a relation methods | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | sub _CreateRelators | ||||||||||||||||||||||||||||||||||||
| 54 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_CreateRelators: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 55 | 15 | my %args = @_; | |||||||||||||||||||||||||||||||||||
| 56 | 15 | my $realname = $args{name}; | |||||||||||||||||||||||||||||||||||
| 57 | 15 | my $name = $args{relate_method}; | |||||||||||||||||||||||||||||||||||
| 58 | 15 | my $uname = $args{unrelate_method}; | |||||||||||||||||||||||||||||||||||
| 59 | 15 | my $rname = $args{related_method}; | |||||||||||||||||||||||||||||||||||
| 60 | 15 | my $iname = $args{is_related_method}; | |||||||||||||||||||||||||||||||||||
| 61 | 15 | my $lname = $args{related_list_method}; | |||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 64 | 27 | ▶ | *$name = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||
| 65 | 27 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 66 | 27 | ▶ | my $args = ( ref($_[$#_]) eq 'HASH' ? pop @_ : {} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | 27 | $man->relate(how => $realname, | |||||||||||||||||||||||||||||||||||
| 68 | self => $_[0], | ||||||||||||||||||||||||||||||||||||
| 69 | object => $_[1], | ||||||||||||||||||||||||||||||||||||
| 70 | other => $_[2], | ||||||||||||||||||||||||||||||||||||
| 71 | args => $args); | ||||||||||||||||||||||||||||||||||||
| 72 | 15 | ▶ | } if (!defined(&$name)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | *$uname = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||
| 74 | 8 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | my $args = ( ref($_[$#_]) eq 'HASH' ? pop @_ : {} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 76 | 8 | $man->unrelate(how => $realname, | |||||||||||||||||||||||||||||||||||
| 77 | object => $_[0], | ||||||||||||||||||||||||||||||||||||
| 78 | other => $_[1], | ||||||||||||||||||||||||||||||||||||
| 79 | args => $args); | ||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | } if (!defined(&$uname)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 81 | 161 | ▶ | *$rname = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 82 | 161 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 83 | 161 | $man->related(how => $realname, object => $_[0]); | |||||||||||||||||||||||||||||||||||
| 84 | 15 | ▶ | } if (!defined(&$rname)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 8 | ▶ | *$iname = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 86 | 8 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 87 | 8 | $man->is_related(how => $realname, self => $_[0], object => $_[1]); | |||||||||||||||||||||||||||||||||||
| 88 | 15 | ▶ | } if (!defined(&$iname)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | *$lname = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 90 | 4 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 91 | 4 | $man->related_list(how => $realname, self => $_[0]); | |||||||||||||||||||||||||||||||||||
| 92 | 15 | ▶ | } if (!defined(&$lname)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | #### | ||||||||||||||||||||||||||||||||||||
| 96 | ## CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | # Basic constructor | ||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | sub new | ||||||||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 102 | 8 | my $proto = shift; | |||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||
| 104 | 8 | my $man = {}; | |||||||||||||||||||||||||||||||||||
| 105 | 8 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 108 | 8 | FetchParams(\@_, \%args, [ | |||||||||||||||||||||||||||||||||||
| 109 | [ 'opt', 'base_id', 0, 'int' ], | ||||||||||||||||||||||||||||||||||||
| 110 | [ 'opt', 'process_list', \@ProcessList, 'arrayref' ], | ||||||||||||||||||||||||||||||||||||
| 111 | [ 'opt', 'process_limit', $ProcessLimit, 'int' ], | ||||||||||||||||||||||||||||||||||||
| 112 | ] ); | ||||||||||||||||||||||||||||||||||||
| 113 | 8 | bless $man, $class; | |||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | # Define storage for created objects. Note that this means that objects | ||||||||||||||||||||||||||||||||||||
| 116 | # will be persistent. They can go out of scope and still exist, since | ||||||||||||||||||||||||||||||||||||
| 117 | # each is identified by a unique ID. | ||||||||||||||||||||||||||||||||||||
| 118 | 8 | $man->{index} = {}; | |||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | # Define tables that handle object relationships | ||||||||||||||||||||||||||||||||||||
| 121 | 8 | $man->{relation_def} = {}; | |||||||||||||||||||||||||||||||||||
| 122 | 8 | $man->{relate_to} = {}; | |||||||||||||||||||||||||||||||||||
| 123 | 8 | $man->{relate_from} = {}; | |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | # Define a counter for creating objects when the user wants us to | ||||||||||||||||||||||||||||||||||||
| 126 | # assume that every new object is unique. The starting number can be | ||||||||||||||||||||||||||||||||||||
| 127 | # changed with base_id() but only if no objects have been created yet. | ||||||||||||||||||||||||||||||||||||
| 128 | 8 | $man->{next} = $args{base_id}; | |||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | # Define a counter that will be used to track the order in which objects | ||||||||||||||||||||||||||||||||||||
| 131 | # are created. This is to support a new feature in v0.05 | ||||||||||||||||||||||||||||||||||||
| 132 | 8 | $man->{order} = 0; | |||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||
| 134 | # And if we are doing this, we want to try and use space efficiently by | ||||||||||||||||||||||||||||||||||||
| 135 | # reclaiming unused IDs. Thus we track the lowest available opening. | ||||||||||||||||||||||||||||||||||||
| 136 | # [ NOT YET IMPLEMENTED ] | ||||||||||||||||||||||||||||||||||||
| 137 | 8 | $man->{reclaim} = 1; | |||||||||||||||||||||||||||||||||||
| 138 | 8 | $man->{avail} = 0; | |||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | # Track the highest priority object. | ||||||||||||||||||||||||||||||||||||
| 141 | 8 | $man->{highest_pri} = 0; | |||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | # Define a table that shows what order process() is supposed to do | ||||||||||||||||||||||||||||||||||||
| 144 | # things. | ||||||||||||||||||||||||||||||||||||
| 145 | 8 | $man->{process_list} = $args{process_list}; | |||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | # Define a limit to how many times the same item can be processed in a | ||||||||||||||||||||||||||||||||||||
| 148 | # queue (see process_queue() for details) | ||||||||||||||||||||||||||||||||||||
| 149 | 8 | $man->{process_limit} = $args{process_limit}; | |||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | # Set the default inherit_from relationship. | ||||||||||||||||||||||||||||||||||||
| 152 | 8 | $man->define_relation( | |||||||||||||||||||||||||||||||||||
| 153 | name => 'inherit', | ||||||||||||||||||||||||||||||||||||
| 154 | relate_method => 'inherit', | ||||||||||||||||||||||||||||||||||||
| 155 | unrelate_method => 'disinherit', | ||||||||||||||||||||||||||||||||||||
| 156 | related_method => 'inheriting_from', | ||||||||||||||||||||||||||||||||||||
| 157 | related_list_method => 'has_inherting', | ||||||||||||||||||||||||||||||||||||
| 158 | is_related_method => 'is_inheriting_from', | ||||||||||||||||||||||||||||||||||||
| 159 | flags => REL_NO_CIRCLE, | ||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | # Done. | ||||||||||||||||||||||||||||||||||||
| 163 | 8 | $man; | |||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | # Constructor for loading entire container from a file. | ||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||
| 168 | sub load | ||||||||||||||||||||||||||||||||||||
| 169 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=11 SCAR=25.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 170 | 1 | my $proto = shift; | |||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||
| 172 | 1 | my $file = shift; | |||||||||||||||||||||||||||||||||||
| 173 | 1 | my $filename; | |||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | # If we got a filename instead of a file object, open the file. | ||||||||||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if (!ref($file)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 177 | 1 | $filename = $file; | |||||||||||||||||||||||||||||||||||
| 178 | 1 | $file = IO::File->new(); | |||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | $file->open("<$filename") or | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 180 | croak "Unable to open manager file '$filename'"; | ||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | # Initialize the object. | ||||||||||||||||||||||||||||||||||||
| 184 | 1 | my $man; | |||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | if (ref($proto)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 186 | # This is a "load in place", meaning we're reloading to an | ||||||||||||||||||||||||||||||||||||
| 187 | # existing object, so clear out the old stuff. | ||||||||||||||||||||||||||||||||||||
| 188 | 0 | $man = $proto; | |||||||||||||||||||||||||||||||||||
| 189 | 0 | foreach my $key (keys %$man) { delete $man->{$key}; } | |||||||||||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||||||||||
| 191 | # Totally new object originating from the file. | ||||||||||||||||||||||||||||||||||||
| 192 | 1 | $man = {}; | |||||||||||||||||||||||||||||||||||
| 193 | 1 | bless $man, $class; | |||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||
| 196 | # Check the header to make sure this is manager data. | ||||||||||||||||||||||||||||||||||||
| 197 | 1 | my $line = <$file>; chomp $line; | |||||||||||||||||||||||||||||||||||
| 198 | 1 | ▶ | croak "Did not find manager header data in file" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 199 | if ($line ne 'OBJ:__MANAGER__'); | ||||||||||||||||||||||||||||||||||||
| 200 | 1 | $line = <$file>; chomp $line; | |||||||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | croak "Second line of manager data header bad" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 202 | if ($line !~ /^CL:(.+)$/); | ||||||||||||||||||||||||||||||||||||
| 203 | 1 | my $mclass = $1; | |||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||
| 205 | # Load. | ||||||||||||||||||||||||||||||||||||
| 206 | 1 | LoadData($file, $man); | |||||||||||||||||||||||||||||||||||
| 207 | 1 | ▶ | $file->close() if defined($filename); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | # Restore manager attributes to all objects. | ||||||||||||||||||||||||||||||||||||
| 210 | 1 | foreach my $obj (values %{$man->{index}}) { $obj->manager($man); } | |||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||
| 212 | # Restore relators. | ||||||||||||||||||||||||||||||||||||
| 213 | 1 | foreach my $rel (values %{$man->{relation_def}}) { | |||||||||||||||||||||||||||||||||||
| 214 | 4 | _CreateRelators(%$rel); | |||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||
| 217 | # Done. | ||||||||||||||||||||||||||||||||||||
| 218 | 1 | bless $man, $mclass; | |||||||||||||||||||||||||||||||||||
| 219 | 1 | $man; | |||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||
| 222 | #### | ||||||||||||||||||||||||||||||||||||
| 223 | ## MANAGER DATA METHODS | ||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||
| 225 | # Save the manager and its contents to a file. | ||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||
| 227 | sub save | ||||||||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 229 | 1 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 230 | 1 | my $file = shift; | |||||||||||||||||||||||||||||||||||
| 231 | 1 | my $filename; | |||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||
| 233 | # If we got a filename instead of a file object, open the file. | ||||||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | if (!ref($file)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 235 | 1 | $filename = $file; | |||||||||||||||||||||||||||||||||||
| 236 | 1 | $file = IO::File->new(); | |||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | $file->open(">$filename") or | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 238 | croak "Unable to open manager file '$filename'"; | ||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | # Save header. This indicates that this is indeed manager object | ||||||||||||||||||||||||||||||||||||
| 242 | # data and preserves the class. | ||||||||||||||||||||||||||||||||||||
| 243 | 1 | print $file "OBJ:__MANAGER__\n" . | |||||||||||||||||||||||||||||||||||
| 244 | "CL:" . ref($man) . "\n"; | ||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||
| 246 | # Save data. See the comments on the save() routine in Games::Object | ||||||||||||||||||||||||||||||||||||
| 247 | # for why we copy the ref to an ordinary hash first. | ||||||||||||||||||||||||||||||||||||
| 248 | 1 | my %hash = %$man; | |||||||||||||||||||||||||||||||||||
| 249 | 1 | SaveData($file, \%hash); | |||||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | $file->close() if (defined($filename)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 251 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||
| 254 | # "Find" an object (i.e. look up its ID). If given something that is | ||||||||||||||||||||||||||||||||||||
| 255 | # already an object, validates that the object is still valid. If the | ||||||||||||||||||||||||||||||||||||
| 256 | # assertion flag is passed, an invalid object will result in a fatal error. | ||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||
| 258 | sub find | ||||||||||||||||||||||||||||||||||||
| 259 | 412 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 260 | 412 | my ($man, $id, $assert) = @_; | |||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | 412 | ▶ | if (!defined($id)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 263 | 26 | ▶ | if ($assert) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 264 | 0 | confess "Assertion failed: ID is undefined"; | |||||||||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||||||||||
| 266 | 26 | return undef; | |||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | 386 | ▶ | $id = $id->id() if (ref($id) && UNIVERSAL::isa($id, 'Games::Object')); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $id and UNIVERSAL::isa($id, "Games::Object")
MC/DC0%0/2
ref $id and UNIVERSAL::isa($id, "Games::Object")
ref $id
UNIVERSAL::isa($id, "Games::Object")
Truth table33%1/3
A and B
A= ref $idB= UNIVERSAL::isa($id, "Games::Object")
| |||||||||||||||||||||||||||||||||||||
| 270 | 386 | ▶ | if (defined($man->{index}{$id})) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 271 | 381 | $man->{index}{$id}; | |||||||||||||||||||||||||||||||||||
| 272 | } elsif ($assert) { | ||||||||||||||||||||||||||||||||||||
| 273 | # Report with confess() so user can see where the assertion was made | ||||||||||||||||||||||||||||||||||||
| 274 | 1 | confess "Assertion failed: '$id' is not a valid/managed object ID"; | |||||||||||||||||||||||||||||||||||
| 275 | } else { | ||||||||||||||||||||||||||||||||||||
| 276 | 4 | undef; | |||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | # Return the number of objects in the manager. | ||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub total_objects { | ||||||||||||||||||||||||||||||||||
|
Subroutine
total_objects: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 284 | 0 | scalar keys %{$man->{index}}; | |||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||
| 287 | # Returns the ID of an object, with the side effect that it validates that | ||||||||||||||||||||||||||||||||||||
| 288 | # this object is really a Games::Object derivative and is being managed by | ||||||||||||||||||||||||||||||||||||
| 289 | # this manager. The user specifies either the ID or the object ref. If valid, | ||||||||||||||||||||||||||||||||||||
| 290 | # the ID is always returned (thus it can be used to guarantee the return of | ||||||||||||||||||||||||||||||||||||
| 291 | # an ID when you're not sure if you were passed an object or the ID). | ||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||
| 293 | sub id | ||||||||||||||||||||||||||||||||||||
| 294 | 359 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
id: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 295 | 359 | my ($man, $obj, $assert) = @_; | |||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | 359 | ▶ | if (ref($obj) && UNIVERSAL::isa($obj, 'Games::Object')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
ref $obj and UNIVERSAL::isa($obj, "Games::Object")
MC/DC50%1/2
ref $obj and UNIVERSAL::isa($obj, "Games::Object")
ref $obj
UNIVERSAL::isa($obj, "Games::Object")
Truth table66%2/3
A and B
A= ref $objB= UNIVERSAL::isa($obj, "Games::Object")
| |||||||||||||||||||||||||||||||||||||
| 298 | 325 | my $id = $obj->id(); | |||||||||||||||||||||||||||||||||||
| 299 | 325 | ▶ | defined($man->{index}{$id}) ? $id : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 300 | } elsif (defined($man->{index}{$obj})) { | ||||||||||||||||||||||||||||||||||||
| 301 | 32 | $obj; | |||||||||||||||||||||||||||||||||||
| 302 | } elsif ($assert) { | ||||||||||||||||||||||||||||||||||||
| 303 | # Report with confess() so user can see where the assertion was made | ||||||||||||||||||||||||||||||||||||
| 304 | 1 | confess "Assertion failed: '$obj' is not a valid/managed object"; | |||||||||||||||||||||||||||||||||||
| 305 | } else { | ||||||||||||||||||||||||||||||||||||
| 306 | 1 | undef; | |||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||
| 310 | #### | ||||||||||||||||||||||||||||||||||||
| 311 | ## OBJECT MANAGEMENT METHODS | ||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||
| 313 | # Add a new object to the manager. The user may either specify an ID (which | ||||||||||||||||||||||||||||||||||||
| 314 | # must not already exist), or allow it to take a predefined ID from the object | ||||||||||||||||||||||||||||||||||||
| 315 | # (if defined), or pick one on its own (if previous two undefined) | ||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||
| 317 | sub add | ||||||||||||||||||||||||||||||||||||
| 318 | 33 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 319 | 33 | my ($man, $obj, $id) = @_; | |||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | # Pick new ID if needed. | ||||||||||||||||||||||||||||||||||||
| 322 | 33 | ▶ | $id = $obj->id() if (!defined($id)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 323 | 33 | ▶ | $id = $man->{next}++ if (!defined($id)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||
| 325 | # Make sure it does not exist. | ||||||||||||||||||||||||||||||||||||
| 326 | croak "Attempt to add duplicate object ID '$id'" | ||||||||||||||||||||||||||||||||||||
| 327 | 33 | ▶ | if (defined($man->{index}{$id})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||
| 329 | # Add it. Do this before adding the manager link so we don't get | ||||||||||||||||||||||||||||||||||||
| 330 | # a call back to us. | ||||||||||||||||||||||||||||||||||||
| 331 | 32 | $man->{index}{$id} = $obj; | |||||||||||||||||||||||||||||||||||
| 332 | 32 | $obj->id($id); | |||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||
| 334 | # Add the manager attribute | ||||||||||||||||||||||||||||||||||||
| 335 | 32 | $obj->manager($man); | |||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||
| 337 | # Done. | ||||||||||||||||||||||||||||||||||||
| 338 | 32 | $id; | |||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||
| 341 | # Similar to add, but allows an object to already exist under this ID, in | ||||||||||||||||||||||||||||||||||||
| 342 | # which case the old on is removed. Returns the same values as add(). The | ||||||||||||||||||||||||||||||||||||
| 343 | # ID to replace is always taken from the existing object. The ID parameter | ||||||||||||||||||||||||||||||||||||
| 344 | # is applied to the new object (thus it must not already exist). | ||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||
| 346 | sub replace | ||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
replace: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 348 | 0 | my ($man, $obj, $id) = @_; | |||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||
| 350 | # Get rid of the old object. Don't worry if the object does not | ||||||||||||||||||||||||||||||||||||
| 351 | # already exist. | ||||||||||||||||||||||||||||||||||||
| 352 | 0 | $man->remove($id); | |||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||
| 354 | # Add new one. | ||||||||||||||||||||||||||||||||||||
| 355 | 0 | $man->add($obj, $id); | |||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||
| 358 | # Remove an object. Returns the object if the object was found and removed, | ||||||||||||||||||||||||||||||||||||
| 359 | # undef if not. The on_removed action is invoked on the object (but before | ||||||||||||||||||||||||||||||||||||
| 360 | # the object is actually removed so it can still access the manager linkage). | ||||||||||||||||||||||||||||||||||||
| 361 | # User may specify additional args to be passed to the action() call. | ||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||
| 363 | sub remove | ||||||||||||||||||||||||||||||||||||
| 364 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=11 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 365 | 1 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 366 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||
| 368 | # If the last arg is a hash, this is additional args to any callback | ||||||||||||||||||||||||||||||||||||
| 369 | # that might get invoked. | ||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | my $aargs = ( @_ && ref($_[$#_]) eq 'HASH' ? pop @_ : {} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ and ref $_[$#_] eq "HASH"
@_
ref $_[$#_] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[$#_] eq "HASH"
| |||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||
| 372 | # Any remaining arg is other. | ||||||||||||||||||||||||||||||||||||
| 373 | 1 | ▶ | my $other = ( @_ ? shift : $self ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | # If object does not exist, no need to go any further. | ||||||||||||||||||||||||||||||||||||
| 376 | 1 | my $id = $man->id($self); | |||||||||||||||||||||||||||||||||||
| 377 | 1 | ▶ | return undef if (!defined($man->{index}{$id})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | # Fetch the object and invoke action. | ||||||||||||||||||||||||||||||||||||
| 380 | 1 | $self = $man->find($id); | |||||||||||||||||||||||||||||||||||
| 381 | 1 | $self->action(other => $other, | |||||||||||||||||||||||||||||||||||
| 382 | action => "object:remove", | ||||||||||||||||||||||||||||||||||||
| 383 | args => $aargs); | ||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||
| 385 | # Break relationships TO this object. These are all done with the | ||||||||||||||||||||||||||||||||||||
| 386 | # force option. This means that no tests will be done for each | ||||||||||||||||||||||||||||||||||||
| 387 | # unrelate(), but post-unrelate() actions WILL occur. | ||||||||||||||||||||||||||||||||||||
| 388 | 1 | my @hows = (); | |||||||||||||||||||||||||||||||||||
| 389 | 1 | @hows = keys %{$man->{relate_from}{$id}} | |||||||||||||||||||||||||||||||||||
| 390 | 1 | ▶ | if (defined($man->{relate_from}{$id})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 391 | 1 | foreach my $how (@hows) { | |||||||||||||||||||||||||||||||||||
| 392 | 2 | my @fobjs = @{$man->{relate_from}{$id}{$how}}; | |||||||||||||||||||||||||||||||||||
| 393 | 2 | foreach my $fobj (@fobjs) { | |||||||||||||||||||||||||||||||||||
| 394 | 4 | $man->unrelate( | |||||||||||||||||||||||||||||||||||
| 395 | how => $how, | ||||||||||||||||||||||||||||||||||||
| 396 | object => $fobj, | ||||||||||||||||||||||||||||||||||||
| 397 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 398 | force => 1, | ||||||||||||||||||||||||||||||||||||
| 399 | args => { source => 'remove:to', %$aargs }, | ||||||||||||||||||||||||||||||||||||
| 400 | ); | ||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||
| 404 | # Break all relationships FROM this object to others. | ||||||||||||||||||||||||||||||||||||
| 405 | 1 | @hows = (); | |||||||||||||||||||||||||||||||||||
| 406 | 1 | @hows = keys %{$man->{relate_to}{$id}} | |||||||||||||||||||||||||||||||||||
| 407 | 1 | ▶ | if (defined($man->{relate_to}{$id})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 408 | 1 | foreach my $how (@hows) { | |||||||||||||||||||||||||||||||||||
| 409 | 0 | my @objs = map { $man->find($_) } @{$man->{relate_from}{$id}{$how}}; | |||||||||||||||||||||||||||||||||||
| 410 | 1 | foreach my $obj (@objs) { | |||||||||||||||||||||||||||||||||||
| 411 | 0 | $man->unrelate( | |||||||||||||||||||||||||||||||||||
| 412 | how => $how, | ||||||||||||||||||||||||||||||||||||
| 413 | object => $obj, | ||||||||||||||||||||||||||||||||||||
| 414 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 415 | force => 1, | ||||||||||||||||||||||||||||||||||||
| 416 | args => { source => 'remove:from', %$aargs } | ||||||||||||||||||||||||||||||||||||
| 417 | ); | ||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||
| 421 | # Delete from internal tables, which should remove all references to | ||||||||||||||||||||||||||||||||||||
| 422 | # it save the one we have. | ||||||||||||||||||||||||||||||||||||
| 423 | 1 | delete $man->{index}{$id}; | |||||||||||||||||||||||||||||||||||
| 424 | 1 | delete $man->{relate_to}{$id}; | |||||||||||||||||||||||||||||||||||
| 425 | 1 | delete $man->{relate_from}{$id}; | |||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||
| 427 | # Remove the manager attribute. | ||||||||||||||||||||||||||||||||||||
| 428 | 1 | $self->manager(undef); | |||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||
| 430 | # Done. | ||||||||||||||||||||||||||||||||||||
| 431 | 1 | $self; | |||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||
| 434 | # Go down the complete list of objects and perform a method call on each. If | ||||||||||||||||||||||||||||||||||||
| 435 | # no args are given, 'process' is assumed. This will call them in order of | ||||||||||||||||||||||||||||||||||||
| 436 | # priority. | ||||||||||||||||||||||||||||||||||||
| 437 | # | ||||||||||||||||||||||||||||||||||||
| 438 | # The caller may choose to filter the list by providing a CODE ref as the | ||||||||||||||||||||||||||||||||||||
| 439 | # first argument. Only the objects for which the CODE ref returns true are | ||||||||||||||||||||||||||||||||||||
| 440 | # considered (new in v0.10). | ||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||
| 442 | sub process | ||||||||||||||||||||||||||||||||||||
| 443 | 13 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 444 | 13 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||
| 446 | # Note that we grab the actual objects and not the ids in the sort. | ||||||||||||||||||||||||||||||||||||
| 447 | # This is more efficient, as each object is simply a reference (a | ||||||||||||||||||||||||||||||||||||
| 448 | # scalar with a fixed size) as opposed to a string (a scalar with | ||||||||||||||||||||||||||||||||||||
| 449 | # a variable size). | ||||||||||||||||||||||||||||||||||||
| 450 | 13 | my $method = shift; | |||||||||||||||||||||||||||||||||||
| 451 | 13 | ▶ | my $code = ( ref($method) eq 'CODE' ? $method : undef ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 452 | 13 | ▶ | $method = shift if ($code); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 453 | 13 | my @args = @_; | |||||||||||||||||||||||||||||||||||
| 454 | 13 | ▶ | $method = 'process' if (!defined($method)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||
| 456 | # Derive the object list. | ||||||||||||||||||||||||||||||||||||
| 457 | my @objs = ( | ||||||||||||||||||||||||||||||||||||
| 458 | $code ? | ||||||||||||||||||||||||||||||||||||
| 459 | 3 | grep { &$code($_, @args) } | |||||||||||||||||||||||||||||||||||
| 460 | 3 | grep { UNIVERSAL::can($_, $method) } | |||||||||||||||||||||||||||||||||||
| 461 | 1 | sort $CompareFunction values %{$man->{index}} | |||||||||||||||||||||||||||||||||||
| 462 | : | ||||||||||||||||||||||||||||||||||||
| 463 | 30 | grep { UNIVERSAL::can($_, $method) } | |||||||||||||||||||||||||||||||||||
| 464 | 13 | ▶ | sort $CompareFunction values %{$man->{index}} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 465 | ); | ||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||
| 467 | # Process. | ||||||||||||||||||||||||||||||||||||
| 468 | 13 | ▶ | unshift @args, $man->{process_list} if ($method eq 'process'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 469 | 13 | foreach my $obj (@objs) { | |||||||||||||||||||||||||||||||||||
| 470 | 29 | ▶ | $obj->$method(@args) if (UNIVERSAL::can($obj, $method)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||
| 473 | # Return the number of objects processed. | ||||||||||||||||||||||||||||||||||||
| 474 | 13 | scalar(@objs); | |||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||
| 477 | # Set/fetch the process list for the process() function. Note that the user is | ||||||||||||||||||||||||||||||||||||
| 478 | # not limited to the methods found here. The methods can be in the subclass | ||||||||||||||||||||||||||||||||||||
| 479 | # if desired. Note that we have no way to validate the method names here, | ||||||||||||||||||||||||||||||||||||
| 480 | # so we take it on good faith that they exist. | ||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||
| 482 | 0 | ▶ | sub process_list { | ||||||||||||||||||||||||||||||||||
|
Subroutine
process_list: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 483 | 0 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | if (@_) { @{$man->{process_list}} = @_ } else { @{$man->{process_list}} } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||
| 487 | #### | ||||||||||||||||||||||||||||||||||||
| 488 | ## OBJECT RELATIONSHIP METHODS | ||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||
| 490 | # Check to see if a relationship is valid. If assertion flag present, this | ||||||||||||||||||||||||||||||||||||
| 491 | # will bomb the program if the relationship is not present. | ||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||
| 493 | sub has_relation | ||||||||||||||||||||||||||||||||||||
| 494 | 684 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
has_relation: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 495 | 684 | my ($man, $how, $assert) = @_; | |||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||
| 497 | 684 | ▶ | defined($man->{relation_def}{$how}) ? 1 : | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 498 | $assert ? croak "'$how' is an invalid relationship type" | ||||||||||||||||||||||||||||||||||||
| 499 | : 0; | ||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||
| 502 | # Define a new relationship. This allows objects to be related with the | ||||||||||||||||||||||||||||||||||||
| 503 | # relate() method, or via a relator method created. | ||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||
| 505 | sub define_relation | ||||||||||||||||||||||||||||||||||||
| 506 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
define_relation: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 507 | 11 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 508 | 11 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||
| 510 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 511 | 11 | FetchParams(\@_, \%args, [ | |||||||||||||||||||||||||||||||||||
| 512 | [ 'req', 'name', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 513 | [ 'opt', 'relate_method', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 514 | [ 'opt', 'unrelate_method', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 515 | [ 'opt', 'related_method', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 516 | [ 'opt', 'related_list_method', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 517 | [ 'opt', 'is_related_method', undef, 'string' ], | ||||||||||||||||||||||||||||||||||||
| 518 | [ 'opt', 'on_remove', undef, 'callback' ], | ||||||||||||||||||||||||||||||||||||
| 519 | [ 'opt', 'flags', 0, 'int' ], | ||||||||||||||||||||||||||||||||||||
| 520 | ], 1 ); | ||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||
| 522 | # Add it. Note that we allow redefinition at will. | ||||||||||||||||||||||||||||||||||||
| 523 | 11 | my $rname = $args{name}; | |||||||||||||||||||||||||||||||||||
| 524 | $args{relate_method} = $rname | ||||||||||||||||||||||||||||||||||||
| 525 | 11 | ▶ | if (!$args{relate_method}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 526 | $args{unrelate_method} = "un${rname}" | ||||||||||||||||||||||||||||||||||||
| 527 | 11 | ▶ | if (!$args{unrelate_method}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 528 | $args{related_method} = "${rname}_to" | ||||||||||||||||||||||||||||||||||||
| 529 | 11 | ▶ | if (!$args{related_method}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 530 | $args{related_list_method} = "${rname}_list" | ||||||||||||||||||||||||||||||||||||
| 531 | 11 | ▶ | if (!$args{related_list_method}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 532 | $args{is_related_method} = "is_${rname}" | ||||||||||||||||||||||||||||||||||||
| 533 | 11 | ▶ | if (!$args{is_related_method}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 534 | 11 | $man->{relation_def}{$rname} = \%args; | |||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||
| 536 | # Create relator. | ||||||||||||||||||||||||||||||||||||
| 537 | 11 | _CreateRelators(%args); | |||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||
| 539 | # Done. | ||||||||||||||||||||||||||||||||||||
| 540 | 11 | 1; | |||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||
| 543 | # Relate two objects. | ||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||
| 545 | sub relate | ||||||||||||||||||||||||||||||||||||
| 546 | 32 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
relate: called CC=10 SCAR=23.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 547 | 32 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 548 | 32 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||
| 550 | # Fetch parameters. Self is the thing being related to, object is | ||||||||||||||||||||||||||||||||||||
| 551 | # the thing being related to it. | ||||||||||||||||||||||||||||||||||||
| 552 | FetchParams(\@_, \%args, [ | ||||||||||||||||||||||||||||||||||||
| 553 | 32 | ▶ | [ 'req', 'how', undef, sub { $man->has_relation(shift); } ], | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 554 | [ 'req', 'self', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 555 | [ 'req', 'object', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 556 | [ 'opt', 'other', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 557 | [ 'opt', 'force', 0, 'boolean' ], | ||||||||||||||||||||||||||||||||||||
| 558 | [ 'opt', 'args', {}, 'hashref' ], | ||||||||||||||||||||||||||||||||||||
| 559 | ] ); | ||||||||||||||||||||||||||||||||||||
| 560 | 32 | my $how = $args{how}; | |||||||||||||||||||||||||||||||||||
| 561 | 32 | my $self = $args{self}; | |||||||||||||||||||||||||||||||||||
| 562 | 32 | my $object = $args{object}; | |||||||||||||||||||||||||||||||||||
| 563 | 32 | my $other = $args{other}; | |||||||||||||||||||||||||||||||||||
| 564 | 32 | my $force = $args{force}; | |||||||||||||||||||||||||||||||||||
| 565 | 32 | my $aargs = $args{args}; | |||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||
| 567 | # If other is undefined, then we set it equal to self, meaning we assume | ||||||||||||||||||||||||||||||||||||
| 568 | # that the receipient of the object itself instigated the action. | ||||||||||||||||||||||||||||||||||||
| 569 | 32 | ▶ | $other = $self if (!defined($other)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||
| 571 | # Do it. First fetch necesary parameters. | ||||||||||||||||||||||||||||||||||||
| 572 | 32 | my $rel = $man->{relation_def}{$how}; | |||||||||||||||||||||||||||||||||||
| 573 | 32 | my $doaction = "object:on_" . $rel->{relate_method}; | |||||||||||||||||||||||||||||||||||
| 574 | 32 | my $tryaction = "object:try_" . $rel->{relate_method}; | |||||||||||||||||||||||||||||||||||
| 575 | 32 | my $idself = $man->id($self); $self = $man->find($idself); | |||||||||||||||||||||||||||||||||||
| 576 | 32 | my $idobject = $man->id($object); $object = $man->find($idobject); | |||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||
| 578 | # Perform check to see if relationship is allowed. We do this | ||||||||||||||||||||||||||||||||||||
| 579 | # before anything else, including attempting to unrelate it from | ||||||||||||||||||||||||||||||||||||
| 580 | # whatever it may be currently related to. This way the relate | ||||||||||||||||||||||||||||||||||||
| 581 | # check code can see how it is related now in case that means | ||||||||||||||||||||||||||||||||||||
| 582 | # anything, plus it prevents orphaned objects (which would happen | ||||||||||||||||||||||||||||||||||||
| 583 | # if we first unrelate()d it and then failed the relate() check). | ||||||||||||||||||||||||||||||||||||
| 584 | 32 | ▶ | my $check = | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$force || $self->action('action', $tryaction, 'object', $object, 'other', $other, 'args', $aargs)
MC/DC50%1/2
$force || $self->action('action', $tryaction, 'object', $object, 'other', $other, 'args', $aargs)
$force
$self->action('action', $tryaction, 'object', $object, 'other', $other, 'args', $aargs)
Truth table66%2/3
A || B
A= $forceB= $self->action('action', $tryaction, 'object', $object, 'other', $other, 'args', $aargs)
| |||||||||||||||||||||||||||||||||||||
| 585 | $force | ||||||||||||||||||||||||||||||||||||
| 586 | || | ||||||||||||||||||||||||||||||||||||
| 587 | $self->action( | ||||||||||||||||||||||||||||||||||||
| 588 | action => $tryaction, | ||||||||||||||||||||||||||||||||||||
| 589 | object => $object, | ||||||||||||||||||||||||||||||||||||
| 590 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 591 | args => $aargs); | ||||||||||||||||||||||||||||||||||||
| 592 | 32 | ▶ | return 0 if (!$check); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||
| 594 | # Relation is allowed, so check to see if already related. | ||||||||||||||||||||||||||||||||||||
| 595 | 31 | ▶ | if (defined($man->{relate_to}{$idobject}{$how})) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||
| 597 | # Already related in this fashion. | ||||||||||||||||||||||||||||||||||||
| 598 | 5 | ▶ | if ($man->{relate_to}{$idobject}{$how} eq $idself) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 599 | # And to the same object, so do nothing (successfully). | ||||||||||||||||||||||||||||||||||||
| 600 | 0 | return 1; | |||||||||||||||||||||||||||||||||||
| 601 | } elsif ($man->unrelate( | ||||||||||||||||||||||||||||||||||||
| 602 | how => $how, | ||||||||||||||||||||||||||||||||||||
| 603 | object => $object, | ||||||||||||||||||||||||||||||||||||
| 604 | force => $force, | ||||||||||||||||||||||||||||||||||||
| 605 | args => { source => 'relate', %$aargs } )) { | ||||||||||||||||||||||||||||||||||||
| 606 | # The unrelate from the previous object succeeded, so | ||||||||||||||||||||||||||||||||||||
| 607 | # invoke myself to try again. | ||||||||||||||||||||||||||||||||||||
| 608 | 5 | return $man->relate(@_); | |||||||||||||||||||||||||||||||||||
| 609 | } else { | ||||||||||||||||||||||||||||||||||||
| 610 | # The unrelate failed, so no-go. | ||||||||||||||||||||||||||||||||||||
| 611 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||
| 616 | # Not currently related to anything in this way. The first | ||||||||||||||||||||||||||||||||||||
| 617 | # thing we do is check the REL_NO_CIRCLE flag. If set, | ||||||||||||||||||||||||||||||||||||
| 618 | # then we make a check to see if a circular reference would | ||||||||||||||||||||||||||||||||||||
| 619 | # result from this. If so, then bomb, as this is assumed to | ||||||||||||||||||||||||||||||||||||
| 620 | # be a logic error in the main program. | ||||||||||||||||||||||||||||||||||||
| 621 | 26 | ▶ | if ($rel->{flags} & REL_NO_CIRCLE) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||
| 623 | # Check to make sure no circular relationship would result from | ||||||||||||||||||||||||||||||||||||
| 624 | # this (i.e. self is already related to object in this manner). | ||||||||||||||||||||||||||||||||||||
| 625 | 26 | ▶ | croak "Relating $idobject to $idself in manner $how would " . | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 626 | "create a circular relationship" | ||||||||||||||||||||||||||||||||||||
| 627 | if ($man->is_related( | ||||||||||||||||||||||||||||||||||||
| 628 | object => $self, | ||||||||||||||||||||||||||||||||||||
| 629 | self => $object, | ||||||||||||||||||||||||||||||||||||
| 630 | how => $how, | ||||||||||||||||||||||||||||||||||||
| 631 | distant => 1)); | ||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||
| 635 | # Do it. | ||||||||||||||||||||||||||||||||||||
| 636 | 25 | $man->{relate_to}{$idobject}{$how} = $idself; | |||||||||||||||||||||||||||||||||||
| 637 | $man->{relate_from}{$idself}{$how} = [] | ||||||||||||||||||||||||||||||||||||
| 638 | 25 | ▶ | if (!defined($man->{relate_from}{$idself}{$how})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 639 | 25 | push @{$man->{relate_from}{$idself}{$how}}, $idobject; | |||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||
| 641 | # Invoke post-relate actions. | ||||||||||||||||||||||||||||||||||||
| 642 | 25 | $self->action( | |||||||||||||||||||||||||||||||||||
| 643 | object => $object, | ||||||||||||||||||||||||||||||||||||
| 644 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 645 | action => $doaction, | ||||||||||||||||||||||||||||||||||||
| 646 | args => $aargs, | ||||||||||||||||||||||||||||||||||||
| 647 | ); | ||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||
| 649 | # Done. | ||||||||||||||||||||||||||||||||||||
| 650 | 25 | 1; | |||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||
| 654 | # Return the object to which this one is related (if any) | ||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||
| 656 | sub related | ||||||||||||||||||||||||||||||||||||
| 657 | 161 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
related: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 658 | 161 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 659 | 161 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||
| 661 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 662 | FetchParams(\@_, \%args, [ | ||||||||||||||||||||||||||||||||||||
| 663 | 161 | ▶ | [ 'req', 'how', undef, sub { $man->has_relation(shift) } ], | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 664 | [ 'req', 'object', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 665 | ] ); | ||||||||||||||||||||||||||||||||||||
| 666 | 161 | my $how = $args{how}; | |||||||||||||||||||||||||||||||||||
| 667 | 161 | my $object = $args{object}; | |||||||||||||||||||||||||||||||||||
| 668 | 161 | my $id = $man->id($object); $object = $man->find($id); | |||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||
| 670 | defined($man->{relate_to}{$id}) && # @*!&$ autovivication | ||||||||||||||||||||||||||||||||||||
| 671 | defined($man->{relate_to}{$id}{$how}) ? | ||||||||||||||||||||||||||||||||||||
| 672 | 161 | ▶ | $man->find($man->{relate_to}{$id}{$how}) : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $man->{"relate_to"}{$id} and defined $man->{"relate_to"}{$id}{$how}
MC/DC100%2/2
defined $man->{"relate_to"}{$id} and defined $man->{"relate_to"}{$id}{$how}
defined $man->{"relate_to"}{$id}
defined $man->{"relate_to"}{$id}{$how}
Truth table100%3/3
A and B
A= defined $man->{"relate_to"}{$id}B= defined $man->{"relate_to"}{$id}{$how}
| |||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||
| 675 | # Return a list of items that are related to a paricular object in a certain | ||||||||||||||||||||||||||||||||||||
| 676 | # way. | ||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||
| 678 | sub related_list | ||||||||||||||||||||||||||||||||||||
| 679 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
related_list: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 680 | 4 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 681 | 4 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||
| 683 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 684 | FetchParams(\@_, \%args, [ | ||||||||||||||||||||||||||||||||||||
| 685 | 4 | ▶ | [ 'req', 'how', undef, sub { $man->has_relation(shift) } ], | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 686 | [ 'req', 'self', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 687 | ] ); | ||||||||||||||||||||||||||||||||||||
| 688 | 4 | my $how = $args{how}; | |||||||||||||||||||||||||||||||||||
| 689 | 4 | my $self = $args{self}; | |||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||
| 691 | # Return list of objects. | ||||||||||||||||||||||||||||||||||||
| 692 | 4 | my $id = $man->id($self); | |||||||||||||||||||||||||||||||||||
| 693 | 4 | my @list = (); | |||||||||||||||||||||||||||||||||||
| 694 | 7 | @list = map { $man->find($_) } @{$man->{relate_from}{$id}{$how}} | |||||||||||||||||||||||||||||||||||
| 695 | if (defined($man->{relate_from}{$id}) | ||||||||||||||||||||||||||||||||||||
| 696 | 4 | ▶ | && defined($man->{relate_from}{$id}{$how})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $man->{"relate_from"}{$id} and defined $man->{"relate_from"}{$id}{$how}
MC/DC0%0/2
defined $man->{"relate_from"}{$id} and defined $man->{"relate_from"}{$id}{$how}
defined $man->{"relate_from"}{$id}
defined $man->{"relate_from"}{$id}{$how}
Truth table33%1/3
A and B
A= defined $man->{"relate_from"}{$id}B= defined $man->{"relate_from"}{$id}{$how}
| |||||||||||||||||||||||||||||||||||||
| 697 | 4 | @list; | |||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||
| 700 | # Check to see if two objects are related. By default, this checks only if | ||||||||||||||||||||||||||||||||||||
| 701 | # two objects are DIRECTLY related. However, specifying the "distant" flag | ||||||||||||||||||||||||||||||||||||
| 702 | # will perform a recursive check to see if the relationship exists indirectly. | ||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||
| 704 | sub is_related | ||||||||||||||||||||||||||||||||||||
| 705 | 46 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
is_related: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 706 | 46 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 707 | 46 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||
| 709 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 710 | FetchParams(\@_, \%args, [ | ||||||||||||||||||||||||||||||||||||
| 711 | 46 | ▶ | [ 'req', 'how', undef, sub { $man->has_relation(shift); } ], | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 712 | [ 'req', 'object', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 713 | [ 'opt', 'self', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 714 | [ 'opt', 'distant', 0, 'boolean' ], | ||||||||||||||||||||||||||||||||||||
| 715 | ] ); | ||||||||||||||||||||||||||||||||||||
| 716 | 46 | my $how = $args{how}; | |||||||||||||||||||||||||||||||||||
| 717 | 46 | my $idobject = $man->id($args{object}); | |||||||||||||||||||||||||||||||||||
| 718 | 46 | my $idself = $man->id($args{self}); | |||||||||||||||||||||||||||||||||||
| 719 | 46 | my $distant = $args{distant}; | |||||||||||||||||||||||||||||||||||
| 720 | 46 | ▶ | return 0 if (!defined($idobject) || !defined($idself)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $idobject and defined $idself
MC/DC0%0/2
defined $idobject and defined $idself
defined $idobject
defined $idself
Truth table33%1/3
A and B
A= defined $idobjectB= defined $idself
| |||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||
| 722 | # If idobject is related to nothing then no relation. | ||||||||||||||||||||||||||||||||||||
| 723 | return 0 if (!defined($man->{relate_to}{$idobject}) | ||||||||||||||||||||||||||||||||||||
| 724 | 46 | ▶ | || !defined($man->{relate_to}{$idobject}{$how})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $man->{"relate_to"}{$idobject} and defined $man->{"relate_to"}{$idobject}{$how}
MC/DC100%2/2
defined $man->{"relate_to"}{$idobject} and defined $man->{"relate_to"}{$idobject}{$how}
defined $man->{"relate_to"}{$idobject}
defined $man->{"relate_to"}{$idobject}{$how}
Truth table100%3/3
A and B
A= defined $man->{"relate_to"}{$idobject}B= defined $man->{"relate_to"}{$idobject}{$how}
| |||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||
| 726 | # If there is a direct relationships, success. | ||||||||||||||||||||||||||||||||||||
| 727 | 20 | ▶ | return 1 if ($man->{relate_to}{$idobject}{$how} eq $idself); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||
| 729 | # If user did not want a distant relationship, then fail. | ||||||||||||||||||||||||||||||||||||
| 730 | 14 | ▶ | return 0 if (!$distant); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||
| 732 | # Otherwise, check what idobject is related to and see if that is | ||||||||||||||||||||||||||||||||||||
| 733 | # related to idself. | ||||||||||||||||||||||||||||||||||||
| 734 | $man->is_related( | ||||||||||||||||||||||||||||||||||||
| 735 | 12 | object => $man->{relate_to}{$idobject}{$how}, | |||||||||||||||||||||||||||||||||||
| 736 | self => $idself, | ||||||||||||||||||||||||||||||||||||
| 737 | how => $how, | ||||||||||||||||||||||||||||||||||||
| 738 | distant => 1); | ||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||
| 741 | # Unrelate an object. | ||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||
| 743 | sub unrelate | ||||||||||||||||||||||||||||||||||||
| 744 | 17 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unrelate: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 745 | 17 | my $man = shift; | |||||||||||||||||||||||||||||||||||
| 746 | 17 | my %args = (); | |||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||
| 748 | # Fetch parameters. | ||||||||||||||||||||||||||||||||||||
| 749 | FetchParams(\@_, \%args, [ | ||||||||||||||||||||||||||||||||||||
| 750 | 17 | ▶ | [ 'req', 'how', undef, sub { $man->has_relation(shift) } ], | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 751 | [ 'req', 'object', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 752 | [ 'opt', 'other', undef, 'any' ], | ||||||||||||||||||||||||||||||||||||
| 753 | [ 'opt', 'args', {}, 'hashref' ], | ||||||||||||||||||||||||||||||||||||
| 754 | ] ); | ||||||||||||||||||||||||||||||||||||
| 755 | 17 | my $how = $args{how}; | |||||||||||||||||||||||||||||||||||
| 756 | 17 | my $object = $args{object}; | |||||||||||||||||||||||||||||||||||
| 757 | 17 | my $other = $args{other}; | |||||||||||||||||||||||||||||||||||
| 758 | 17 | my $aargs = $args{args}; | |||||||||||||||||||||||||||||||||||
| 759 | 17 | my $rel = $man->{relation_def}{$how}; | |||||||||||||||||||||||||||||||||||
| 760 | 17 | my $doaction = "object:on_" . $rel->{unrelate_method}; | |||||||||||||||||||||||||||||||||||
| 761 | 17 | my $tryaction = "object:try_" . $rel->{unrelate_method}; | |||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||
| 763 | # Set the source if not already defined. | ||||||||||||||||||||||||||||||||||||
| 764 | 17 | ▶ | $aargs->{source} = 'direct' if (!defined($aargs->{source})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||
| 766 | # Get ID and check if related. | ||||||||||||||||||||||||||||||||||||
| 767 | 17 | my $idobject = $man->id($object); $object = $man->find($idobject); | |||||||||||||||||||||||||||||||||||
| 768 | 17 | ▶ | if (defined($man->{relate_to}{$idobject}) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $man->{"relate_to"}{$idobject} and defined $man->{"relate_to"}{$idobject}{$how}
MC/DC50%1/2
defined $man->{"relate_to"}{$idobject} and defined $man->{"relate_to"}{$idobject}{$how}
defined $man->{"relate_to"}{$idobject}
defined $man->{"relate_to"}{$idobject}{$how}
Truth table66%2/3
A and B
A= defined $man->{"relate_to"}{$idobject}B= defined $man->{"relate_to"}{$idobject}{$how}
| |||||||||||||||||||||||||||||||||||||
| 769 | && defined($man->{relate_to}{$idobject}{$how})) { | ||||||||||||||||||||||||||||||||||||
| 770 | # Yes it is, so check that object to see if we can unrelate. | ||||||||||||||||||||||||||||||||||||
| 771 | 12 | my $idself = $man->{relate_to}{$idobject}{$how}; | |||||||||||||||||||||||||||||||||||
| 772 | 12 | my $self = $man->find($idself); | |||||||||||||||||||||||||||||||||||
| 773 | 12 | ▶ | $other = $self if (!defined($other)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 774 | 12 | my $check = | |||||||||||||||||||||||||||||||||||
| 775 | $self->action( | ||||||||||||||||||||||||||||||||||||
| 776 | object => $object, | ||||||||||||||||||||||||||||||||||||
| 777 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 778 | action => $tryaction, | ||||||||||||||||||||||||||||||||||||
| 779 | args => { %$aargs }, | ||||||||||||||||||||||||||||||||||||
| 780 | ); | ||||||||||||||||||||||||||||||||||||
| 781 | 12 | ▶ | if ($check) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 782 | # Check succeeded, so unrelate them. | ||||||||||||||||||||||||||||||||||||
| 783 | 12 | delete $man->{relate_to}{$idobject}{$how}; | |||||||||||||||||||||||||||||||||||
| 784 | 12 | my @nlist = (); | |||||||||||||||||||||||||||||||||||
| 785 | 12 | foreach my $item (@{$man->{relate_from}{$idself}{$how}}) { | |||||||||||||||||||||||||||||||||||
| 786 | 30 | ▶ | push @nlist, $item if ($item ne $idobject); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||||||||||
| 788 | 12 | @{$man->{relate_from}{$idself}{$how}} = @nlist; | |||||||||||||||||||||||||||||||||||
| 789 | # Invoke post-unrelate actions. | ||||||||||||||||||||||||||||||||||||
| 790 | 12 | $self->action( | |||||||||||||||||||||||||||||||||||
| 791 | object => $object, | ||||||||||||||||||||||||||||||||||||
| 792 | other => $other, | ||||||||||||||||||||||||||||||||||||
| 793 | action => $doaction, | ||||||||||||||||||||||||||||||||||||
| 794 | args => $aargs, | ||||||||||||||||||||||||||||||||||||
| 795 | ); | ||||||||||||||||||||||||||||||||||||
| 796 | 12 | 1; | |||||||||||||||||||||||||||||||||||
| 797 | } else { | ||||||||||||||||||||||||||||||||||||
| 798 | 0 | 0; | |||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||
| 800 | } else { | ||||||||||||||||||||||||||||||||||||
| 801 | # Not related to anything in this manner. Since the end result | ||||||||||||||||||||||||||||||||||||
| 802 | # is the same as the original condition, we consider this to | ||||||||||||||||||||||||||||||||||||
| 803 | # be success. | ||||||||||||||||||||||||||||||||||||
| 804 | 5 | 1; | |||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||
| 808 | 9 | 1; | |||||||||||||||||||||||||||||||||||