| 1 | package Bot::Cobalt::Core::ContextMeta; | ||||||||||||||||||||||||||||
| 2 | 2 | $Bot::Cobalt::Core::ContextMeta::VERSION = '0.021003'; | |||||||||||||||||||||||||||
| 3 | ## Base class for context-specific dynamic hashes | ||||||||||||||||||||||||||||
| 4 | ## (ignores, auth, .. ) | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Bot::Cobalt::Common ':types'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Scalar::Util 'reftype'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has _list => ( | ||||||||||||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||||||||||
| 19 | isa => HashRef, | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | builder => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build__list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 3 | my ($self, $context, $key, $meta) = @_; | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | confess "add() needs at least a context and key" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $context and defined $key
MC/DC0%0/2
defined $context and defined $key
defined $context
defined $key
Truth table33%1/3
A and B
A= defined $contextB= defined $key
| |||||||||||||||||||||||||||||
| 27 | unless defined $context and defined $key; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | my $ref = +{ AddedAt => time }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ## Allow AddedAt to be adjusted: | ||||||||||||||||||||||||||||
| 32 | 3 | ▶ | if (ref $meta && reftype $meta eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $meta and builtin::reftype($meta) eq "HASH"
MC/DC0%0/2
ref $meta and builtin::reftype($meta) eq "HASH"
ref $meta
builtin::reftype($meta) eq "HASH"
Truth table33%1/3
A and B
A= ref $metaB= builtin::reftype($meta) eq "HASH"
| |||||||||||||||||||||||||||||
| 33 | 3 | $ref->{$_} = $meta->{$_} for keys %$meta; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | $self->_list->{$context}->{$key} = $ref; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | $key | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 1 | my ($self, $context) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $self->_list(+{}) unless defined $context; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | delete $self->_list->{$context} | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | sub del { | ||||||||||||||||||||||||||
|
Subroutine
del: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 1 | my ($self, $context, $key) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | confess "del() needs a context and item" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $context and defined $key
MC/DC0%0/2
defined $context and defined $key
defined $context
defined $key
Truth table33%1/3
A and B
A= defined $contextB= defined $key
| |||||||||||||||||||||||||||||
| 53 | unless defined $context and defined $key; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | my $list = $self->_list->{$context} // return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_list->{$context} // (return)
MC/DC0%0/1
$self->_list->{$context} // (return)
$self->_list->{$context}
Truth table50%1/2
A
A= $self->_list->{$context}
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | delete $list->{$key} | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 4 | my ($self, $context, $key) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | ▶ | confess "fetch() needs a context and key" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $context and defined $key
MC/DC0%0/2
defined $context and defined $key
defined $context
defined $key
Truth table33%1/3
A and B
A= defined $contextB= defined $key
| |||||||||||||||||||||||||||||
| 64 | unless defined $context and defined $key; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | return unless exists $self->_list->{$context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | $self->_list->{$context}->{$key} | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 3 | ▶ | wantarray ? $self->list_as_array(@_) : $self->list_as_ref(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | ## Less ambiguous list methods. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub list_as_array { keys %{ shift->list_as_ref(@_) || +{} } } | ||||||||||||||||||||||||||
|
Subroutine
list_as_array: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
(shift())->list_as_ref(@_) || {}
MC/DC0%0/1
(shift())->list_as_ref(@_) || {}
(shift())->list_as_ref(@_)
Truth table0%0/2
A
A= (shift())->list_as_ref(@_)
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | sub list_as_ref { | ||||||||||||||||||||||||||
|
Subroutine
list_as_ref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 3 | my ($self, $context) = @_; | |||||||||||||||||||||||||||
| 82 | 3 | ▶ | defined $context ? $self->_list->{$context} : $self->_list ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | 1; | |||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||