| 1 | package Ordeal::Model; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # vim: ts=3 sts=3 sw=3 et ai : | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | { our $VERSION = '0.002'; } | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use English qw< -no_match_vars >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Ouch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Mo qw< default >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Module::Runtime qw< use_module require_module is_module_name >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Ordeal::Model::ChaCha20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Ordeal::Model::Evaluator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Ordeal::Model::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use experimental qw< signatures postderef >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | no warnings qw< experimental::signatures experimental::postderef >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | has 'backend'; | |||||||||||||||||||||||||||
| 25 | has random_source => ( | ||||||||||||||||||||||||||||
| 26 | default => sub { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | require Ordeal::Model::ChaCha20; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 28 | 0 | return Ordeal::Model::ChaCha20->new; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 5 | ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ▶ | sub _backend_factory ($package, $name, @args) { | |||||||||||||||||||||||||||
|
Subroutine
_backend_factory: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | $name = $package->resolve_backend_name($name); | |||||||||||||||||||||||||||
| 34 | 1 | return use_module($name)->new(@args); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | ▶ | sub _default_backend ($package) { | |||||||||||||||||||||||||||
|
Subroutine
_default_backend: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 0 | require Ordeal::Model::Backend::PlainFile; | |||||||||||||||||||||||||||
| 39 | 0 | return Ordeal::Model::Backend::PlainFile->new; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ▶ | sub evaluate ($self, $what, %args) { | |||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | my $ast = ref($what) ? $what : $self->parse($what); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 2 | return Ordeal::Model::Evaluator::EVALUATE( | |||||||||||||||||||||||||||
| 45 | ast => $ast, | ||||||||||||||||||||||||||||
| 46 | model => $self, | ||||||||||||||||||||||||||||
| 47 | random_source => $self->_random_source(%args), | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | sub get_card ($self, $id) { return $self->backend->card($id) } | ||||||||||||||||||||||||||
|
Subroutine
get_card: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | sub get_deck ($self, $id) { return $self->backend->deck($id) } | ||||||||||||||||||||||||||
|
Subroutine
get_deck: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub get_deck_ids ($self) { return $self->backend->decks } | ||||||||||||||||||||||||||
|
Subroutine
get_deck_ids: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ▶ | sub new ($package, @rest) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | my %args = (@_ && ref($_[0])) ? %{$rest[0]} : @rest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 57 | 4 | my $backend; | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | if (defined(my $b = $args{backend})) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 59 | $backend = blessed($b) ? $args{backend} | ||||||||||||||||||||||||||||
| 60 | 3 | ▶ | : (ref($b) eq 'ARRAY') ? $package->_backend_factory(@$b) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | : ouch 400, 'invalid backend'; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif (scalar(keys %args) == 0) { | ||||||||||||||||||||||||||||
| 64 | 0 | $backend = $package->_default_backend; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif (scalar(keys %args) == 1) { | ||||||||||||||||||||||||||||
| 67 | 1 | my ($name, $as) = %args; | |||||||||||||||||||||||||||
| 68 | 1 | ▶ | my @args = ref($as) eq 'ARRAY' ? @$as : %$as; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | $backend = $package->_backend_factory($name, @args); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | 0 | ouch 400, 'too many arguments to initialize Model'; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | return $package->SUPER::new(backend => $backend); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ▶ | sub parse ($self, $text) { | |||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | ouch 400, 'undefined input expression to parse()' unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | return Ordeal::Model::Parser::PARSE($text); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ▶ | sub _random_source ($self, %args) { | |||||||||||||||||||||||||||
|
Subroutine
_random_source: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | return $args{random_source} if $args{random_source}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | return Ordeal::Model::ChaCha20->new->restore($args{random_source_state}) | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | if defined $args{random_source_state}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | return Ordeal::Model::ChaCha20->new(seed => $args{seed}) | ||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if defined $args{seed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | return $self->random_source; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | ▶ | sub resolve_backend_name ($package, $name) { | |||||||||||||||||||||||||||
|
Subroutine
resolve_backend_name: called CC=9 SCAR=27.5
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | $package = ref($package) || $package; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $package || $package
MC/DC0%0/2
ref $package || $package
ref $package
$package
Truth table33%1/3
A || B
A= ref $packageB= $package
| |||||||||||||||||||||||||||||
| 97 | 1 | my $invalid_error = "invalid name '$name' for module resolution"; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # if it has "::" *inside* but does not start with them, use directly | ||||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (($name =~ s{\A - }{}mxs) || ($name =~ m{\A [^:]+ ::})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name =~ s/\A - //msux or $name =~ /\A [^:]+ ::/u
MC/DC0%0/2
$name =~ s/\A - //msux or $name =~ /\A [^:]+ ::/u
$name =~ s/\A - //msux
$name =~ /\A [^:]+ ::/u
Truth table33%1/3
A or B
A= $name =~ s/\A - //msuxB= $name =~ /\A [^:]+ ::/u
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | is_module_name($name) or ouch 400, $invalid_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | return $name; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # otherwise, remove any leading "::" | ||||||||||||||||||||||||||||
| 106 | 1 | $name =~ s{\A ::}{}mxs; | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | is_module_name($name) or ouch 400, $invalid_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # look for classes inside "backend" kind | ||||||||||||||||||||||||||||
| 110 | 1 | my %flag; | |||||||||||||||||||||||||||
| 111 | 1 | for my $base ($package, __PACKAGE__) { | |||||||||||||||||||||||||||
| 112 | 1 | ▶ | next if $flag{$base}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | my $class = $base . '::Backend::' . $name; | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | eval { require_module($class) } and return $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ouch 400, "cannot resolve '$name' to a backend module package"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | 1; | |||||||||||||||||||||||||||