| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2011-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 15 | ▶ | use v5.26; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 15 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 15 | ▶ | use Object::Pad 0.800; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | package Tangence::Meta::Class 0.33; | ||||||||||||||||||||
| 11 | class Tangence::Meta::Class :strict(params); | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 15 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | C<Tangence::Meta::Class> - structure representing one C<Tangence> class | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | This data structure object stores information about one L<Tangence> class. | ||||||||||||||||||||
| 22 | Once constructed and defined, such objects are immutable. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head2 new | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | $class = Tangence::Meta::Class->new( name => $name ) | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | Returns a new instance representing the given name. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 602 | ▶ | field $name :param :reader; | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 39 | 0 | field $defined :reader = 0; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | field @superclasses; | ||||||||||||||||||
|
Subroutine
defined: not called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||
| 42 | 15 | field %methods; | |||||||||||||||||||
| 43 | 15 | field %events; | |||||||||||||||||||
| 44 | 15 | field %properties; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head2 define | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | $class->define( %args ) | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Provides a definition for the class. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =over 8 | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =item methods => HASH | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =item events => HASH | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =item properties => HASH | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Optional HASH references containing metadata about methods, events and | ||||||||||||||||||||
| 61 | properties, as instances of L<Tangence::Meta::Method>, | ||||||||||||||||||||
| 62 | L<Tangence::Meta::Event> or L<Tangence::Meta::Property>. | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =item superclasses => ARRAY | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | Optional ARRAY reference containing superclasses as | ||||||||||||||||||||
| 67 | C<Tangence::Meta::Class> references. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =back | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 61 | ▶ | method define ( %args ) | ||||||||||||||||||
|
Subroutine
define: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||
| 74 | { | ||||||||||||||||||||
| 75 | 61 | ▶ | $defined and croak "Cannot define $name twice"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 61 | $defined++; | |||||||||||||||||||
| 78 | 61 | ▶ | @superclasses = @{ delete $args{superclasses} // [] }; | ||||||||||||||||||
|
Condition50%1/2
delete $args{"superclasses"} // []
MC/DC0%0/1
delete $args{"superclasses"} // []
delete $args{"superclasses"}
Truth table50%1/2
A
A= delete $args{"superclasses"}
| |||||||||||||||||||||
| 79 | 61 | ▶ | %methods = %{ delete $args{methods} // {} }; | ||||||||||||||||||
|
Condition50%1/2
delete $args{"methods"} // {}
MC/DC0%0/1
delete $args{"methods"} // {}
delete $args{"methods"}
Truth table50%1/2
A
A= delete $args{"methods"}
| |||||||||||||||||||||
| 80 | 61 | ▶ | %events = %{ delete $args{events} // {} }; | ||||||||||||||||||
|
Condition50%1/2
delete $args{"events"} // {}
MC/DC0%0/1
delete $args{"events"} // {}
delete $args{"events"}
Truth table50%1/2
A
A= delete $args{"events"}
| |||||||||||||||||||||
| 81 | 61 | ▶ | %properties = %{ delete $args{properties} // {} }; | ||||||||||||||||||
|
Condition50%1/2
delete $args{"properties"} // {}
MC/DC0%0/1
delete $args{"properties"} // {}
delete $args{"properties"}
Truth table50%1/2
A
A= delete $args{"properties"}
| |||||||||||||||||||||
| 82 | 15 | } | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 ACCESSORS | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head2 defined | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | $defined = $class->defined | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Returns true if a definintion for the class has been provided using C<define>. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 name | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | $name = $class->name | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Returns the name of the class | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head2 perlname | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | $perlname = $class->perlname | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Returns the perl name of the class. This will be the Tangence name, with dots | ||||||||||||||||||||
| 109 | replaced by double colons (C<::>). | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | method perlname | ||||||||||||||||||||
| 114 | 571 | ▶ | { | ||||||||||||||||||
|
Subroutine
perlname: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 115 | 571 | ( my $perlname = $self->name ) =~ s{\.}{::}g; # s///rg in 5.14 | |||||||||||||||||||
| 116 | 571 | return $perlname; | |||||||||||||||||||
| 117 | 15 | } | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head2 direct_superclasses | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | @superclasses = $class->direct_superclasses | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | Return the direct superclasses in a list of C<Tangence::Meta::Class> | ||||||||||||||||||||
| 124 | references. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | method direct_superclasses | ||||||||||||||||||||
| 129 | 21 | ▶ | { | ||||||||||||||||||
|
Subroutine
direct_superclasses: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 130 | 21 | ▶ | $defined or croak "$name is not yet defined"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 21 | return @superclasses; | |||||||||||||||||||
| 132 | 15 | } | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 direct_methods | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | $methods = $class->direct_methods | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Return the methods that this class directly defines (rather than inheriting | ||||||||||||||||||||
| 139 | from superclasses) as a HASH reference mapping names to | ||||||||||||||||||||
| 140 | L<Tangence::Meta::Method> instances. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | method direct_methods | ||||||||||||||||||||
| 145 | 53 | ▶ | { | ||||||||||||||||||
|
Subroutine
direct_methods: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 146 | 53 | ▶ | $defined or croak "$name is not yet defined"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 53 | return { %methods }; | |||||||||||||||||||
| 148 | 15 | } | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 direct_events | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | $events = $class->direct_events | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Return the events that this class directly defines (rather than inheriting | ||||||||||||||||||||
| 155 | from superclasses) as a HASH reference mapping names to | ||||||||||||||||||||
| 156 | L<Tangence::Meta::Event> instances. | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | method direct_events | ||||||||||||||||||||
| 161 | 150 | ▶ | { | ||||||||||||||||||
|
Subroutine
direct_events: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 162 | 150 | ▶ | $defined or croak "$name is not yet defined"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 150 | return { %events }; | |||||||||||||||||||
| 164 | 15 | } | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head2 direct_properties | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | $properties = $class->direct_properties | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Return the properties that this class directly defines (rather than inheriting | ||||||||||||||||||||
| 171 | from superclasses) as a HASH reference mapping names to | ||||||||||||||||||||
| 172 | L<Tangence::Meta::Property> instances. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | method direct_properties | ||||||||||||||||||||
| 177 | 821 | ▶ | { | ||||||||||||||||||
|
Subroutine
direct_properties: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 178 | 821 | ▶ | $defined or croak "$name is not yet defined"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 821 | return { %properties }; | |||||||||||||||||||
| 180 | 15 | } | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head1 AGGREGATE ACCESSORS | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | The following accessors inspect the full inheritance tree of this class and | ||||||||||||||||||||
| 185 | all its superclasses | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head2 superclasses | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | @superclasses = $class->superclasses | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Return all the superclasses in a list of unique C<Tangence::Meta::Class> | ||||||||||||||||||||
| 194 | references. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | method superclasses | ||||||||||||||||||||
| 199 | 627 | ▶ | { | ||||||||||||||||||
|
Subroutine
superclasses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 200 | # This algorithm doesn't have to be particularly good, C3 or whatever. | ||||||||||||||||||||
| 201 | # We're not really forming a search order, mearly uniq'ifying | ||||||||||||||||||||
| 202 | 627 | my %seen; | |||||||||||||||||||
| 203 | 627 | return grep { !$seen{$_}++ } map { $_, $_->superclasses } @superclasses; | |||||||||||||||||||
| 204 | 15 | } | |||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =head2 methods | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | $methods = $class->methods | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Return all the methods available to this class as a HASH reference mapping | ||||||||||||||||||||
| 211 | names to L<Tangence::Meta::Method> instances. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | method methods | ||||||||||||||||||||
| 216 | 16 | ▶ | { | ||||||||||||||||||
|
Subroutine
methods: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||
| 217 | 16 | my %methods; | |||||||||||||||||||
| 218 | 16 | foreach ( $self, $self->superclasses ) { | |||||||||||||||||||
| 219 | 32 | my $m = $_->direct_methods; | |||||||||||||||||||
| 220 | 32 | ▶ | $methods{$_} ||= $m->{$_} for keys %$m; | ||||||||||||||||||
|
Condition33%1/3
$methods{$_} ||= $m->{$_}
MC/DC0%0/2
$methods{$_} ||= $m->{$_}
$methods{$_}
$m->{$_}
Truth table33%1/3
A ||= B
A= $methods{$_}B= $m->{$_}
| |||||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | 16 | return \%methods; | |||||||||||||||||||
| 223 | 15 | } | |||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 method | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | $method = $class->method( $name ) | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | Return the named method as a L<Tangence::Meta::Method> instance, or C<undef> | ||||||||||||||||||||
| 230 | if no such method exists. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 8 | ▶ | method method ( $name ) | ||||||||||||||||||
|
Subroutine
method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 235 | { | ||||||||||||||||||||
| 236 | 8 | return $self->methods->{$name}; | |||||||||||||||||||
| 237 | 15 | } | |||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =head2 events | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | $events = $class->events | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | Return all the events available to this class as a HASH reference mapping | ||||||||||||||||||||
| 244 | names to L<Tangence::Meta::Event> instances. | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | method events | ||||||||||||||||||||
| 249 | 64 | ▶ | { | ||||||||||||||||||
|
Subroutine
events: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||
| 250 | 64 | my %events; | |||||||||||||||||||
| 251 | 64 | foreach ( $self, $self->superclasses ) { | |||||||||||||||||||
| 252 | 129 | my $e = $_->direct_events; | |||||||||||||||||||
| 253 | 129 | ▶ | $events{$_} ||= $e->{$_} for keys %$e; | ||||||||||||||||||
|
Condition33%1/3
$events{$_} ||= $e->{$_}
MC/DC0%0/2
$events{$_} ||= $e->{$_}
$events{$_}
$e->{$_}
Truth table33%1/3
A ||= B
A= $events{$_}B= $e->{$_}
| |||||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | 64 | return \%events; | |||||||||||||||||||
| 256 | 15 | } | |||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | =head2 event | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | $event = $class->event( $name ) | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | Return the named event as a L<Tangence::Meta::Event> instance, or C<undef> if | ||||||||||||||||||||
| 263 | no such event exists. | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 13 | ▶ | method event ( $name ) | ||||||||||||||||||
|
Subroutine
event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 268 | { | ||||||||||||||||||||
| 269 | 13 | return $self->events->{$name}; | |||||||||||||||||||
| 270 | 15 | } | |||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =head2 properties | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | $properties = $class->properties | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | Return all the properties available to this class as a HASH reference mapping | ||||||||||||||||||||
| 277 | names to L<Tangence::Meta::Property> instances. | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | method properties | ||||||||||||||||||||
| 282 | 379 | ▶ | { | ||||||||||||||||||
|
Subroutine
properties: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||
| 283 | 379 | my %properties; | |||||||||||||||||||
| 284 | 379 | foreach ( $self, $self->superclasses ) { | |||||||||||||||||||
| 285 | 760 | my $p = $_->direct_properties; | |||||||||||||||||||
| 286 | 760 | ▶ | $properties{$_} ||= $p->{$_} for keys %$p; | ||||||||||||||||||
|
Condition33%1/3
$properties{$_} ||= $p->{$_}
MC/DC0%0/2
$properties{$_} ||= $p->{$_}
$properties{$_}
$p->{$_}
Truth table33%1/3
A ||= B
A= $properties{$_}B= $p->{$_}
| |||||||||||||||||||||
| 287 | } | ||||||||||||||||||||
| 288 | 379 | return \%properties; | |||||||||||||||||||
| 289 | 15 | } | |||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | =head2 property | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | $property = $class->property( $name ) | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | Return the named property as a L<Tangence::Meta::Property> instance, or | ||||||||||||||||||||
| 296 | C<undef> if no such property exists. | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | =cut | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | 138 | ▶ | method property ( $name ) | ||||||||||||||||||
|
Subroutine
property: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 301 | { | ||||||||||||||||||||
| 302 | 138 | return $self->properties->{$name}; | |||||||||||||||||||
| 303 | 15 | } | |||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | =head1 AUTHOR | ||||||||||||||||||||
| 306 | |||||||||||||||||||||
| 307 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | =cut | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | 15 | 0x55AA; | |||||||||||||||||||