| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package KiokuDB::TypeMap; | ||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 28 | ▶ | use KiokuDB::TypeMap::Entry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use KiokuDB::TypeMap::Entry::Alias; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 28 | with qw(KiokuDB::Role::TypeMap); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 28 | has [qw(entries isa_entries)] => ( | |||||||||||||||||||||||||||
| 17 | #isa => "HashRef[KiokuDB::TypeMap::Entry|KiokuDB::TypeMap::Entry::Alias]", # dog slow regex | ||||||||||||||||||||||||||||
| 18 | is => "ro", | ||||||||||||||||||||||||||||
| 19 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 331 | ▶ | sub _build_entries { +{} } | ||||||||||||||||||||||||||
|
Subroutine
_build_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 121 | ▶ | sub _build_isa_entries { +{} } | ||||||||||||||||||||||||||
|
Subroutine
_build_isa_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 28 | has [qw(all_entries all_isa_entries)] => ( | |||||||||||||||||||||||||||
| 26 | #isa => "HashRef[KiokuDB::TypeMap::Entry|KiokuDB::TypeMap::Entry::Alias]", # dog slow regex | ||||||||||||||||||||||||||||
| 27 | is => "ro", | ||||||||||||||||||||||||||||
| 28 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 28 | has all_isa_entry_classes => ( | |||||||||||||||||||||||||||
| 32 | isa => "ArrayRef[Str]", | ||||||||||||||||||||||||||||
| 33 | is => "ro", | ||||||||||||||||||||||||||||
| 34 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 28 | has includes => ( | |||||||||||||||||||||||||||
| 38 | isa => "ArrayRef[KiokuDB::TypeMap]", | ||||||||||||||||||||||||||||
| 39 | is => "ro", | ||||||||||||||||||||||||||||
| 40 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 477 | ▶ | sub _build_includes { [] } | ||||||||||||||||||||||||||
|
Subroutine
_build_includes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 28 | my %loaded; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub resolve { | ||||||||||||||||||||||||||||
| 48 | 850 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # if we're linking the class might not be loaded yet | ||||||||||||||||||||||||||||
| 51 | 850 | ▶ | unless ( $loaded{$class}++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 101 | ( my $pmfile = $class . ".pm" ) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | try { | ||||||||||||||||||||||||||||
| 55 | 101 | ▶ | require $pmfile; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | } catch { | ||||||||||||||||||||||||||||
| 57 | 40 | ▶ | croak $_ unless /Can't locate \Q$pmfile\E in \@INC/; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 101 | }; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # if this is an anonymous class, redo the lookup using a single named | ||||||||||||||||||||||||||||
| 62 | # ancestor | ||||||||||||||||||||||||||||
| 63 | 850 | ▶ | if ( my $meta = Class::MOP::get_metaclass_by_name($class) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 339 | ▶ | if ( $meta->is_anon_class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 35 | my $ancestor = $meta; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | search: { | ||||||||||||||||||||||||||||
| 68 | 35 | my @super = $ancestor->superclasses; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 35 | ▶ | if ( @super == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 35 | $ancestor = Class::MOP::get_metaclass_by_name($super[0]); | |||||||||||||||||||||||||||
| 72 | 35 | ▶ | if ( $ancestor->is_anon_class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | redo search; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 0 | croak "Cannot resolve anonymous class with multiple inheritence: $class"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 35 | return $self->resolve( $ancestor->name ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 815 | ▶ | if ( my $entry = $self->all_entries->{$class} || $self->all_isa_entries->{$class} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->all_entries->{$class} || $self->all_isa_entries->{$class}
MC/DC100%2/2
$self->all_entries->{$class} || $self->all_isa_entries->{$class}
$self->all_entries->{$class}
$self->all_isa_entries->{$class}
Truth table100%3/3
A || B
A= $self->all_entries->{$class}B= $self->all_isa_entries->{$class}
| |||||||||||||||||||||||||||||
| 86 | 349 | return $self->resolve_entry( $entry ); | |||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | 466 | foreach my $superclass ( @{ $self->all_isa_entry_classes } ) { | |||||||||||||||||||||||||||
| 89 | 2222 | ▶ | if ( $class->isa($superclass) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 90 | return $self->resolve_entry( $self->all_isa_entries->{$superclass} ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 376 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub resolve_entry { | ||||||||||||||||||||||||||||
| 99 | 439 | ▶ | my ( $self, $entry ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resolve_entry: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 439 | ▶ | if ( $entry->isa("KiokuDB::TypeMap::Entry::Alias") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 4 | return $self->resolve( $entry->to ); | |||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||
| 104 | 435 | return $entry; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub BUILD { | ||||||||||||||||||||||||||||
| 109 | 558 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # verify that there are no conflicting internal definitions | ||||||||||||||||||||||||||||
| 112 | 558 | my $reg = $self->entries; | |||||||||||||||||||||||||||
| 113 | 558 | foreach my $key ( keys %{ $self->isa_entries } ) { | |||||||||||||||||||||||||||
| 114 | 514 | ▶ | if ( exists $reg->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | croak "isa entry $key already present in plain entries"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Verify that there are no conflicts between the includesd type maps | ||||||||||||||||||||||||||||
| 120 | 557 | my %seen; | |||||||||||||||||||||||||||
| 121 | 557 | foreach my $map ( @{ $self->includes } ) { | |||||||||||||||||||||||||||
| 122 | 424 | foreach my $key ( keys %{ $map->all_entries } ) { | |||||||||||||||||||||||||||
| 123 | 487 | ▶ | if ( $seen{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | croak "entry $key found in $map conflicts with $seen{$key}"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 485 | $seen{$key} = $map; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 422 | foreach my $key ( keys %{ $map->all_isa_entries } ) { | |||||||||||||||||||||||||||
| 131 | 477 | ▶ | if ( $seen{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | croak "isa entry $key found in $map conflicts with $seen{$key}"; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 476 | $seen{$key} = $map; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _build_all_entries { | ||||||||||||||||||||||||||||
| 141 | 553 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_all_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | return { | ||||||||||||||||||||||||||||
| 144 | 964 | map { %$_ } ( | |||||||||||||||||||||||||||
| 145 | 553 | ( map { $_->all_entries } @{ $self->includes } ), | |||||||||||||||||||||||||||
| 146 | $self->entries, | ||||||||||||||||||||||||||||
| 147 | ), | ||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub _build_all_isa_entries { | ||||||||||||||||||||||||||||
| 152 | 549 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_all_isa_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | return { | ||||||||||||||||||||||||||||
| 155 | 960 | map { %$_ } ( | |||||||||||||||||||||||||||
| 156 | 549 | ( map { $_->all_isa_entries } @{ $self->includes } ), | |||||||||||||||||||||||||||
| 157 | $self->isa_entries, | ||||||||||||||||||||||||||||
| 158 | ), | ||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _build_all_isa_entry_classes { | ||||||||||||||||||||||||||||
| 163 | 127 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_all_isa_entry_classes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | return [ | ||||||||||||||||||||||||||||
| 166 | 734 | sort { !$a->isa($b) <=> !$b->isa($a) } # least derived first | |||||||||||||||||||||||||||
| 167 | 127 | keys %{ $self->all_isa_entries } | |||||||||||||||||||||||||||
| 168 | ]; | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 28 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 28 | __PACKAGE__ | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =pod | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 NAME | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | KiokuDB::TypeMap - Class to collapsing/expanding logic. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | use KiokuDB::TypeMap; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | KiokuDB::TypeMap->new( | ||||||||||||||||||||||||||||
| 188 | entries => { | ||||||||||||||||||||||||||||
| 189 | 'Foo' => KiokuDB::TypeMap::Entry::Naive->new, | ||||||||||||||||||||||||||||
| 190 | }, | ||||||||||||||||||||||||||||
| 191 | isa_entries => { | ||||||||||||||||||||||||||||
| 192 | 'My::Class' => KiokuDB::TypeMap::Entry::Naive->new, | ||||||||||||||||||||||||||||
| 193 | }, | ||||||||||||||||||||||||||||
| 194 | includes => [ | ||||||||||||||||||||||||||||
| 195 | $typemap_foo, | ||||||||||||||||||||||||||||
| 196 | $typemap_bar, | ||||||||||||||||||||||||||||
| 197 | ], | ||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | The L<KiokuDB> typemap maps classes to L<KiokuDB::TypeMap::Entry> objects. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | The mapping is by class, and entries can be keyed normally (using | ||||||||||||||||||||||||||||
| 205 | C<ref $object> equality) or by filtering on C<< $object->isa($class) >> | ||||||||||||||||||||||||||||
| 206 | (C<isa_entries>). | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =over 4 | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item entries | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | A hash of normal entries. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item isa_entries | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | A hash of C<< $object->isa >> based entries. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item includes | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | A list of parent typemaps to inherit entries from. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =back | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 METHODS | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =over 4 | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =item resolve $class | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Given a class returns the C<KiokuDB::TypeMap::Entry> object corresponding tot | ||||||||||||||||||||||||||||
| 233 | hat class. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Called by L<KiokuDB::TypeMap::Resover> | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =item resolve_entry $entry | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | If the entry is an alias, it will be resolved recursively, and simply returned | ||||||||||||||||||||||||||||
| 240 | otherwise. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =item all_entries | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Returns the merged C<entries> from this typemap and all the included typemaps. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =item all_isa_entries | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Returns the merged C<isa_entries> from this typemap and all the included | ||||||||||||||||||||||||||||
| 249 | typemaps. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =item all_isa_entry_classes | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | An array reference of all the classes in C<all_isa_entries>, sorted from least | ||||||||||||||||||||||||||||
| 254 | derived to most derived. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =back | ||||||||||||||||||||||||||||