| 1 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Data::Remember::Hybrid; | ||||||||||
| 5 | { | ||||||||||
| 6 | 3 | $Data::Remember::Hybrid::VERSION = '0.140490'; | |||||||||
| 7 | } | ||||||||||
| 8 | # ABSTRACT: a brain for Data::Remember with multiple personalities | ||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | use Data::Remember::Util | ||||||||||
| 12 | 3 | ▶ | process_que => { -as => '_process_que' }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | init_brain => { -as => '_init_brain' }; | |||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 17 | 3 | my $class = shift; | |||||||||
| 18 | 3 | my @table = @_; | |||||||||
| 19 | |||||||||||
| 20 | 3 | my $self = bless { root => undef, mounts => {} }, $class; | |||||||||
| 21 | |||||||||||
| 22 | 3 | ▶ | while (my ($que, $config) = splice @table, 0, 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 8 | $self->register_brain($que, $config); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | croak 'No root brain was registered!' | ||||||||||
| 27 | 3 | ▶ | unless defined $self->{root}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | |||||||||||
| 29 | 3 | return $self; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 8 | ▶ | sub register_brain { | ||||||||
|
Subroutine
register_brain: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||
| 34 | 8 | my $self = shift; | |||||||||
| 35 | 8 | my $que = shift; | |||||||||
| 36 | 8 | my $config = shift; | |||||||||
| 37 | |||||||||||
| 38 | 8 | ▶ | croak "You must give a que." unless defined $que; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 8 | ▶ | croak "You must give a configuration." unless defined $config; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 8 | $que = _process_que($que); | |||||||||
| 42 | 8 | ▶ | $config = [ $config ] unless ref $config; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 8 | ▶ | if (scalar(@$que) == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 3 | $self->{root} = _init_brain(@$config); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | else { | ||||||||||
| 49 | 5 | my $object = $self->{mounts}; | |||||||||
| 50 | 5 | for my $que_entry (@$que) { | |||||||||
| 51 | 7 | ▶ | croak 'You ran amuck of my secret special que name "__BRAIN". ' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | . 'I cannot work with such a que name.' | ||||||||||
| 53 | if $que_entry eq '__BRAIN'; | ||||||||||
| 54 | |||||||||||
| 55 | 7 | ▶ | if (defined $object->{$que_entry}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 1 | $object = $object->{$que_entry}; | |||||||||
| 57 | } | ||||||||||
| 58 | else { | ||||||||||
| 59 | 6 | $object = $object->{$que_entry} = {}; | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 5 | $object->{__BRAIN} = _init_brain(@$config); | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub unregister_brain { | ||||||||
|
Subroutine
unregister_brain: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 69 | 0 | my $self = shift; | |||||||||
| 70 | 0 | my $que = shift; | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | croak "You must give a que." unless defined $que; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 0 | $que = _process_que($que); | |||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | if (scalar(@$que) == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | croak 'You cannot unregister the root. You may, however, replace it ' | |||||||||
| 78 | . 'using register_brain()'; | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | my $object = $self->{mounts}; | |||||||||
| 82 | 0 | for my $que_entry (@$que) { | |||||||||
| 83 | 0 | ▶ | croak 'You ran amuck of my secret special que name "__BRAIN". ' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | . 'I cannot work with such a que name.' | ||||||||||
| 85 | if $que_entry eq '__BRAIN'; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | if (defined $object->{$que_entry}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | $object = $object->{$que_entry}; | |||||||||
| 89 | } | ||||||||||
| 90 | else { | ||||||||||
| 91 | 0 | return; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 0 | return scalar defined delete $object->{__BRAIN}; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | 8 | ▶ | sub brain_for { | ||||||||
|
Subroutine
brain_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 100 | 8 | my $self = shift; | |||||||||
| 101 | 8 | my $que = shift; | |||||||||
| 102 | |||||||||||
| 103 | 8 | $que = Data::Remember::Class::_process_que($que); | |||||||||
| 104 | 8 | push @$que, 'X'; | |||||||||
| 105 | |||||||||||
| 106 | 8 | my ($best_brain) = $self->_best_brain($que); | |||||||||
| 107 | 8 | return scalar $best_brain; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | 94 | ▶ | sub _best_brain { | ||||||||
|
Subroutine
_best_brain: called CC=4 SCAR=13.9
| |||||||||||
| 112 | 94 | my $self = shift; | |||||||||
| 113 | 94 | my $que = shift; | |||||||||
| 114 | |||||||||||
| 115 | 94 | my @sub_que = @$que; | |||||||||
| 116 | 94 | my $last_que = pop @sub_que; | |||||||||
| 117 | 94 | my $object = $self->{mounts}; | |||||||||
| 118 | |||||||||||
| 119 | 94 | my $best_brain = $self->{root}; | |||||||||
| 120 | 94 | my $best_que = [ @$que ]; | |||||||||
| 121 | |||||||||||
| 122 | 94 | ▶ | while (my $que_entry = shift @sub_que) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 67 | ▶ | if (defined $object->{$que_entry}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 37 | $object = $object->{$que_entry}; | |||||||||
| 125 | |||||||||||
| 126 | 37 | ▶ | if (defined $object->{__BRAIN}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 26 | $best_brain = $object->{__BRAIN}; | |||||||||
| 128 | 26 | $best_que = [ @sub_que, $last_que ]; | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | else { | ||||||||||
| 132 | 30 | last; | |||||||||
| 133 | } | ||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 94 | return ($best_brain, $best_que); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 23 | ▶ | sub remember { | ||||||||
|
Subroutine
remember: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 140 | 23 | my $self = shift; | |||||||||
| 141 | 23 | my $que = shift; | |||||||||
| 142 | 23 | my $fact = shift; | |||||||||
| 143 | |||||||||||
| 144 | 23 | my ($best_brain, $best_que) = $self->_best_brain($que); | |||||||||
| 145 | 23 | return $best_brain->remember($best_que, $fact); | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | 58 | ▶ | sub recall { | ||||||||
|
Subroutine
recall: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 150 | 58 | my $self = shift; | |||||||||
| 151 | 58 | my $que = shift; | |||||||||
| 152 | |||||||||||
| 153 | 58 | my ($best_brain, $best_que) = $self->_best_brain($que); | |||||||||
| 154 | 58 | return $best_brain->recall($best_que); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | 5 | ▶ | sub forget { | ||||||||
|
Subroutine
forget: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 159 | 5 | my $self = shift; | |||||||||
| 160 | 5 | my $que = shift; | |||||||||
| 161 | |||||||||||
| 162 | 5 | my ($best_brain, $best_que) = $self->_best_brain($que); | |||||||||
| 163 | 5 | return $best_brain->forget($best_que); | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | |||||||||||
| 167 | 3 | 1 | |||||||||
| 168 | |||||||||||
| 169 | __END__ | ||||||||||