| 1 | #================================ Members.pm ================================= | ||||||||||
| 2 | # Filename: Members.pm | ||||||||||
| 3 | # Description: Manage a Members file. | ||||||||||
| 4 | # Original Author: Dale Amon | ||||||||||
| 5 | # Revised by: $Author: amon $ | ||||||||||
| 6 | # Date: $Date: 2008-08-28 23:26:17 $ | ||||||||||
| 7 | # Version: $Revision: 1.5 $ | ||||||||||
| 8 | # License: LGPL 2.1, Perl Artistic or BSD | ||||||||||
| 9 | # | ||||||||||
| 10 | #============================================================================= | ||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | package Document::Members; | ||||||||||
| 14 | 1 | ▶ | use vars qw{@ISA}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | @ISA = qw( UNIVERSAL ); | |||||||||
| 16 | |||||||||||
| 17 | #============================================================================= | ||||||||||
| 18 | # Class Methods | ||||||||||
| 19 | #============================================================================= | ||||||||||
| 20 | |||||||||||
| 21 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 22 | 0 | my ($class) = @_; | |||||||||
| 23 | 0 | my $self = bless {}, $class; | |||||||||
| 24 | 0 | @$self{'toc','lazy','cnt','mark','first'} = ({},1,0,[],[]); | |||||||||
| 25 | 0 | return $self; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | #============================================================================= | ||||||||||
| 29 | # Object Methods | ||||||||||
| 30 | #============================================================================= | ||||||||||
| 31 | # Predicate operators | ||||||||||
| 32 | #----------------------------------------------------------------------------- | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub isMember { | ||||||||
|
Subroutine
isMember: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 35 | 0 | my ($self) = shift; | |||||||||
| 36 | 0 | my ($member) = @_; | |||||||||
| 37 | 0 | ▶ | defined $member || (return 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | return ($self->_havemember (@_)); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | #----------------------------------------------------------------------------- | ||||||||||
| 42 | # Key listing operators | ||||||||||
| 43 | #----------------------------------------------------------------------------- | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub members {return (shift->_listmembers);} | ||||||||
|
Subroutine
members: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | #----------------------------------------------------------------------------- | ||||||||||
| 48 | # Add operators | ||||||||||
| 49 | #----------------------------------------------------------------------------- | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub addMembers {my $s=shift; | ||||||||
|
Subroutine
addMembers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 0 | my $a=$s->{'toc'}; @$a{@_}=(); $s->_l8r; return 1;} | |||||||||
| 53 | |||||||||||
| 54 | #----------------------------------------------------------------------------- | ||||||||||
| 55 | # Remove operators | ||||||||||
| 56 | #----------------------------------------------------------------------------- | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub removeMembers { | ||||||||
|
Subroutine
removeMembers: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | 0 | ▶ | foreach my $m (@_) {($self->_havemember ($m)) && ($self->_killmember ($m));} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $self->_l8r; | |||||||||
| 62 | 0 | return 1; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | #----------------------------------------------------------------------------- | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub initmark { | ||||||||
|
Subroutine
initmark: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | 0 | $self->_lazy; | |||||||||
| 70 | 0 | @{$self->{'mark'}} = @{$self->{'first'}}; | |||||||||
| 71 | 0 | return (@{$self->{'mark'}}); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | #----------------------------------------------------------------------------- | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub mark {return (@{+shift->{'mark'}});} | ||||||||
|
Subroutine
mark: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 77 | |||||||||||
| 78 | #----------------------------------------------------------------------------- | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub _selmember { | ||||||||
|
Subroutine
_selmember: not called CC=3 SCAR=24.8
| |||||||||||
| 81 | 0 | my ($self,$item) = @_; | |||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | if (!defined $item) {return (undef);} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | ▶ | if (!$self->_havemember($item)) {return ($item);} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | return (undef); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | #============================================================================= | ||||||||||
| 89 | # Internal Methods | ||||||||||
| 90 | #============================================================================= | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | sub _l8r {shift->{'lazy'}=1;} | ||||||||
|
Subroutine
_l8r: not called CC=1 SCAR=6.9
| |||||||||||
| 93 | |||||||||||
| 94 | #----------------------------------------------------------------------------- | ||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub _lazy { | ||||||||
|
Subroutine
_lazy: not called CC=2 SCAR=17.9
| |||||||||||
| 97 | 0 | my $self = shift; | |||||||||
| 98 | 0 | ▶ | $self->{'lazy'} || return 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | 0 | $self->{'cnt'} = $self->_cntmembers; | |||||||||
| 101 | 0 | $self->{'first'}[0] = $self->_1stmember; | |||||||||
| 102 | 0 | $self->{'lazy'} = 0; | |||||||||
| 103 | 0 | return 1; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | #============================================================================= | ||||||||||
| 107 | # The following are a set of internal primitive operations on the toc data | ||||||||||
| 108 | # structure. They are used by external methods which also do arg checking. | ||||||||||
| 109 | #============================================================================= | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub _listmembers {return (sort keys %{shift->{'toc'}});} | ||||||||
|
Subroutine
_listmembers: not called CC=1 SCAR=6.9
| |||||||||||
| 112 | 0 | ▶ | sub _cntmembers {return ($#{ [keys %{shift->{'toc'}}] } + 1);} | ||||||||
|
Subroutine
_cntmembers: not called CC=1 SCAR=6.9
| |||||||||||
| 113 | 0 | ▶ | sub _1stmember {my $s=shift; | ||||||||
|
Subroutine
_1stmember: not called CC=2 SCAR=17.9
| |||||||||||
| 114 | 0 | ▶ | return ($s->{'cnt'} > 0) ? ($s->_listmembers)[0] : undef;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | |||||||||||
| 116 | #----------------------------------------------------------------------------- | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub _initmember { shift->{'toc'}->{+shift} = undef;} | ||||||||
|
Subroutine
_initmember: not called CC=1 SCAR=6.9
| |||||||||||
| 119 | 0 | ▶ | sub _killmember { delete shift->{'toc'}->{+shift};} | ||||||||
|
Subroutine
_killmember: not called CC=1 SCAR=6.9
| |||||||||||
| 120 | 0 | ▶ | sub _havemember {return exists shift->{'toc'}->{+shift};} | ||||||||
|
Subroutine
_havemember: not called CC=1 SCAR=6.9
| |||||||||||
| 121 | |||||||||||
| 122 | #============================================================================= | ||||||||||
| 123 | # Pod Documentation | ||||||||||
| 124 | #============================================================================= | ||||||||||
| 125 | # You may extract and format the documention section with the 'perldoc' cmd. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 NAME | ||||||||||
| 128 | |||||||||||
| 129 | Document::Members - Manage a Members file. | ||||||||||
| 130 | |||||||||||
| 131 | =head1 SYNOPSIS | ||||||||||
| 132 | |||||||||||
| 133 | use Document::Members; | ||||||||||
| 134 | $obj = Document::Members->new | ||||||||||
| 135 | |||||||||||
| 136 | @members = $obj->members | ||||||||||
| 137 | $flg = $obj->isMember ($member) | ||||||||||
| 138 | $flg = $obj->addMembers (@members) | ||||||||||
| 139 | $flg = $obj->removeMembers (@members) | ||||||||||
| 140 | @curselection = $obj->initmark | ||||||||||
| 141 | @listmark = $obj->mark | ||||||||||
| 142 | |||||||||||
| 143 | =head1 Inheritance | ||||||||||
| 144 | |||||||||||
| 145 | UNIVERSAL | ||||||||||
| 146 | |||||||||||
| 147 | =head1 Description | ||||||||||
| 148 | |||||||||||
| 149 | This Class manages a sorted hierarchical hash of set members. It manages | ||||||||||
| 150 | addition and subtraction of members; tests for set membership and does the | ||||||||||
| 151 | book-keeping so that 'foreach' operations may be done on the set or subsets | ||||||||||
| 152 | of the members. The sorting required for stepping through elements in | ||||||||||
| 153 | alphabetic order is only done when necessary (an internal flag has been set | ||||||||||
| 154 | to indicate the changes have been made) and at the last possible moment | ||||||||||
| 155 | (lazy evaluation). | ||||||||||
| 156 | |||||||||||
| 157 | For the most part this class will be used as an Abstract Superclass. It is | ||||||||||
| 158 | structured so that elements are defined not by a single lookup key, but by a | ||||||||||
| 159 | path list of keys which lead from the root members hash to a terminal element. | ||||||||||
| 160 | This allows a foreach operation to traverse the entire structure via depth | ||||||||||
| 161 | first recursive search. In this class there is only the first level. But the | ||||||||||
| 162 | structure is all here for subclasses to chain, override and extend. | ||||||||||
| 163 | |||||||||||
| 164 | For example, a subclass implementing a three level hierarchy might have | ||||||||||
| 165 | address marks something like this: | ||||||||||
| 166 | |||||||||||
| 167 | (Book1, Page1, Paragraph5) | ||||||||||
| 168 | |||||||||||
| 169 | where Book1 is the top level hash key. The hash attached to Book1 is | ||||||||||
| 170 | accessed with the Page1 second level hash key; the hash attached to Page1 | ||||||||||
| 171 | can then finally be accessed with Paragraph5 to return the data stored at | ||||||||||
| 172 | the leaf (terminal) node. | ||||||||||
| 173 | |||||||||||
| 174 | =head1 Examples | ||||||||||
| 175 | |||||||||||
| 176 | use Document::Members; | ||||||||||
| 177 | my $foo = Document::Members->new (); | ||||||||||
| 178 | my $flg = $obj->addMembers ("one","two","three","four"); | ||||||||||
| 179 | $flg = $obj->removeMembers ("two","three"); | ||||||||||
| 180 | $flg = $obj->isMember ("two"); | ||||||||||
| 181 | $flg = $obj->isMember ("one"); | ||||||||||
| 182 | my @members = $obj->members; | ||||||||||
| 183 | my @curselection = $obj->initmark; | ||||||||||
| 184 | my @listmark = $obj->mark; | ||||||||||
| 185 | |||||||||||
| 186 | =head1 Class Variables | ||||||||||
| 187 | |||||||||||
| 188 | None. | ||||||||||
| 189 | |||||||||||
| 190 | =head1 Instance Variables | ||||||||||
| 191 | |||||||||||
| 192 | toc Pointer to the member hash. | ||||||||||
| 193 | mark Array containing the current 'address mark'. | ||||||||||
| 194 | first Array containing the 'address mark' of the first member | ||||||||||
| 195 | as of the last key sort. | ||||||||||
| 196 | cnt Number of elements in the member hash. | ||||||||||
| 197 | |||||||||||
| 198 | =head1 Class Methods | ||||||||||
| 199 | |||||||||||
| 200 | =over 4 | ||||||||||
| 201 | |||||||||||
| 202 | =item B<$obj = Document::Members-E<gt>new> | ||||||||||
| 203 | |||||||||||
| 204 | Create a blank members object and return true. | ||||||||||
| 205 | |||||||||||
| 206 | =head1 Instance Methods | ||||||||||
| 207 | |||||||||||
| 208 | =item B<$flg = $obj-E<gt>addMembers (@members)> | ||||||||||
| 209 | |||||||||||
| 210 | Add a new member name to the hash, with an undef value. Members is a list | ||||||||||
| 211 | of member names and may be empty. True if it succeeds. | ||||||||||
| 212 | |||||||||||
| 213 | Override if you have to use the value. Sets lazy eval bit. | ||||||||||
| 214 | |||||||||||
| 215 | =item B<@curselection = $obj-E<gt>initmark> | ||||||||||
| 216 | |||||||||||
| 217 | Copy the contents of the first selection (a list) to the current selection | ||||||||||
| 218 | (also a list). It is done this way so that subclasses may have arbitrarily | ||||||||||
| 219 | long selection marks; the length can even vary from one selection to the next. | ||||||||||
| 220 | (eg, [page, title, (category list)] in a Toc subclass) | ||||||||||
| 221 | |||||||||||
| 222 | =item B<$flg = $obj-E<gt>isMember ($member)> | ||||||||||
| 223 | |||||||||||
| 224 | True if $member exists. | ||||||||||
| 225 | |||||||||||
| 226 | =item B<@listmark = $obj-E<gt>mark> | ||||||||||
| 227 | |||||||||||
| 228 | Return the current selection mark array. The list mark contains only one | ||||||||||
| 229 | element in this class, but this could be extended in child classes. | ||||||||||
| 230 | |||||||||||
| 231 | =item B<@members = $obj-E<gt>members> | ||||||||||
| 232 | |||||||||||
| 233 | Return a list of the members in the hash. List may be empty. | ||||||||||
| 234 | |||||||||||
| 235 | =item B<$flg = $obj-E<gt>removeMembers (@members)> | ||||||||||
| 236 | |||||||||||
| 237 | Remove a member name from the hash. Returns true if the operation succeeds. | ||||||||||
| 238 | |||||||||||
| 239 | =back 4 | ||||||||||
| 240 | |||||||||||
| 241 | =head1 Private Class Methods | ||||||||||
| 242 | |||||||||||
| 243 | None. | ||||||||||
| 244 | |||||||||||
| 245 | =head1 Private Instance Methods | ||||||||||
| 246 | |||||||||||
| 247 | =over 4 | ||||||||||
| 248 | |||||||||||
| 249 | =item B<$membername = $obj-E<gt>_1stmember > | ||||||||||
| 250 | |||||||||||
| 251 | Returns the first element of the current sorted key list or undef if there | ||||||||||
| 252 | the list is empty. | ||||||||||
| 253 | |||||||||||
| 254 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 255 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 256 | you are doing. | ||||||||||
| 257 | |||||||||||
| 258 | =item B<$cnt = $obj-E<gt>_cntmembers> | ||||||||||
| 259 | |||||||||||
| 260 | Returns a current count of the number of keys that are in the members hash. | ||||||||||
| 261 | |||||||||||
| 262 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 263 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 264 | you are doing. | ||||||||||
| 265 | |||||||||||
| 266 | =item B<$obj-E<gt>_l8r> | ||||||||||
| 267 | |||||||||||
| 268 | Set the lazy evaluation bit so we'll do things l8r. Mananna. | ||||||||||
| 269 | |||||||||||
| 270 | This is an internal primitive operation on the members data. It assumes you | ||||||||||
| 271 | know what you are doing. You probably can't do much damage with this one | ||||||||||
| 272 | other than to cause the lazy evaluation to happen more often than it should. | ||||||||||
| 273 | |||||||||||
| 274 | =item B<$dideval = $obj-E<gt>_lazy> | ||||||||||
| 275 | |||||||||||
| 276 | SUBCLASS MAY CHAIN. If the lazy evaluation bit is set, do evaluations and | ||||||||||
| 277 | then clear it. Returns true if subclass should carry out it's own lazy | ||||||||||
| 278 | evaluation. | ||||||||||
| 279 | |||||||||||
| 280 | This is an internal primitive operation on the members data. It assumes you | ||||||||||
| 281 | know what you are doing because if you screw up the lazy evaluation you could | ||||||||||
| 282 | create some really subtle bugs. | ||||||||||
| 283 | |||||||||||
| 284 | =item B<$exists = $obj-E<gt>_havemember ($membername)> | ||||||||||
| 285 | |||||||||||
| 286 | It returns true if the specified member exists in the top level set. | ||||||||||
| 287 | |||||||||||
| 288 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 289 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 290 | you are doing. | ||||||||||
| 291 | |||||||||||
| 292 | =item B<$obj-E<gt>_initmember ($membername)> | ||||||||||
| 293 | |||||||||||
| 294 | Set the value of the specified set member to undef. | ||||||||||
| 295 | |||||||||||
| 296 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 297 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 298 | you are doing. | ||||||||||
| 299 | |||||||||||
| 300 | =item B<$obj-E<gt>_killmember ($membername)> | ||||||||||
| 301 | |||||||||||
| 302 | Remove the member from the set if it exists. | ||||||||||
| 303 | |||||||||||
| 304 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 305 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 306 | you are doing. | ||||||||||
| 307 | |||||||||||
| 308 | =item B<@keys = $obj-E<gt>_listmembers> | ||||||||||
| 309 | |||||||||||
| 310 | Returns a current sorted list of keys that are in the members hash. | ||||||||||
| 311 | |||||||||||
| 312 | This is an internal primitive operation on the members data.. It is used by | ||||||||||
| 313 | child methods which also do the argument checking. It assumes you know what | ||||||||||
| 314 | you are doing. | ||||||||||
| 315 | |||||||||||
| 316 | =item B<$membername = $obj-E<gt>_selmember ($membername) > | ||||||||||
| 317 | |||||||||||
| 318 | If $membername exists in the set, return it as is. If it does not exist, | ||||||||||
| 319 | return undef instead. This routine does argument checking, but is primarily | ||||||||||
| 320 | here for the convenience of subclasses. | ||||||||||
| 321 | |||||||||||
| 322 | =back 4 | ||||||||||
| 323 | |||||||||||
| 324 | =head1 KNOWN BUGS | ||||||||||
| 325 | |||||||||||
| 326 | See TODO. | ||||||||||
| 327 | |||||||||||
| 328 | =head1 SEE ALSO | ||||||||||
| 329 | |||||||||||
| 330 | None. | ||||||||||
| 331 | |||||||||||
| 332 | =head1 AUTHOR | ||||||||||
| 333 | |||||||||||
| 334 | Dale Amon <amon@vnl.com> | ||||||||||
| 335 | |||||||||||
| 336 | =cut | ||||||||||
| 337 | |||||||||||
| 338 | #============================================================================= | ||||||||||
| 339 | # CVS HISTORY | ||||||||||
| 340 | #============================================================================= | ||||||||||
| 341 | # $Log: Members.pm,v $ | ||||||||||
| 342 | # Revision 1.5 2008-08-28 23:26:17 amon | ||||||||||
| 343 | # perldoc section regularization. | ||||||||||
| 344 | # | ||||||||||
| 345 | # Revision 1.4 2008-08-13 21:04:32 amon | ||||||||||
| 346 | # Third phase of reformatting to current standard. | ||||||||||
| 347 | # | ||||||||||
| 348 | # Revision 1.3 2008-08-13 20:56:53 amon | ||||||||||
| 349 | # Second phase of reformatting to current standard. | ||||||||||
| 350 | # | ||||||||||
| 351 | # Revision 1.2 2008-08-13 14:50:02 amon | ||||||||||
| 352 | # First phase of reformatting to current standard. | ||||||||||
| 353 | # | ||||||||||
| 354 | # Revision 1.1.1.1 2004-08-30 13:28:42 amon | ||||||||||
| 355 | # Manages a Document directory | ||||||||||
| 356 | # | ||||||||||
| 357 | # 20040813 Dale Amon <amon@islandone.org> | ||||||||||
| 358 | # Moved to Document:: from Archivist:: | ||||||||||
| 359 | # to make it easier to enforce layers. | ||||||||||
| 360 | # | ||||||||||
| 361 | # 20030110 Dale Amon <amon@vnl.com> | ||||||||||
| 362 | # Added selection mark support | ||||||||||
| 363 | # | ||||||||||
| 364 | # 20021222 Dale Amon <amon@vnl.com> | ||||||||||
| 365 | # Created as a superclass to Toc.pm | ||||||||||
| 366 | # | ||||||||||
| 367 | 1 | 1; | |||||||||