| 1 | package Ordeal::Model::Backend::PlainFile; | ||||||||||
| 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; # redundant, but still useful to document | ||||||||
|
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 | 5 | ▶ | use Mo qw< default >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 5 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Ouch; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | use experimental qw< signatures postderef >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | no warnings qw< experimental::signatures experimental::postderef >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 5 | ▶ | use Ordeal::Model::Card; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | use Ordeal::Model::Deck; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 5 | has base_directory => (default => 'ordeal-assets'); | |||||||||
| 21 | |||||||||||
| 22 | ▶ | sub card ($self, $id) { | |||||||||
|
Subroutine
card: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 23 | 29 | ▶ | my ($name, $extension) = $id =~ m{\A (.*) \. (.*) \z}mxs | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | or ouch 400, 'invalid identifier', $id; | ||||||||||
| 25 | 28 | my $content_type = $self->content_type_for($extension); | |||||||||
| 26 | 27 | my $path = $self->path_for(cards => $id); | |||||||||
| 27 | return Ordeal::Model::Card->new( | ||||||||||
| 28 | content_type => $content_type, | ||||||||||
| 29 | group => '', | ||||||||||
| 30 | id => $id, | ||||||||||
| 31 | name => $name, | ||||||||||
| 32 | 1 | ▶ | data => sub { $path->slurp_raw }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 26 | ); | |||||||||
| 34 | } ## end sub get_card | ||||||||||
| 35 | |||||||||||
| 36 | ▶ | sub content_type_for ($self, $extension) { | |||||||||
|
Subroutine
content_type_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 37 | 28 | state $content_type_for = { | |||||||||
| 38 | png => 'image/png', | ||||||||||
| 39 | jpg => 'image/jpeg', | ||||||||||
| 40 | svg => 'image/svg+xml', | ||||||||||
| 41 | }; | ||||||||||
| 42 | 28 | ▶ | my $content_type = $content_type_for->{lc($extension)} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | or ouch 400, 'invalid extension', $extension; | ||||||||||
| 44 | 27 | return $content_type; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | ▶ | sub deck ($self, $id) { | |||||||||
|
Subroutine
deck: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 48 | 5 | my $path = $self->path_for(decks => $id); | |||||||||
| 49 | |||||||||||
| 50 | 4 | my @cards; | |||||||||
| 51 | 4 | ▶ | if ($path->is_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | @cards = | ||||||||||
| 53 | 20 | map { $_->realpath->basename } | |||||||||
| 54 | 4 | sort { $a->basename cmp $b->basename } $path->children; | |||||||||
| 55 | } | ||||||||||
| 56 | else { | ||||||||||
| 57 | 0 | @cards = $path->lines({chomp => 1}); | |||||||||
| 58 | } | ||||||||||
| 59 | 4 | $_ = $self->card($_) for @cards; | |||||||||
| 60 | |||||||||||
| 61 | 4 | return Ordeal::Model::Deck->new( | |||||||||
| 62 | group => '', | ||||||||||
| 63 | id => $id, | ||||||||||
| 64 | name => $id, | ||||||||||
| 65 | cards => \@cards, | ||||||||||
| 66 | ); | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | ▶ | sub decks ($s) { | |||||||||
|
Subroutine
decks: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | return | ||||||||||
| 71 | 0 | grep { ! m{\A [._]}mxs } # nothing hidden or starting with underscore | |||||||||
| 72 | 0 | map { $_->basename } # return identifiers of decks | |||||||||
| 73 | path($s->base_directory)->children; | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | ▶ | sub path_for ($self, $type, $id) { | |||||||||
|
Subroutine
path_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 77 | 32 | my $path = path($self->base_directory)->child($type => $id); | |||||||||
| 78 | 32 | ▶ | $path->exists or ouch 404, 'not found', $id; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 30 | return $path; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 5 | 1; | |||||||||
| 83 | __END__ | ||||||||||