File Coverage

blib/lib/Ordeal/Model/Backend/PlainFile.pm
Criterion Covered Total %
statement 68 75 90.6
branch 7 8 87.5
condition n/a
subroutine 16 17 94.1
pod 5 5 100.0
total 96 105 91.4


line stmt bran cond sub pod time code
1             package Ordeal::Model::Backend::PlainFile;
2              
3             # vim: ts=3 sts=3 sw=3 et ai :
4              
5 5     5   6453 use 5.020;
  5         20  
6 5     5   33 use strict; # redundant, but still useful to document
  5         9  
  5         103  
7 5     5   59 use warnings;
  5         13  
  5         273  
8             { our $VERSION = '0.003'; }
9 5     5   32 use Mo qw< default >;
  5         9  
  5         28  
10 5     5   1107 use Path::Tiny;
  5         10  
  5         285  
11 5     5   29 use Ouch;
  5         11  
  5         34  
12 5     5   2625 use autodie;
  5         62605  
  5         40  
13              
14 5     5   33952 use experimental qw< signatures postderef >;
  5         11  
  5         32  
15 5     5   823 no warnings qw< experimental::signatures experimental::postderef >;
  5         21  
  5         205  
16              
17 5     5   2015 use Ordeal::Model::Card;
  5         12  
  5         161  
18 5     5   33 use Ordeal::Model::Deck;
  5         10  
  5         3778  
19              
20             has base_directory => (default => 'ordeal-assets');
21              
22 29     29 1 378 sub card ($self, $id) {
  29         49  
  29         44  
  29         61  
23 29 100       207 my ($name, $extension) = $id =~ m{\A (.*) \. (.*) \z}mxs
24             or ouch 400, 'invalid identifier', $id;
25 28         80 my $content_type = $self->content_type_for($extension);
26 27         126 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     1   7 data => sub { $path->slurp_raw },
33 26         236 );
34             } ## end sub get_card
35              
36 28     28 1 43 sub content_type_for ($self, $extension) {
  28         40  
  28         46  
  28         39  
37 28         60 state $content_type_for = {
38             png => 'image/png',
39             jpg => 'image/jpeg',
40             svg => 'image/svg+xml',
41             };
42 28 100       98 my $content_type = $content_type_for->{lc($extension)}
43             or ouch 400, 'invalid extension', $extension;
44 27         58 return $content_type;
45             }
46              
47 5     5 1 32 sub deck ($self, $id) {
  5         10  
  5         8  
  5         7  
48 5         16 my $path = $self->path_for(decks => $id);
49              
50 4         11 my @cards;
51 4 50       15 if ($path->is_dir) {
52             @cards =
53 20         3996 map { $_->realpath->basename }
54 4         84 sort { $a->basename cmp $b->basename } $path->children;
  28         1748  
55             }
56             else {
57 0         0 @cards = $path->lines({chomp => 1});
58             }
59 4         905 $_ = $self->card($_) for @cards;
60              
61 4         92 return Ordeal::Model::Deck->new(
62             group => '',
63             id => $id,
64             name => $id,
65             cards => \@cards,
66             );
67             }
68              
69 0     0 1 0 sub decks ($s) {
  0         0  
  0         0  
70             return
71 0         0 grep { ! m{\A [._]}mxs } # nothing hidden or starting with underscore
72 0         0 map { $_->basename } # return identifiers of decks
  0         0  
73             path($s->base_directory)->children;
74             }
75              
76 32     32 1 51 sub path_for ($self, $type, $id) {
  32         48  
  32         48  
  32         42  
  32         43  
77 32         127 my $path = path($self->base_directory)->child($type => $id);
78 32 100       1850 $path->exists or ouch 404, 'not found', $id;
79 30         793 return $path;
80             }
81              
82             1;
83             __END__