| 1 | package PDF::Collage::TemplatesCollection; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | { our $VERSION = '0.002' } | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON::PP qw< decode_json >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use PDF::Collage::Template (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use experimental qw< signatures >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | has resolver => (is => ro => required => 1 => coerce => \&__resolver); | |||||||||||||||||||||||||||
| 17 | 2 | has _selectors => (is => 'lazy'); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ▶ | sub _build__selectors ($self) { | |||||||||||||||||||||||||||
|
Subroutine
_build__selectors: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | my @selectors = | ||||||||||||||||||||||||||||
| 21 | 9 | map { s{\.json}{}rmxs } | |||||||||||||||||||||||||||
| 22 | 6 | grep { m{\.json \z}mxs } | |||||||||||||||||||||||||||
| 23 | $self->resolver->get_sub_resolver('definitions')->list_asset_keys; | ||||||||||||||||||||||||||||
| 24 | 6 | return \@selectors; | |||||||||||||||||||||||||||
| 25 | } ## end sub _build__selectors | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub contains ($self, $sel) { defined($self->_selector_from($sel)) } | ||||||||||||||||||||||||||
|
Subroutine
contains: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ▶ | sub get ($self, $selector) { | |||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 21 | ▶ | $selector = $self->_selector_from($selector) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | or croak 'invalid selector'; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 19 | my $resolver = $self->resolver; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # my $def = $resolver->get_sub_resolver('definitions') | ||||||||||||||||||||||||||||
| 36 | # ->get_asset("$selector.json")->parsed_as_json; | ||||||||||||||||||||||||||||
| 37 | 19 | my $def = $resolver->get_asset("./definitions/$selector.json")->parsed_as_json; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 19 | ▶ | $def = { commands => $def } unless ref($def) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $def->{functions} = { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | as_data => sub ($key) { $resolver->get_asset($key)->raw_data }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 49 | ▶ | as_file => sub ($key) { $resolver->get_asset($key)->file }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 19 | }; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 19 | return PDF::Collage::Template->new($def); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | sub render ($self, $selector = undef, $data = undef) { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 12 | ▶ | ($selector, $data) = ($data, $selector) if ref($selector); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 12 | return $self->get($selector)->render($data); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # coercion function to get a Data::Resolver CDDE reference back | ||||||||||||||||||||||||||||
| 55 | ▶ | sub __resolver ($candidate) { | |||||||||||||||||||||||||||
|
Subroutine
__resolver: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | return $candidate if blessed($candidate); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 5 | my $class = $candidate->{class}; | |||||||||||||||||||||||||||
| 58 | 5 | my $path = "$class.pm" =~ s{::}{/}rgmxs; | |||||||||||||||||||||||||||
| 59 | 5 | require $path; | |||||||||||||||||||||||||||
| 60 | 5 | return $class->new($candidate->%*); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ▶ | sub _selector_from ($self, $selector) { | |||||||||||||||||||||||||||
|
Subroutine
_selector_from: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 64 | 21 | my $sels = $self->_selectors; | |||||||||||||||||||||||||||
| 65 | 21 | ▶ | $selector //= $sels->[0] if $sels->@* == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$selector //= $sels->[0]
MC/DC0%0/2
$selector //= $sels->[0]
$selector
$sels->[0]
Truth table66%2/3
A //= B
A= $selectorB= $sels->[0]
| |||||||||||||||||||||||||||||
| 66 | 21 | ▶ | return undef unless defined $selector; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 20 | my @candidates = grep { $selector eq $_ } $sels->@*; | |||||||||||||||||||||||||||
| 68 | 20 | ▶ | return @candidates ? $candidates[0] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | sub selectors ($self) { $self->_selectors->@* } | ||||||||||||||||||||||||||
|
Subroutine
selectors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | 1; | |||||||||||||||||||||||||||