| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package KiokuDB::TypeMap::Resolver; | ||||||||||||||||||||
| 4 | 22 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 22 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 22 | ▶ | use KiokuDB::TypeMap; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 22 | ▶ | use KiokuDB::TypeMap::Entry::MOP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 22 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 22 | has typemap => ( | |||||||||||||||||||
| 14 | does => "KiokuDB::Role::TypeMap", | ||||||||||||||||||||
| 15 | is => "ro", | ||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | has _compiled => ( | ||||||||||||||||||||
| 19 | isa => "HashRef", | ||||||||||||||||||||
| 20 | is => "ro", | ||||||||||||||||||||
| 21 | 82 | ▶ | default => sub { return {} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 22 | ); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has fallback_entry => ( | ||||||||||||||||||||
| 25 | does => "KiokuDB::TypeMap::Entry", | ||||||||||||||||||||
| 26 | is => "ro", | ||||||||||||||||||||
| 27 | 23 | ▶ | default => sub { KiokuDB::TypeMap::Entry::MOP->new }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 22 | ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub clear_compiled { | ||||||||||||||||||||
| 31 | 19 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
clear_compiled: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 19 | %{ $self->_compiled } = (); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub resolved { | ||||||||||||||||||||
| 37 | 29 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
resolved: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 29 | exists $self->_compiled->{$class}; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | sub collapse_method { | ||||||||||||||||||||
| 43 | 11964 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
collapse_method: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 11964 | return $self->find_or_resolve($class)->collapse_method; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub expand_method { | ||||||||||||||||||||
| 49 | 4306 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
expand_method: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 4306 | return $self->find_or_resolve($class)->expand_method; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub refresh_method { | ||||||||||||||||||||
| 55 | 27 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
refresh_method: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 27 | return $self->find_or_resolve($class)->refresh_method; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub id_method { | ||||||||||||||||||||
| 61 | 2140 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
id_method: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2140 | return $self->find_or_resolve($class)->id_method; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub compile_entry { | ||||||||||||||||||||
| 67 | 656 | ▶ | my ( $self, $class, $entry ) = @_; | ||||||||||||||||||
|
Subroutine
compile_entry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 656 | return $self->register_compiled( $class, $entry->compile($class, $self) ); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | sub register_compiled { | ||||||||||||||||||||
| 73 | 656 | ▶ | my ( $self, $class, $compiled ) = @_; | ||||||||||||||||||
|
Subroutine
register_compiled: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 656 | return ( $self->_compiled->{$class} = $compiled ); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub find_or_resolve { | ||||||||||||||||||||
| 79 | 18437 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
find_or_resolve: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 18437 | ▶ | return ( $self->_compiled->{$class} || $self->resolve($class) ); | ||||||||||||||||||
|
Condition100%3/3
$self->_compiled->{$class} || $self->resolve($class)
MC/DC0%0/2
$self->_compiled->{$class} || $self->resolve($class)
$self->_compiled->{$class}
$self->resolve($class)
Truth table66%2/3
A || B
A= $self->_compiled->{$class}B= $self->resolve($class)
| |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub resolve { | ||||||||||||||||||||
| 85 | 670 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
resolve: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 670 | ▶ | if ( my $entry = $self->typemap->resolve($class) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 393 | return $self->compile_entry( $class, $entry ); | |||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||
| 90 | 277 | return $self->resolve_fallback($class); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | sub resolve_fallback { | ||||||||||||||||||||
| 95 | 277 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
resolve_fallback: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 277 | ▶ | if ( my $meta = Class::MOP::get_metaclass_by_name($class) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 263 | return $self->resolve_fallback_with_meta($class, $meta); | |||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||
| 100 | 14 | return $self->resolve_fallback_without_meta($class); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub resolve_fallback_with_meta { | ||||||||||||||||||||
| 105 | 263 | ▶ | my ( $self, $class, $meta ) = @_; | ||||||||||||||||||
|
Subroutine
resolve_fallback_with_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # FIXME only allow with Storage? | ||||||||||||||||||||
| 108 | 263 | return $self->compile_entry( $class => $self->fallback_entry ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub resolve_fallback_without_meta { | ||||||||||||||||||||
| 112 | 14 | ▶ | my ( $self, $class ) = @_; | ||||||||||||||||||
|
Subroutine
resolve_fallback_without_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 14 | croak "$class has no metaclass, please provide a typemap entry or add to the allowed classes"; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 22 | __PACKAGE__ | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =pod | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 NAME | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | KiokuDB::TypeMap::Resolver - Caching resolver for L<KiokuDB::TypeMap> | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 SYNOPSIS | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | This object is used by L<KiokuDB::Linker> and L<KiokuDB::Collapser> to map | ||||||||||||||||||||
| 132 | class names to collapsing/expanding method bodies. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Since L<KiokuDB::TypeMap>s are fairly complex, and L<KiokuDB::TypeMap::Entry> | ||||||||||||||||||||
| 135 | objects can benefit from specializing to a class by precomputing some things, | ||||||||||||||||||||
| 136 | resolution is performed once per class, and the results are cached in the | ||||||||||||||||||||
| 137 | resolver. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||